繁体   English   中英

使用带有matplotlib的mdates将x轴设置为日期

[英]Setting x-axis as dates using mdates with matplotlib

我正在加载这些包:

import pandas as pd
from matplotlib import pyplot as plt
import numpy
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib
import matplotlib.dates as mdates
sns.set()
%matplotlib inline

我有一个看起来像这样的数据框df

df['element_date'] = pd.to_datetime(df['element_date'])
df['mdate'] = [mdates.date2num(d) for d in df['element_date']]
df.head()

id            Tier    element     element_date           mdate
5228039     Tier B      4      2018-05-28 10:59:00  736842.457639
5232263     Tier B      3      2018-05-28 10:59:00  736842.457639
5245478     Tier B      EA     2018-05-27 13:58:00  736841.581944
4975552     Tier B      2      2018-05-30 21:01:00  736844.875694
4975563     Tier A      2      2018-05-30 21:01:00  736844.875694

我试图将计数图的x轴设置为仅月和日,并且收到一条错误消息。 这是我正在运行的代码(为了节省空间,我删除了命名标签):

fig, ax = plt.subplots(figsize=(15,10))
fig = sns.countplot(x="mdate", hue="element", data=df)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
plt.show(fig)

我正在获取DateFormatter found a value of x=0, which is an illegal date. This usually occurs because you have not informed the axis that it is plotting dates, eg, with ax.xaxis_date() DateFormatter found a value of x=0, which is an illegal date. This usually occurs because you have not informed the axis that it is plotting dates, eg, with ax.xaxis_date()

现在,我当然尝试将ax.xaxis_date()添加到无效。 我也没有x的值等于0。我删除了NA,并将值计数为mdate,没有找到0。

我在这里查看了许多不同的答案,似乎无法找到解决方案。 我已经尝试过使用element_date作为我的日期时间值,以及尝试通过mdate使用“ mathplotlib”日期。

任何想法将不胜感激。 本质上,我只是想让我的x轴成为两个月内有序的日期序列,并为每个日期计算元素。

谢谢!

埋在GitHub熊猫问题页面上,用户@pawaller发现了使用plt.FixedFormatter解决方法 ,您在其中对日期时间数据帧列进行了字符串格式化。

ax.xaxis.set_major_formatter(plt.FixedFormatter(df['element_date'].dt.strftime("%m-%d")))

但是,由于值标签混乱且对齐不正确,因此不能立即使用上述方法。 因此,需要unique()sort_values()

x_dates = df['element_date'].dt.strftime('%m-%d').sort_values().unique()
ax.xaxis.set_major_formatter(plt.FixedFormatter(x_dates))

在下面进行演示(从不使用mdate列):

数据

from io import StringIO
...

txt = '''id            Tier    element     element_date           mdate
5228039     "Tier B"      4      "2018-05-28 10:59:00"  736842.457639
5232263     "Tier B"      3      "2018-05-28 10:59:00"  736842.457639
5245478     "Tier B"      EA     "2018-05-27 13:58:00"  736841.581944
4975552     "Tier B"      2      "2018-05-30 21:01:00"  736844.875694
4975563     "Tier A"      2      "2018-05-30 21:01:00"  736844.875694'''

df = pd.read_table(StringIO(txt), sep="\s+", parse_dates=[3])

情节

fig, ax = plt.subplots(figsize=(13,4))

fig = sns.countplot(x="element_date", hue="element", data=df, ax=ax)

x_dates = df['element_date'].dt.strftime('%m-%d').sort_values().unique()
ax.xaxis.set_major_formatter(plt.FixedFormatter(x_dates))

plt.legend(loc='upper left')
plt.show()
plt.close()

绘图输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM