使用 matplotlib 和时间序列数据的意外绘图行为 [英] Unexpected plotting behaviour using matplotlib and time series data
问题描述
我想使用 matlplotlib 绘制时间序列的折线图,但是 matplotlib 不会绘制我的所有数据,除非我使用 .plot_date()
或使用 .plot()
传递 o
作为标记的参数.
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.
如果我使用 .plot_date
或 .plot()
并使用-"标记,我的数据不能正确绘制:
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.
提前致谢.
这是我当前的代码:
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()
我的数据在 Pandas DataFrame
中,索引是 datetime64
.
My data is in a Pandas DataFrame
and the index is datetime64
.
推荐答案
您的 Dataframe 中可能有很多 NaN
值.Matplotlib 仅在连续(有效)数据点之间画一条线,并在 NaN
值处留有空隙.
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.
如果是这种情况,在绘图之前删除 NaN
应该可以解决问题.例如:
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-')
这篇关于使用 matplotlib 和时间序列数据的意外绘图行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!