Scatter plot form dataframe with index on x-axis

后端 未结 2 1953
甜味超标
甜味超标 2020-12-01 18:15

I\'ve got pandas DataFrame, df, with index named date and the columns columnA, columnB and

相关标签:
2条回答
  • 2020-12-01 18:40

    A more simple solution would be:

    df['x1'] = df.index
    
    df.plot(kind='scatter', x='x1', y='columnA')
    

    Just create the index variable outside of the plot statement.

    0 讨论(0)
  • 2020-12-01 18:42

    This is kind of ugly (I think the matplotlib solution you used in your question is better, FWIW), but you can always create a temporary DataFrame with the index as a column usinng

    df.reset_index()
    

    If the index was nameless, the default name will be 'index'. Assuming this is the case, you could use

    df.reset_index().plot(kind='scatter', x='index', y='columnA')
    
    0 讨论(0)
提交回复
热议问题