Pandas Dataframe 线图在 xaxis 上显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44213781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Dataframe line plot display date on xaxis
提问by Cheng
Compare the following code:
比较以下代码:
test = pd.DataFrame({'date':['20170527','20170526','20170525'],'ratio1':[1,0.98,0.97]})
test['date'] = pd.to_datetime(test['date'])
test = test.set_index('date')
ax = test.plot()
I added DateFormatter
in the end:
我最后补充道DateFormatter
:
test = pd.DataFrame({'date':['20170527','20170526','20170525'],'ratio1':[1,0.98,0.97]})
test['date'] = pd.to_datetime(test['date'])
test = test.set_index('date')
ax = test.plot()
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n\n%a')) ## Added this line
The issue with the second graph is that it starts on 5-24
instead 5-25
. Also, 5-25
of 2017 is Thursday not Monday. What is causing the issue? Is this timezone related? (I don't understand why the date numbers are stacked on top of each other either)
与第二张图的问题是,它开始于5-24
代替5-25
。此外,5-25
2017 年是星期四而不是星期一。是什么导致了这个问题?这个时区有关系吗?(我不明白为什么日期数字也堆叠在一起)
回答by ImportanceOfBeingErnest
In general the datetime utilities of pandas and matplotlib are incompatible. So trying to use a matplotlib.dates
object on a date axis created with pandas will in most cases fail.
通常,pandas 和 matplotlib 的日期时间实用程序是不兼容的。因此,matplotlib.dates
在大多数情况下,尝试在用 Pandas 创建的日期轴上使用对象会失败。
One reason is e.g. seen from the documentation
datetime
objects are converted to floating point numbers which represent time in days since 0001-01-01 UTC, plus 1. For example, 0001-01-01, 06:00 is 1.25, not 0.25.
datetime
对象被转换为浮点数,表示自 0001-01-01 UTC 以来的天数,加上 1。例如,0001-01-01、06:00 是 1.25,而不是 0.25。
However, this is not the only difference and it is thus advisable not to mix pandas and matplotlib when it comes to datetime objects.
然而,这并不是唯一的区别,因此建议不要将 Pandas 和 matplotlib 混用在 datetime 对象上。
There is however the option to tell pandas not to use its own datetime format. In that case using the matplotlib.dates
tickers is possible. This can be steered via.
但是,可以选择告诉 Pandas 不要使用它自己的日期时间格式。在这种情况下,可以使用matplotlib.dates
股票代码。这可以通过。
df.plot(x_compat=True)
Since pandas does not provide sophisticated formatting capabilities for dates, one can use matplotlib for plotting and formatting.
由于 Pandas 不提供复杂的日期格式化功能,因此可以使用 matplotlib 进行绘图和格式化。
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
df = pd.DataFrame({'date':['20170527','20170526','20170525'],'ratio1':[1,0.98,0.97]})
df['date'] = pd.to_datetime(df['date'])
usePandas=True
#Either use pandas
if usePandas:
df = df.set_index('date')
df.plot(x_compat=True)
plt.gca().xaxis.set_major_locator(dates.DayLocator())
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%d\n\n%a'))
plt.gca().invert_xaxis()
plt.gcf().autofmt_xdate(rotation=0, ha="center")
# or use matplotlib
else:
plt.plot(df["date"], df["ratio1"])
plt.gca().xaxis.set_major_locator(dates.DayLocator())
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%d\n\n%a'))
plt.gca().invert_xaxis()
plt.show()