【发布时间】:2018-12-08 21:06:53
【问题描述】:
这看起来很简单,但我这辈子都想不通。
我是 Python 和 Seaborn 的新手,我在 PythonAnywhere 在线完成所有这些工作。
我要做的就是在 seaborn 中创建一个简单的条形图,日期在 x 轴上正确排序(即从左到右升序)。
当我尝试这个时:
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()
我明白了:
Nice looking bar graph but with the dates ordered descending from left to right
然后当我尝试将对象转换为日期时间时:
(注意:我在下面使用 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()
我明白了:
Bar plot with the dates in the right order, but WRONG format
我得到相同的条形图,日期排序正确,但采用完整的长日期时间格式,时间等。但我想要的只是日/月/年。
我已经搜索 stackoverflow 两天了,但没有任何效果。我开始怀疑部分原因是否是因为我正在研究 PythonAnywhere。但我也找不到任何原因。
这让我发疯了。期待任何帮助。谢谢。
【问题讨论】:
-
@Parfait,感谢您的提示。这是我第一次提出问题,很高兴收到这样的提示。非常感激。我希望我的编辑现在使我的问题可行。
标签: python pandas matplotlib seaborn pythonanywhere