Doctor White Doctor White - 5 months ago 10
Python Question

Create continuous graph using matplotlib and pandas

I have a dataframe like this.

column1 column2 column3
MyIndexes
7 22 90 98
8 50 06 56
23 60 58 44
49 30 62 00


I am using df.plot to plot a line chart. The problem is that using df.plot() treats the index as categorical data and plots graph for each of them (7, 8, 23 and 49). However I want these to be treated as numeric values and have a graph with even xticks and then plot these points into the graph. How will I be able to do that?

Answer

When I construct the dataframe as such:

df = pd.DataFrame([[22, 90, 98],
                   [50, 06, 56],
                   [60, 58, 44],
                   [30, 62, 00]],
                  index=pd.Index([7, 8, 23, 49], name='MyIndexex'),
                  columns=['column1', 'column2', 'column3'])

print df

           column1  column2  column3
MyIndexex                           
7               22       90       98
8               50        6       56
23              60       58       44
49              30       62        0

then plot:

df.plot()

enter image description here

I suspect your index is not what you think it is.

To force your index to be integers do:

df.index = df.index.astype(int)
df.plot()
Comments