简体   繁体   English

如何绘制时间序列,其中x轴是matplotlib中的datetime.time对象?

[英]how to plot time series where x-axis is datetime.time object in matplotlib?

I'm trying to graph timeseries data beginning from 9pm to 6pm the next day. 我正在尝试绘制第二天晚上9点到下午6点的时间序列数据。 Here is my failed attempt. 这是我失败的尝试。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import time    

a=np.array([35,25,24,25,27,28,30,35])
df=pd.DataFrame(index=pd.date_range("00:00", "23:00", freq="3H").time,data={'column1':a})

          column1
00:00:00       35
03:00:00       25
06:00:00       24
09:00:00       25
12:00:00       27
15:00:00       28
18:00:00       30
21:00:00       35

reindexing data to go from 21:00 to 18:00. 重新索引数据从21:00到18:00。 perhaps there is a better way of achieving this part too, but this works. 也许有更好的方法来实现这一部分,但这是有效的。

df=df.reindex(np.concatenate([df.loc[time(21,00):].index,df.loc[:time(21,00)].index[:-1]]))

          column1
21:00:00       35
00:00:00       35
03:00:00       25
06:00:00       24
09:00:00       25
12:00:00       27
15:00:00       28
18:00:00       30

plt.plot(df.index,df['column1'])

the x-axis does not seem to match df.index. x轴似乎与df.index不匹配。 also the axis begins at 00:00 not 21:00. 轴也从00:00开始,而不是21:00。 does anyone know a solution that doesn't involve using string labels for the x-axis? 有没有人知道一个不涉及使用x轴的字符串标签的解决方案?

A simple way to do that is to plot the data without expliciting the x axis and the to change the labels. 一种简单的方法是在不明确x轴的情况下绘制数据并更改标签。 The problem is that this will only works if the time between the data is constant. 问题是这只有在数据之间的时间不变时才有效。 I know you said you didn't want to use string labels, so I don't know of this solution will be what you want. 我知道你说你不想使用字符串标签,所以我不知道这个解决方案会是你想要的。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import time    

a=np.array([35,25,24,25,27,28,30,35])

df=pd.DataFrame(index=pd.date_range("00:00", "23:00",freq="3H").time,data={'column1':a})

df=df.reindex(np.concatenate([df.loc[time(21,00):].index,df.loc[:time(21,00)].index[:-1]]))

# Now we create the figure and the axes
fig,axes=plt.subplots()
axes.plot(df['column1'])  # In the x axis will appear 0,1,2,3...
axes.set_xticklabels(df.index)  # now we change the labels of the xaxis

plt.show()

This should do the trick and will plot what you want. 这应该是诀窍,并将绘制你想要的。

结果的例子

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

相关问题 不能 Plot Time alone as x-axis >> TypeError: float() argument must be a string or a number, not 'datetime.time' - Cannot Plot Time alone as x-axis >> TypeError: float() argument must be a string or a number, not 'datetime.time' 如何在matplotlib中绘制datetime.time? - How plot datetime.time in matplotlib? Plotly 在 x 轴上带有 datetime.time() 和缺失值 - Plotly with datetime.time() in the x-axis and missing values 如何在 matplotlib 中绘制 200 行(时间序列)并使其在 x 轴上可读? - How can plot 200 rows (time series) in matplotlib and make it readable in the x-axis? 更改 matplotlib 中 datetime.time 轴的格式 - Changing the formatting of a datetime.time axis in matplotlib 如何在 Python 中将分钟时间设置为 Matplotlib plot 的 x 轴? - How to set minutes time as x-axis of a Matplotlib plot in Python? 如何 plot 数据,x 轴上的时间不是日期时间 - How to plot data, time on x-axis not datetime 绘制datetime.time python / matplotlib的直方图 - plot histogram of datetime.time python / matplotlib 垂直对齐时间序列(绘图和条形图)在 matplotlib 中共享相同的 x 轴 - Vertically align time series (plot and barplot) sharing same x-axis in matplotlib 使用matplotlib按时间序列自定义日期范围(x轴) - Custom date range (x-axis) in time series with matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM