簡體   English   中英

修改 x 軸下方凌亂和重疊日期標簽的最優雅方法? (Seaborn,條形圖)

[英]The most elegant way to modify messy and overlapping date labels below x axis? (Seaborn, barplot)

df (Pandas DataFrame) 有兩列:Date (as datetime64) 和 Amount (as float)。

我使用條形圖根據時間繪制 Amount 列中的值:

sns.barplot(x="Date", y="Amount", data=df)
plt.show()

然而,日期標簽一團糟(見圖)。 在 Pandas 中處理這個問題的優雅方式是什么? 我正在考慮從標簽中刪除月份和年份,或者將標簽旋轉 90 度。 這些將如何完成,或者有更好的選擇嗎? 謝謝你。

在 x 軸上重疊日期標簽

我會同時做:旋轉你的 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()

在此處輸入圖片說明

這會自動調整 SNS 繪圖的日期 x 軸,因此在大多數情況下您不必手動執行此操作: sns_plot.get_figure().autofmt_xdate()

另一種解決方案,如果您有大量日期並且希望以更稀疏的間隔標記它們;

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

# dummy data:
df = pd.DataFrame({'Date':pd.to_datetime(['1999-12-12', '2000-12-12', '2001-12-12',
                                          '2002-12-12', '2003-12-12', '2004-12-12',
                                          '2005-12-12','2006-12-12', '2007-12-12', '2008-12-12']),
                                          'Amount':[1,2,3,4,5,6,7,8,9,10]})

fig, ax = plt.subplots()
sns.barplot(x="Date", y="Amount", data=df, ax=ax)

# set the frequency for labelling the xaxis
freq = int(2)

# set the xlabels as the datetime data for the given labelling frequency,
# also use only the date for the label
ax.set_xticklabels(df.iloc[::freq].Date.dt.date)
# set the xticks at the same frequency as the xlabels
xtix = ax.get_xticks()
ax.set_xticks(xtix[::freq])
# nicer label format for dates
fig.autofmt_xdate()

plt.tight_layout()
plt.show()

點擊查看劇情

還值得考慮使用 seaborn 繪圖默認值,並將日期放在 yaxis 上以方便閱讀,但這更多是個人喜好。

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

# set the seaborn asthetics
sns.set()

# dummy data:
df = pd.DataFrame({'Date':pd.to_datetime(['1999-12-12', '2000-12-12', '2001-12-12',
                                          '2002-12-12', '2003-12-12', '2004-12-12',
                                          '2005-12-12','2006-12-12', '2007-12-12', '2008-12-12']),
                                          'Amount':[1,2,3,4,5,6,7,8,9,10]})

fig, ax = plt.subplots()
# plot with a horizontal orientation
sns.barplot(y="Date", x="Amount", data=df, ax=ax, orient='h')

# set the frequency for labelling the yaxis
freq = int(2)

# set the ylabels as the datetime data for the given labelling frequency,
# also use only the date for the label
ax.set_yticklabels(df.iloc[::freq].Date.dt.date)
# set the yticks at the same frequency as the ylabels
ytix = ax.get_yticks()
ax.set_yticks(ytix[::freq])

plt.tight_layout()
plt.show()

點擊查看更好的情節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM