Home > database >  Problems plotting price data against my datetime which is indexed
Problems plotting price data against my datetime which is indexed

Time:04-25

Here is my code:

df=pd.read_csv('data.csv')
df['datetime']=pd.to_datetime(df['datetime'])
df=df.set_index('datetime')
data = df.filter(['avgLowPrice'])
plt.plot(data['avgLowPrice'])
plt.show()

the graph looks like this:enter image description here I have no idea why its doing this...

CodePudding user response:

I suppose that your DataFrame is not sorted by the index, i.e. consecutive rows have "intermixed" (instead of ordered) index values.

Sort your DataFrame, even in-place:

df.sort_index(inplace=True)

and then generate your plot.

Another (not related) hint, to make your code more concise:

To read your input file, convert datetime column to datetime and set it as the index, in one go, run:

df = pd.read_csv('data.csv', parse_dates=['datetime'], index_col='datetime')
  • Related