首页 > 解决方案 > 如何在 Matplotlib 中正确设置日期?

问题描述

x 轴上的年份不正确;表格格式为 YY-MM-DD。我错过了代码的哪一部分?你能提个建议吗?

date    new_sentiment   count
0   2020-03-06  POSITIVE    1
1   2020-03-17  NEGATIVE    2
2   2020-03-18  NEUTRAL 1
3   2020-03-21  NEUTRAL 1
4   2020-03-23  NEUTRAL 1
... ... ... ...
831 2021-05-30  NEGATIVE    92
832 2021-05-30  NEUTRAL 84
833 2021-05-30  POSITIVE    26
834 2021-05-31  NEGATIVE    19
835 2021-05-31  NEUTRAL 7

我的代码

sns.lineplot(data=data, x='date', y='count', hue='new_sentiment')
sns.set_style("whitegrid", {
    "ytick.major.size": 0.1,
    "ytick.minor.size": 0.05,
    'grid.linestyle': 'solid'
 })
plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")
plt.setp(plt.gca().xaxis.get_majorticklabels(),rotation=90)
myFmt = mdates.DateFormatter('%d-%b-%Y')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=16))
plt.xlim('2020-03-06','2021-06-15')
plt.savefig('04_#clean_sentiment_count_2020_2021.tiff', dpi=300, format='tiff', bbox_inches='tight')

输出 图形

标签: pythondatematplotlibgraphx-axis

解决方案


推荐阅读