KMF*_*MFR 5 python label bar-chart pandas seaborn
df (Pandas DataFrame) 有两列:Date (as datetime64) 和 Amount (as float)。
我使用条形图根据时间绘制 Amount 列中的值:
sns.barplot(x="Date", y="Amount", data=df)
plt.show()
Run Code Online (Sandbox Code Playgroud)
然而,日期标签一团糟(见图)。在 Pandas 中处理这个问题的优雅方式是什么?我正在考虑从标签中删除月份和年份,或者将标签旋转 90 度。这些将如何完成,或者有更好的选择吗?谢谢你。
这会自动调整 SNS 图的日期 x 轴,因此在大多数情况下您无需手动执行此操作:
sns_plot.get_figure().autofmt_xdate()
我会同时做这两件事:旋转你的 xlabels 并仅使用日期:
import seaborn as sns
import matplotlib.pyplot as plt
# dummy data:
df = pd.DataFrame({'Date':pd.to_datetime(['1999-12-12', '2000-12-12', '2001-12-12']),'Amount':[1,2,3]})
sns.barplot(x="Date", y="Amount", data=df)
# use the original locations of your xticks, and only the date for your label
# rotate the labels 90 degrees using the rotation argument
plt.xticks(plt.xticks()[0], df.Date.dt.date, rotation=90)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)