matplotlib/pandas中日期的内部表示不一致 [英] Inconsistent internal representation of dates in matplotlib/pandas
问题描述
将pandas导入为pdindex = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-02'])数据= pd.DataFrame({'a':[1,2,3],'b':[4、5、6]},索引=索引)ax = data.plot()打印(ax.get_xlim())#出:(736066.7,736469.3)
现在,如果我们更改最后日期.
index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-01'])数据= pd.DataFrame({'a':[1,2,3],'b':[4、5、6]},索引=索引)斧= data.plot()打印(ax.get_xlim())# 输出:(184.8, 189.2)
第一个示例似乎与
并将其替换为标准的勾号
另一方面,这将允许使用非常可定制的 matplotlib.dates
代码和格式化程序.例如获取季度刻度/标签
将matplotlib.pyplot导入为plt导入 matplotlib.dates 作为 mdates导入 matplotlib.ticker 作为 mticker将熊猫作为pd导入index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-01'])数据= pd.DataFrame({'a':[1,2,3],'b':[4、5、6]},索引=索引)ax = data.plot(x_compat=True)# 季度报价ax.xaxis.set_major_locator(mdates.MonthLocator((1,4,7,10)))#格式:def func(x,pos):q = (mdates.num2date(x).month-1)//3+1tx = "Q{}".format(q)如果 q == 1:tx + ="\ n {}".format(mdates.num2date(x).year)返回TXax.xaxis.set_major_formatter(mticker.FuncFormatter(func))plt.setp(ax.get_xticklabels(),旋转= 0,ha ="center")plt.show()
import pandas as pd
index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-02'])
data = pd.DataFrame({'a': [1, 2, 3],
'b': [4, 5, 6]}, index=index)
ax = data.plot()
print(ax.get_xlim())
# Out: (736066.7, 736469.3)
Now, if we change the last date.
index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-01'])
data = pd.DataFrame({'a': [1, 2, 3],
'b': [4, 5, 6]}, index=index)
ax = data.plot()
print(ax.get_xlim())
# Out: (184.8, 189.2)
The first example seems consistent with the matplotlib docs:
Matplotlib represents dates using floating point numbers specifying the number of days since 0001-01-01 UTC, plus 1
Why does the second example return something seemingly completely different? I'm using pandas version 0.22.0 and matplotlib version 2.2.2.
Pandas uses different units to represents dates and times on the axes, depending on the range of dates/times in use. This means that different locators are in use.
In the first case,
print(ax.xaxis.get_major_locator())
# Out: pandas.plotting._converter.PandasAutoDateLocator
in the second case
print(ax.xaxis.get_major_locator())
# pandas.plotting._converter.TimeSeries_DateLocator
You may force pandas to always use the PandasAutoDateLocator
using the x_compat
argument,
df.plot(x_compat=True)
This would ensure to always get the same datetime definition, consistent with the matplotlib.dates
convention.
The drawback is that this removes the nice quarterly ticking
and replaces it with the standard ticking
On the other hand it would then allow to use the very customizable matplotlib.dates
tickers and formatters. For example to get quarterly ticks/labels
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
import pandas as pd
index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-01'])
data = pd.DataFrame({'a': [1, 2, 3],
'b': [4, 5, 6]}, index=index)
ax = data.plot(x_compat=True)
# Quarterly ticks
ax.xaxis.set_major_locator(mdates.MonthLocator((1,4,7,10)))
# Formatting:
def func(x,pos):
q = (mdates.num2date(x).month-1)//3+1
tx = "Q{}".format(q)
if q == 1:
tx += "\n{}".format(mdates.num2date(x).year)
return tx
ax.xaxis.set_major_formatter(mticker.FuncFormatter(func))
plt.setp(ax.get_xticklabels(), rotation=0, ha="center")
plt.show()
这篇关于matplotlib/pandas中日期的内部表示不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!