简体   繁体   English

Plot pandas dataframe 带有定义的刻度(转换为日期时间)

[英]Plot pandas dataframe with defined ticks (conversion to datetime)

I want to plot a pandas dateframe with defined ticks.我想 plot 定义刻度的 pandas 日期框。

在此处输入图像描述

As it can be seen in the plot the time distance between ticks is 8 minutes and 20 seconds.从 plot 中可以看出,滴答之间的时间距离为 8 分 20 秒。 I would like to have this in 5 Minute steps.我想在 5 分钟内完成。

The data is a dataframe with the following structure:数据是一个dataframe,结构如下:

                           Data1      Data2       
timestamps                                                                                                                       
0 days 00:00:00         36.59338  36.465206  
0 days 00:00:00.100000  36.59338  36.465206  
0 days 00:00:00.200000  36.59338  36.465206  
0 days 00:00:00.300000  36.59338  36.465206  
0 days 00:00:00.400000  36.59338  36.465206  

How can I control the step size of the ticks in the example?如何控制示例中刻度的步长?

The code:编码:

df.plot(color=colors); 

Leads to the above Picture.导致上图。

df.plot() fortunately returns a matplotlib.AxesSubplot so you can change paramaters as you would usually do. df.plot()幸运地返回了一个matplotlib.AxesSubplot所以你可以像往常一样更改参数。 Also, to properly define datetime xticks, you should use the matplotlib.dates package.此外,要正确定义日期时间 xticks,您应该使用matplotlib.dates package。

import matplotlib.pyplot as plt
import matplotlib.dates as md
ax = df.plot(color=colors)
min_time = df.index.minute.min
max_time = df.index.minute.max
time_int = 5
xlocator = md.MinuteLocator(byminute=np.arange(min_time, max_time+1, time_int), interval = 1)
ax.xaxis.set_major_locator(xlocator)

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

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