在xaxis上显示Pandas Dataframe线图的日期 [英] Pandas Dataframe line plot display date on xaxis

查看:72
本文介绍了在xaxis上显示Pandas Dataframe线图的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比较以下代码:

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()

我最后添加了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

第二张图的问题在于它从5-24开始而不是5-25.另外,2017年的5-25是星期四而不是星期一.是什么原因引起的?这个时区有关系吗? (我也不明白为什么日期数字会彼此叠加)

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)

推荐答案

通常,pandas和matplotlib的日期时间实用程序不兼容.因此,在大多数情况下,尝试在使用熊猫创建的日期轴上使用matplotlib.dates对象将失败.

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.

一个原因是从文档

datetime对象将转换为浮点数,该浮点数表示自0001-01-01 UTC以来的天数( 加1 ).例如,0001-01-01、06:00是1.25,而不是0.25.

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.

但是,这并不是唯一的区别,因此建议在日期时间对象中不要将pandas和matplotlib混合使用.

However, this is not the only difference and it is thus advisable not to mix pandas and matplotlib when it comes to datetime objects.

但是,可以选择告诉熊猫不要使用其自己的日期时间格式.在这种情况下,可以使用matplotlib.dates标记.可以通过以下方式进行引导.

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.

df.plot(x_compat=True)

由于熊猫没有提供日期的复杂格式化功能,因此可以使用matplotlib进行绘图和格式化.

Since pandas does not provide sophisticated formatting capabilities for dates, one can use matplotlib for plotting and formatting.

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()

这篇关于在xaxis上显示Pandas Dataframe线图的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆