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

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

问题描述

比较以下代码:

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

%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 的 datetime 实用程序是不兼容的.因此,在使用 Pandas 创建的日期轴上尝试使用 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 混合用于 datetime 对象.

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

由于 pandas 不提供复杂的日期格式化功能,因此可以使用 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

%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

%a'))
    plt.gca().invert_xaxis()

plt.show()

  • 使用 matplotlib 面向对象的 API 进行更新
usePandas=True
#Either use pandas
if usePandas:
    df = df.set_index('date')
    ax = df.plot(x_compat=True, figsize=(6, 4))
    ax.xaxis.set_major_locator(dates.DayLocator())
    ax.xaxis.set_major_formatter(dates.DateFormatter('%d

%a'))
    ax.invert_xaxis()
    ax.get_figure().autofmt_xdate(rotation=0, ha="center")
    
# or use matplotlib
else:
    fig, ax = plt.subplots(figsize=(6, 4))
    ax.plot('date', 'ratio1', data=df)
    ax.xaxis.set_major_locator(dates.DayLocator())
    ax.xaxis.set_major_formatter(dates.DateFormatter('%d

%a'))
    fig.invert_xaxis()

plt.show()

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

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