matplotlib DateFormatter 未显示带有 yyyy-mm-dd 列的正确日期 [英] matplotlib DateFormatter not showing correct dates with yyyy-mm-dd column
问题描述
我有一个带有两列的python数据框,y轴上的数字列(total_outbounds),x轴上的日期列(月,请输入错误名称):
I have a python dataframe with two columns, a numeric column (total_outbounds) on the y-axis and a date column (month, pardon the bad name) for x-axis:
以及当我运行以下代码以使用此数据框创建图形时:
and when when I run this code to create a graph using this dataframe:
fig,ax = plt.subplots()
my_df.plot(x='month', y='total_outbounds', ax=ax, label = 'Total Email Outbounds on LE Change')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%y'))
plt.xlabel('')
plt.title('Total LE Changes and Outbounds by Month', pad = 10)
我收到一个X轴不是我想要的图形...我使用 mdates.DateFormatter
错误吗?希望在 X 轴上接收 mm/yy,而不是当前显示的 Apr、Jul 等.
I receive a graph where the X-axis is not what I was hoping for... Am I using mdates.DateFormatter
wrong? Looking to receive mm/yy on the X-Axis, instead of the Apr, Jul, etc. that are currently appearing.
为了重现性,这里是带有 my_df.to_dict()
For reproducibility, here is the dataframe output with my_df.to_dict()
{'month': {0: Timestamp('2020-01-01 00:00:00'),
1: Timestamp('2020-02-01 00:00:00'),
2: Timestamp('2020-03-01 00:00:00'),
3: Timestamp('2020-04-01 00:00:00'),
4: Timestamp('2020-05-01 00:00:00'),
5: Timestamp('2020-06-01 00:00:00'),
6: Timestamp('2020-07-01 00:00:00'),
7: Timestamp('2020-08-01 00:00:00'),
8: Timestamp('2020-09-01 00:00:00'),
9: Timestamp('2020-10-01 00:00:00'),
10: Timestamp('2020-11-01 00:00:00'),
11: Timestamp('2020-12-01 00:00:00'),
12: Timestamp('2021-01-01 00:00:00'),
13: Timestamp('2021-02-01 00:00:00'),
14: Timestamp('2021-03-01 00:00:00')},
'total_outbounds': {0: 26364,
1: 33081,
2: 35517,
3: 34975,
4: 40794,
5: 51659,
6: 50948,
7: 65332,
8: 82839,
9: 96408,
10: 86923,
11: 99176,
12: 122199,
13: 116057,
14: 108439}}
,我认为您应该能够使用pd.DataFrame.from_dict()将其从字典转换回数据框 my_df
.如果有更可重现的方式来共享数据框,请告诉我.
and I think you should be able to use pd.DataFrame.from_dict() to turn that back into a dataframe my_df
from the dictionary. Please let me know if there's a more reproducible way to share the dataframe.
注释中的解决方案有效,但是现在我无法使用 plt.xaxis(rotation=50)
旋转次要刻度,这只旋转两个主要刻度滴答声...出现的 X 轴值也很奇怪(将 71
显示为年份?)
the solution in the comments works, however now I cannot rotate the minor ticks using plt.xaxis(rotation=50)
, this only rotates the two major ticks... also the X-axis values appearing are odd (showing 71
as the year?)
推荐答案
如评论中所述,Apr/Jul/Oct是次要刻度.
As discussed in the comments, the Apr/Jul/Oct are minor ticks.
但是,建议不要增加主刻度线和副刻度线,而建议自定义主刻度线频率,禁用次刻度线,并使用 autofmt_xdate()
设置日期刻度的样式:
However, rather than customizing both major/minor ticks, I suggest increasing the major tick frequency, disabling minor ticks, and using autofmt_xdate()
to style the date ticks:
fig, ax = plt.subplots()
ax.plot(df.month, df.total_outbounds, label='Total Email Outbounds on LE Change')
ax.legend()
# increase the major tick frequency (8 ticks in this example)
start, end = ax.get_xlim()
xticks = np.linspace(start, end, 8)
ax.set_xticks(xticks)
ax.set_xticklabels(xticks)
# set date format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%y'))
# use matplotlib's auto date styling
fig.autofmt_xdate()
# disable minor ticks
plt.minorticks_off()
这篇关于matplotlib DateFormatter 未显示带有 yyyy-mm-dd 列的正确日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!