首页 > 解决方案 > 在 python matplotlib 中绘制 x 轴的空格日期

问题描述

我在 python 中有一个时间序列图,它产生了我想要的结果,除了轴不太正确......

下图:

在此处输入图像描述

我只是想把它隔开整个情节图......

我的代码如下:

x2 = all_sentiment['date']
x2 = pd.Series(list(set(x2)))
y2 = all_sentiment['sent_count']

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20, 10))
axes.plot(range(len(s_pos)), s_pos, linewidth=2, color='green')
axes.plot(range(len(s_neg)), s_neg, linewidth=2, color='red')
#axes.set_xticks(x2.index.tolist())
axes.set_xticks(np.arange(len(x2.index.tolist())))
axes.set_xticklabels([date.strftime("%Y-%m-%d") for date in x2])
axes.margins = 0
axes.set_xlabel('Date/Time')
axes.set_ylabel('Num of Tweets')
axes.set_title('Number of Tweets Over Time - Positive and Negative')
axes.set_xlim(0, len(y2))
axes.legend(loc="upper right", labels=['Positive', 'Negative'])
fig.subplots_adjust(hspace=1)
plt.show()

你能指出我在这里做错了什么吗?

非常感谢。

标签: pythonmatplotlib

解决方案


推荐阅读