[英]How to plot.show() datetime formatted data “mm-dd-yy hh:mm:ss” in PyPlot?
I am trying to plot a dataset from data.boston.gov ( https://data.boston.gov/dataset/central-library-electricity-usage ) using Anaconda's distribution of Spyder. 我正在尝试使用Anaconda的Spyder分布从data.boston.gov( https://data.boston.gov/dataset/central-library-electricity-usage )绘制数据集。 Original dataset contains more than 2x10^5 instances so I have confined to 2018. Plot won't show.
原始数据集包含2x10 ^ 5个以上的实例,因此我仅限于2018年。该图将不会显示。
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('bpl_energy_2018.csv')
plt.plot(data.datetime_measured,data.total_demand_kw)
plt.show()
['datetime_measured','total_demand_kw']
- 0 12-31-18 23:55:00 561
- 1 12-31-18 23:50:00 568
- 2 12-31-18 23:45:00 576
...
- 53690 01-01-18 03:40:00 770
- 53691 01-01-18 03:30:00 813
- 53692 01-01-18 02:55:00 777
[53693 rows x 2 columns]
I think the reason it's not working is because your data is all out of order, so matplotlib doesn't know what to do with the values you're giving it. 我认为它不起作用的原因是因为您的数据全部乱了,所以matplotlib不知道如何处理您提供的值。
Pandas has some built-in plotting features, so you should be able to plot your data just with Pandas具有一些内置的绘图功能,因此您应该能够使用
data.plot()
plt.show()
The plot then looks like this: 情节看起来像这样:
But that is basically just random noise. 但这基本上只是随机噪声。 If you look at the values in the CSV, you'll see that they're not sorted perfectly by time.
如果您查看CSV中的值,则会发现它们未按时间完美排序。 We can fix this without too much trouble, though:
不过,我们可以解决此问题而没有太多麻烦:
data.sort_values('datetime_measured', inplace=True)
data.reset_index(drop=True, inplace=True)
If we plot it again, we get this: 如果再次绘制,将得到以下结果:
.
。
Since you are plotting a time series, I would recommend using the pandas
built in plotting functions, especially as you already have the data as a DataFrame
. 由于您正在绘制时间序列,因此建议您使用内置在绘制函数中的
pandas
,尤其是当您已经将数据作为DataFrame
。
To keep the datetime format on the xaxis, you just need to tell the .plot()
function which columns to use. 要在xaxis上保留日期时间格式,只需告诉
.plot()
函数要使用哪些列。 For example: 例如:
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('bpl_energy_2018.csv')
data.sort_values('datetime_measured', inplace=True)
data.plot('datetime_measured', 'total_demand_kw')
# Rotate and align xtick labels
ax.get_figure().autofmt_xdate()
# make room for tick labels
plt.tight_layout()
plt.show()
Note that I rotated the ticks with ax.get_figure().autofmt_xdate()
and made space for them using tight_layout()
. 请注意,我使用
ax.get_figure().autofmt_xdate()
旋转了刻度ax.get_figure().autofmt_xdate()
,并使用tight_layout()
为它们tight_layout()
了空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.