Skip to content Skip to sidebar Skip to footer

Unexpected Plotting Behaviour Using Matplotlib And Time Series Data

I'd like to plot a line graph of a time series using matlplotlib, however matplotlib will not plot all of my data unless I use .plot_date() or use .plot() passing o as the argument

Solution 1:

You probably have a lot of NaN values in your Dataframe. Matplotlib only draws a line between consecutive (valid) data points, and leaves a gap at NaN values.

If that's the case, removing the NaN's before plotting should do the trick. For example:

dftmp = sve2_all['MeHg ng/l']['1993-01-18':'1997-05-02'].dropna()
plt.plot(dftmp.index, dftmp,'b-')

Solution 2:

For Pandas's Series you can just ser.dropna() as @RutgerKassies suggested. However this will not work for DataFrames. For DataFrames someone solved the issue using masks. Or you can just df.fillna(method='ffill'):

sve2_all['MeHg ng/l ffill']= sve2_all['MeHg ng/l'].fillna(method='ffill')
sve2_all['MeHg ng/2 ffill']= sve2_all['MeHg ng/2'].fillna(method='ffill')
sve2_all[['MeHg ng/l ffill','MeHg ng/2 ffill']].plot()

make sure forward filling is logical to your data model.

Post a Comment for "Unexpected Plotting Behaviour Using Matplotlib And Time Series Data"