[英]Change the tick labels of a seaborn barplot
我正在嘗試創建一個帶有三個共享 x 軸的堆疊圖形的seaborn
條形圖,我希望 x 軸刻度標簽是產品名稱,旋轉 90 度,以便它們清晰易讀。
我已經設法使用以下代碼通過單個圖形圖來做到這一點:
ax = plt.subplots(1, 1, figsize=(15, 7), sharex=True)
#products_bar
products_bar = sns.barplot(x=products_all.index, y=products_all['Unique Purchases'], palette="tab10")
products_bar.set_xticklabels(products_all.index)
for item in products_bar.get_xticklabels():
item.set_rotation(90)
但是,當我嘗試使用三個數字(將相同的數據拆分為三個過濾器)重復此操作時,標簽仍然頑固地保留為數據幀中的數字。 這是代碼 -
f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(15, 10), sharex=True)
products_bar.set_xticklabels(organic.index)
for item in products_bar.get_xticklabels():
item.set_rotation(45)
products_bar = sns.barplot(x=organic.index, y=organic['Unique Purchases'], palette="tab10", ax=ax1)
products_bar = sns.barplot(x=paid.index, y=paid['Unique Purchases'], palette="tab10", ax=ax2)
products_bar = sns.barplot(x=social.index, y=social['Unique Purchases'], palette="tab10", ax=ax3)
我不完全確定要搜索哪些關鍵字才能找到答案。
由於您使用了sharex=True
,您只需更改最后一個圖中的軸刻度:
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
idx = ["day"+str(i) for i in range(5)]
organic = pd.DataFrame({"Unique Purchases":np.random.randint(1,10,5)},index=idx)
paid = pd.DataFrame({"Unique Purchases":np.random.randint(1,10,5)},index=idx)
social = pd.DataFrame({"Unique Purchases":np.random.randint(1,10,5)},index=idx)
f, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(15, 10), sharex=True)
ax1 = sns.barplot(x=organic.index, y=organic['Unique Purchases'], palette="tab10", ax=ax1)
ax2 = sns.barplot(x=paid.index, y=paid['Unique Purchases'], palette="tab10", ax=ax2)
ax3 = sns.barplot(x=social.index, y=social['Unique Purchases'], palette="tab10", ax=ax3)
ax3.tick_params(labelrotation=45)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.