Reputation: 4546
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 for the markers.
If I use .plot_date
or .plot()
and using the '-' marker, my data doesn't plot correctly:
Does anybody know why this is happening and how it can be fixed? I need the data points to be connect with lines.
Thanks in advance.
Here's my current code:
import pandas as pd
import matplotlib.pyplot as plt
import dateutil
from datetime import date, datetime, timedelta
plt.plot(sve2_all['MeHg ng/l']['1993-01-18':'1997-05-02'].index, sve2_all['MeHg ng/l']['1993-01-18':'1997-05-02'],'bo')
plt.xticks(rotation=70)
plt.show()
My data is in a Pandas DataFrame
and the index is datetime64
.
Upvotes: 2
Views: 1215
Reputation: 4661
For Pandas's Series
you can just ser.dropna()
as @RutgerKassies suggested. However this will not work for DataFrame
s. 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.
Upvotes: 0
Reputation: 64463
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-')
Upvotes: 2