使用 pandas.DataFrame.plot 方法时的 Timeserie datetick 问题
Timeserie datetick problems when using pandas.DataFrame.plot method
我刚刚在使用 pandas.DataFrame
的 plot
方法时发现了一些非常奇怪的事情。我正在使用 pandas 0.19.1
。这是我的 MWE:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
t = pd.date_range('1990-01-01', '1990-01-08', freq='1H')
x = pd.DataFrame(np.random.rand(len(t)), index=t)
fig, axe = plt.subplots()
x.plot(ax=axe)
plt.show(axe)
xt = axe.get_xticks()
当我尝试格式化我的 xticklabels 时,我得到了奇怪的行为,然后我检查了对象以了解并发现了以下内容:
t[-1] - t[0] = Timedelta('7 days 00:00:00')
,确认 DateTimeIndex
是我所期望的;
xt = [175320, 175488]
、xticks
是整数,但它们不等于自纪元以来的天数(我不知道它是什么);
xt[-1] - xt[0] = 168
有更多类似的索引,有相同的数量 len(x) = 169
.
这解释了为什么我无法成功格式化我的斧头使用:
axe.xaxis.set_major_locator(mdates.HourLocator(byhour=(0,6,12,18)))
axe.xaxis.set_major_formatter(mdates.DateFormatter("%a %H:%M"))
第一个引发一个错误,有很多报价生成
第二个显示我的第一个刻度是 Fri 00:00
但它应该是 Mon 00:00
(事实上 matplotlib
假设第一个刻度是 0481-01-03 00:00
,哎呀这是我的错误所在) .
看起来 pandas
和 matplotlib
整数到日期的转换 之间存在一些不兼容性,但我找不到解决此问题的方法。
如果我 运行 而不是:
fig, axe = plt.subplots()
axe.plot(x)
axe.xaxis.set_major_formatter(mdates.DateFormatter("%a %H:%M"))
plt.show(axe)
xt = axe.get_xticks()
一切都按预期工作,但我错过了 pandas.DataFrame.plot
方法的所有很酷的功能,例如曲线标签等。这里 xt = [726468. 726475.]
。
如何使用 pandas.DataFrame.plot
方法而不是 axe.plot
正确格式化我的报价并避免这个问题?
更新
问题似乎与日期表示的基础数字的来源和比例(单位)有关。无论如何,我无法控制它,即使将它强制为正确的类型也是如此:
t = pd.date_range('1990-01-01', '1990-01-08', freq='1H', origin='unix', units='D')
matplotlib 和 pandas 表示之间存在差异。而且我找不到关于这个问题的任何文档。
这就是你想要的吗?请注意,我缩短了 date_range 以便更容易看到标签。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
t = pd.date_range('1990-01-01', '1990-01-04', freq='1H')
x = pd.DataFrame(np.random.rand(len(t)), index=t)
# resample the df to get the index at 6-hour intervals
l = x.resample('6H').first().index
# set the ticks when you plot. this appears to position them, but not set the label
ax = x.plot(xticks=l)
# set the display value of the tick labels
ax.set_xticklabels(l.strftime("%a %H:%M"))
# hide the labels from the initial pandas plot
ax.set_xticklabels([], minor=True)
# make pretty
ax.get_figure().autofmt_xdate()
plt.show()
我刚刚在使用 pandas.DataFrame
的 plot
方法时发现了一些非常奇怪的事情。我正在使用 pandas 0.19.1
。这是我的 MWE:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
t = pd.date_range('1990-01-01', '1990-01-08', freq='1H')
x = pd.DataFrame(np.random.rand(len(t)), index=t)
fig, axe = plt.subplots()
x.plot(ax=axe)
plt.show(axe)
xt = axe.get_xticks()
当我尝试格式化我的 xticklabels 时,我得到了奇怪的行为,然后我检查了对象以了解并发现了以下内容:
t[-1] - t[0] = Timedelta('7 days 00:00:00')
,确认DateTimeIndex
是我所期望的;xt = [175320, 175488]
、xticks
是整数,但它们不等于自纪元以来的天数(我不知道它是什么);xt[-1] - xt[0] = 168
有更多类似的索引,有相同的数量len(x) = 169
.
这解释了为什么我无法成功格式化我的斧头使用:
axe.xaxis.set_major_locator(mdates.HourLocator(byhour=(0,6,12,18)))
axe.xaxis.set_major_formatter(mdates.DateFormatter("%a %H:%M"))
第一个引发一个错误,有很多报价生成
第二个显示我的第一个刻度是 Fri 00:00
但它应该是 Mon 00:00
(事实上 matplotlib
假设第一个刻度是 0481-01-03 00:00
,哎呀这是我的错误所在) .
看起来 pandas
和 matplotlib
整数到日期的转换 之间存在一些不兼容性,但我找不到解决此问题的方法。
如果我 运行 而不是:
fig, axe = plt.subplots()
axe.plot(x)
axe.xaxis.set_major_formatter(mdates.DateFormatter("%a %H:%M"))
plt.show(axe)
xt = axe.get_xticks()
一切都按预期工作,但我错过了 pandas.DataFrame.plot
方法的所有很酷的功能,例如曲线标签等。这里 xt = [726468. 726475.]
。
如何使用 pandas.DataFrame.plot
方法而不是 axe.plot
正确格式化我的报价并避免这个问题?
更新
问题似乎与日期表示的基础数字的来源和比例(单位)有关。无论如何,我无法控制它,即使将它强制为正确的类型也是如此:
t = pd.date_range('1990-01-01', '1990-01-08', freq='1H', origin='unix', units='D')
matplotlib 和 pandas 表示之间存在差异。而且我找不到关于这个问题的任何文档。
这就是你想要的吗?请注意,我缩短了 date_range 以便更容易看到标签。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
t = pd.date_range('1990-01-01', '1990-01-04', freq='1H')
x = pd.DataFrame(np.random.rand(len(t)), index=t)
# resample the df to get the index at 6-hour intervals
l = x.resample('6H').first().index
# set the ticks when you plot. this appears to position them, but not set the label
ax = x.plot(xticks=l)
# set the display value of the tick labels
ax.set_xticklabels(l.strftime("%a %H:%M"))
# hide the labels from the initial pandas plot
ax.set_xticklabels([], minor=True)
# make pretty
ax.get_figure().autofmt_xdate()
plt.show()