我有3个pandas数据帧的dict(“groups_dict”),其中包含以下属性:
print(groups_dict['high'].info())
print(groups_dict['medium'].info())
print(groups_dict['low'].info())
# OUTPUT
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1241 entries, 0 to 1240
Data columns (total 2 columns):
date 1241 non-null datetime64[ns]
ppg 1241 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 19.5 KB
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1238 entries, 0 to 1237
Data columns (total 2 columns):
date 1238 non-null datetime64[ns]
ppg 1238 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 19.4 KB
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1179 entries, 0 to 1178
Data columns (total 2 columns):
date 1179 non-null datetime64[ns]
ppg 1179 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 18.5 KB
我编写了以下脚本来绘制3个数据帧中每个数据帧的时间序列散点图:
fig, axs = plt.subplots(3, 1, figsize=(15,15))
for ax, key in zip(axs, groups_dict.keys()):
df = groups_dict[key]
ax.scatter(df.index, df.ppg, s=5, alpha=0.5)
ax.set_title(f"{key} quality")
ax.set_ylabel('price per gram ($)')
ax.set_xlabel('date')
ax.set_xticklabels([dt.strftime('%m-%d-%Y') for dt in df.date], rotation=0)
请在此处查看输出:https://i.imgur.com/zlF4XAu.png
问题是我无法在x轴上正确显示日期。如果我在每个数据框中打印最小/最大日期,我发现实际上的日期范围比上面的图表中显示的范围要广泛得多。
for key, df in groups_dict.items():
print("df name:",key)
print("min date:", df.date.min())
print("max date:", df.date.max(),'\n')
# OUTPUT
df name: high
min date: 2010-09-02 00:00:00
max date: 2014-05-13 00:00:00
df name: low
min date: 2010-09-02 00:00:00
max date: 2014-05-13 00:00:00
df name: medium
min date: 2010-09-02 00:00:00
max date: 2014-05-13 00:00:00
作为检查以确保我传递ax.set_xticklabels()正确数量的唯一身份:
for d in groups_dict.values():
xtickarg = len(set([dt.strftime('%m-%d-%Y') for dt in d.date.tolist()]))
print(d.shape[0], "==", xtickarg)
# OUTPUT
1241 == 1241
1179 == 1179
1238 == 1238
如果有人能够提供有关xticklabels未正确显示的原因的见解,我将非常感激。提前致谢。
答案 0 :(得分:0)
x轴上有7个刻度,但是你试图为这7个刻度设置超过1000个标签。尽管如此,因为只有7个刻度可用,不会超过7个。
直接绘制日期可能是有意义的,例如
import matplotlib.dates
dates = matplotlib.dates.date2num(d.date.tolist())
ax.scatter(dates, df.ppg, s=5, alpha=0.5)
然后使用一些有用的自动收报机,例如一个MonthLocator:
ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())