简体   繁体   English

使用 matplotlib 绘制时间序列 Pandas 数据框时标签错误

[英]Wrong labels when plotting a time series pandas dataframe with matplotlib

I am working with a dataframe containing data of 1 week.我正在使用一个包含 1 周数据的数据框。

                          y
ds                             
2017-08-31 10:15:00    1.000000
2017-08-31 10:20:00    1.049107
2017-08-31 10:25:00    1.098214
...
2017-09-07 10:05:00   99.901786
2017-09-07 10:10:00   99.950893
2017-09-07 10:15:00  100.000000

I create a new index by combining the weekday and time ie我通过结合工作日和时间来创建一个新索引,即

                y
dayIndex             
4 - 10:15    1.000000
4 - 10:20    1.049107
4 - 10:25    1.098214
...
4 - 10:05   99.901786
4 - 10:10   99.950893
4 - 10:15  100.000000

The plot of this data is the following:该数据的图如下: 每周数据 The plot is correct as the labels reflect the data in the dataframe.该图是正确的,因为标签反映了数据框中的数据。 However, when zooming in, the labels do not seem correct as they no longer correspond to their original values:但是,放大时,标签似乎不正确,因为它们不再对应于它们的原始值: 缩放时标签错误 What is causing this behavior?是什么导致了这种行为?

Here is the code to reproduce this:这是重现此内容的代码:

import datetime
import numpy as np
import pandas as pd

dtnow = datetime.datetime.now()
dindex = pd.date_range(dtnow , dtnow  + datetime.timedelta(7), freq='5T')
data = np.linspace(1,100, num=len(dindex))
df = pd.DataFrame({'ds': dindex, 'y': data})
df = df.set_index('ds')
df = df.resample('5T').mean()
df['dayIndex'] = df.index.strftime('%w - %H:%M')
df= df.set_index('dayIndex')
df.plot()

"What is causing this behavior?" “是什么导致了这种行为?”

The formatter of an axes of a pandas dates plot is a matplotlib.ticker.FixedFormatter (see eg print plt.gca().xaxis.get_major_formatter() ). pandas 日期图的轴的格式化程序是matplotlib.ticker.FixedFormatter (参见例如print plt.gca().xaxis.get_major_formatter() )。 "Fixed" means that it formats the i th tick (if shown) with some constant string. “固定”意味着它用一些常量字符串格式化第i个刻度(如果显示)。

When zooming or panning, you shift the tick locations, but not the format strings.缩放或平移时,您会移动刻度位置,但不会移动格式字符串。
In short: A pandas date plot may not be the best choice for interactive plots.简而言之:熊猫日期图可能不是交互式图的最佳选择。

Solution解决方案

A solution is usually to use matplotlib formatters directly.解决方案通常是直接使用 matplotlib 格式化程序。 This requires the dates to be datetime objects (which can be ensured using df.index.to_pydatetime() ).这要求日期是datetime对象(可以使用df.index.to_pydatetime()确保)。

import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates

dtnow = datetime.datetime.now()
dindex = pd.date_range(dtnow , dtnow  + datetime.timedelta(7), freq='110T')
data = np.linspace(1,100, num=len(dindex))
df = pd.DataFrame({'ds': dindex, 'y': data})
df = df.set_index('ds')
df.index.to_pydatetime()
df.plot(marker="o")


plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%w - %H:%M'))
plt.show()

在此处输入图片说明

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

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