pandas Seaborn 条形图中 X 轴上的日期排序和格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51105648/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:45:58  来源:igfitidea点击:

Ordering and Formatting Dates on X-Axis in Seaborn Bar Plot

pythonpandasmatplotlibseabornpythonanywhere

提问by ngunsch

This seems so simple, but for the life of me I can't figure it out.

这看起来很简单,但对于我的生活,我无法弄清楚。

I am new to Python and Seaborn, and I am doing all this online at PythonAnywhere.

我是 Python 和 Seaborn 的新手,我正在 PythonAnywhere 在线完成所有这些工作。

All I am trying to do is create a simple barplot in seaborn, with dates ordered properly (that is, ascending from left to right), on the x-axis.

我想要做的就是在 seaborn 中创建一个简单的条形图,在 x 轴上正确排列日期(即从左到右升序)。

When I try this:

当我尝试这个时:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import pandas as pd
import seaborn as sns

emp = pd.DataFrame([[32, "5/31/2018"], [3, "2/28/2018"], [40, "11/30/2017"], [50, "8/31/2017"], [51, "5/31/2017"]], 
               columns=["jobs", "12monthsEnding"])

fig = plt.figure(figsize = (10,7))

sns.barplot(x = "12monthsEnding", y = "uniqueClientExits", data = emp, 
estimator = sum, ci = None)

fig.autofmt_xdate()
plt.show()

I get this:

我明白了:

Nice looking bar graph but with the dates ordered descending from left to right

漂亮的条形图,但日期从左到右降序

And then when I try to convert the objects to datetime:

然后当我尝试将对象转换为日期时间时:

(note: i'm using pd.to_datetime() below in order to try and recreate what happens when I use parse_dates in pd.read_csv(), which is how I'm actually creating the dataframe.)

(注意:我在下面使用 pd.to_datetime() 是为了尝试重新创建当我在 pd.read_csv() 中使用 parse_dates 时会发生什么,这就是我实际创建数据帧的方式。)

emp = pd.DataFrame([[32, pd.to_datetime("5/31/2018")], [3, pd.to_datetime("2/28/2018")], [40, pd.to_datetime("11/30/2017")], [50, pd.to_datetime("8/31/2017")], [51, pd.to_datetime("5/31/2017")]], 
               columns=["jobs", "12monthsEnding"])

fig = plt.figure(figsize = (10,7))

sns.barplot(x = "12monthsEnding", y = "uniqueClientExits", data = emp, 
estimator = sum, ci = None)

fig.autofmt_xdate()

plt.show()

I get this:

我明白了:

Bar plot with the dates in the right order, but WRONG format

日期顺序正确但格式错误的条形图

I get the same bar plot, with the dates ordered properly, but in the full, long datetime format, with the time, etc. But all I want is the day/month/year.

我得到了相同的条形图,日期顺序正确,但采用完整、长日期时间格式、时间等。但我想要的只是日/月/年。

I've scoured stackoverflow for two days now and nothing has worked. I'm starting to wonder if part of the reason is because I'm working on PythonAnywhere. But I also can't find any reason why that would be.

我已经搜索 stackoverflow 两天了,但没有任何效果。我开始怀疑部分原因是否是因为我在 PythonAnywhere 上工作。但我也找不到任何原因。

This is driving me nuts. Looking forward to any assistance. Thanks.

这让我发疯。期待任何帮助。谢谢。

回答by Parfait

Using your second approach, simply sort and reformat the datetime values to YYYY-MM-DDand pass values into set_xticklabels. Below demonstrates with random, seeded data:

使用第二种方法,只需对日期时间值进行排序和重新格式化,YYYY-MM-DD然后将值传递到set_xticklabels. 下面用随机种子数据演示:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

# RANDOM DATA
np.random.seed(62918)
emp = pd.DataFrame({'uniqueClientExits': [np.random.randint(15) for _ in range(50)],
                    '12monthsEnding': pd.to_datetime(
                                          np.random.choice(
                                              pd.date_range('2018-01-01', periods=50), 
                                          50)
                                      )
                   }, columns = ['uniqueClientExits','12monthsEnding'])

# PLOTTING
fig, ax = plt.subplots(figsize = (12,6))    
fig = sns.barplot(x = "12monthsEnding", y = "uniqueClientExits", data = emp, 
                  estimator = sum, ci = None, ax=ax)

x_dates = emp['12monthsEnding'].dt.strftime('%Y-%m-%d').sort_values().unique()
ax.set_xticklabels(labels=x_dates, rotation=45, ha='right')

Plot Output

绘图输出

To check graph output, run a groupby().sum():

要检查图形输出,请运行groupby().sum()

print(emp.groupby('12monthsEnding').sum().head())

#                 uniqueClientExits
# 12monthsEnding                   
# 2018-01-01                     12
# 2018-01-02                      4
# 2018-01-04                     11
# 2018-01-06                     13
# 2018-01-08                     10
# 2018-01-11                     11
# 2018-01-14                      9
# 2018-01-15                      0
# 2018-01-16                      4
# 2018-01-17                      5
# ...