简体   繁体   English

无法使用Python在图形中绘制日期

[英]Can't plot dates in graph with Python

I'm trying to plot a graph dates vs. counts. 我正在尝试绘制日期与计数的图表。 This is my code: 这是我的代码:

trip_data['start_date'] = sorted(trip_data['start_date'], key=lambda x: datetime.datetime.strptime(x, '%Y-%m-%d'))
data_count = pd.DataFrame(data=trip_data['start_date'].value_counts().sort_index())
data_count.rename(columns={'start_date' : 'count'}, inplace = True)
data_count.reset_index(inplace=True)
data_count.rename(columns={'index' : 'start_date'}, inplace = True)
data_count.plot()

And I get this graph: 我得到这张图:

So clearly the dates won't plot correctly, they just add up as int. 因此,很明显日期无法正确绘制,它们只是加为int。 I've tried a number of solutions I've found on Google and here on Stackoverflow, but none seems to work. 我已经尝试过在Google和Stackoverflow上找到的许多解决方案,但是似乎都没有用。 The majority of them also had stripping dates as the main point, but as the code shows that has already been done. 他们中的大多数人还以剥离日期为重点,但是如代码所示,这已经完成了。 Plotting a hist graph also reverses the x and y axes, if that's useful. 如果有用的话,绘制历史图也会使x和y轴反转。 Any thoughts? 有什么想法吗? Thank you in advance! 先感谢您!

An option you have is to change the x-axis labels. 您可以选择更改x轴标签。 Assuming your plot's current x-axis is using the index of the dataframe, you can use this code: 假设绘图的当前x轴使用数据框的索引,则可以使用以下代码:

labels = data_count['start_date'].tolist()
plt.xticks(data_count.index.values, labels, rotation='vertical')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM