简体   繁体   English

如何在PyPlot中plot.show()日期时间格式的数据“ mm-dd-yy hh:mm:ss”?

[英]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: 情节看起来像这样:

data_plotted_through_2018

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.

相关问题 将 Python 中的 TimeFormat 'DD/MM/YY HH:MM AM' 从字符串转换为 Datetime 'DD/MM/YY HH:MM:SS' - Converting TimeFormat 'DD/MM/YY HH:MM AM' from String in Python to Datetime 'DD/MM/YY HH:MM:SS' 如何将 csv 文件中的时间格式从 DD:MM:YY HH:MM 更改为 YYYY-MM-DD HH:MM:SS。 或 YYYY/MM/DD HH:MM:SS - How to change time format in a csv file from DD:MM:YY HH:MM to YYYY-MM-DD HH:MM:SS. or YYYY/MM/DD HH:MM:SS PYTHON 将 mm/dd/yy hh:mm:ss AM 转换为 mm/dd/yy - PYTHON convert mm/dd/yy hh:mm:ss AM TO mm/dd/yy Python YYYY-MM-DD hh:mm:ss.fff-zz:xx 格式化日期时间问题 - Python YYYY-MM-DD hh:mm:ss.fff-zz:xx formatted datetime issue 如何将日期数组(格式为“ mm / dd / yy HH:MM:SS”)转换为数字? - How to convert an array of dates (format 'mm/dd/yy HH:MM:SS') to numerics? Python/Pandas 日期时间转换格式为 DD-MON-YY HH:MM:SS.NS PM - Python/Pandas Datetime Conversion of Date in Format DD-MON-YY HH:MM:SS.NS PM 如何基于python中的日期(以mm-dd-yy格式)从数据集中选择行? - How to select rows from data set based on the date(form in mm-dd-yy) in python? 在YYYY-MM-DD中转换日期时间HH:MM [:SS [.SSSSSS]] - Transform datetime in YYYY-MM-DD HH:MM[:SS[.SSSSSS]] Python 日期时间转换格式为 dd/mm/yyyy HH:MM:SS - Python Datetime convert format into dd/mm/yyyy HH:MM:SS 散景日期时间轴应显示所有 yyyy、mm、dd、hh、mm、ss - Bokeh datetime axis should show all yyyy, mm, dd, hh, mm, ss
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM