我正在尋找解決方案:如何為 Seaborn 中的三個子圖之一設定刻度?
我知道我可以設定xticks和/或yticks所有子圖
#specify positions of ticks on x-axis and y-axis
plt.xticks([15, 20, 25])
plt.yticks([4, 8, 12])
...但我不知道我怎么能只為三個子圖之一做到這一點。
我想yticks在這個示例代碼中為第三個子圖設定不同的(我的意思是ax3):
import seaborn as sns
ts = pd.Series(df_1["total_purchase"]).str.split(expand=True) # split value from `$`
# df_1[["total_purchase"]]
# print(ts[0])
df_1["purchase_float"] = ts[0].astype(float) # We need `float` for plotting
df_1 = df_1.reset_index() # We need index as the first column
sns.set_theme(style="whitegrid")
fig, (ax1, ax2, ax3) = plt.subplots(3,1,figsize=(12,12))
sns.barplot(x='purchase_float', y='employee_name', data=df_1, ax=ax1)
sns.barplot(y='customer_canada', x='employee_name', data=df_1, ax=ax2)
sns.barplot(y='customer_dif_country', x='employee_name', data=df_1, ax=ax3)
plt.subplots_adjust(hspace = 0.8)
plt.show()
輸出顯示現在第三個子圖中缺少細節:

uj5u.com熱心網友回復:
我使用#Elena_Kosourova 的想法找到了我的問題的完整解決方案。
我的代碼:
import seaborn as sns
ts = pd.Series(df_1["total_purchase"]).str.split(expand=True) # split value from `$`
# df_1[["total_purchase"]]
# print(ts[0])
df_1["purchase_float"] = ts[0].astype(float) # We need `float` for plotting
df_1 = df_1.reset_index() # We need index as the first column
sns.set_theme(style="whitegrid")
plt.figure(figsize=(16, 16))
plt.subplot(3, 1, 1)
sns.barplot(x='purchase_float', y='employee_name', data=df_1)
plt.xticks([250, 500, 750, 1000, 1393, 1584, 1731])
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.xlabel("total purchase in USD", fontsize=18)
plt.ylabel("", fontsize=18)
plt.title('employee income', fontsize=30)
plt.subplot(3, 1, 2)
sns.barplot(y='customer_canada', x='employee_name', data=df_1)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.ylabel("canada customers", fontsize=18)
plt.xlabel("", fontsize=18)
plt.title("employee's clients from Canada", fontsize=30)
plt.subplot(3, 1, 3)
sns.barplot(y='customer_dif_country', x='employee_name', data=df_1)
plt.yticks([4, 8, 12, 16, 19])
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.ylabel("foreign customers", fontsize=18)
plt.xlabel("", fontsize=18)
plt.title("the employee's foreign customers", fontsize=30)
plt.subplots_adjust(hspace = 0.8) # space between subplots
plt.show()

uj5u.com熱心網友回復:
這應該有效:
import seaborn as sns
ts = pd.Series(df_1["total_purchase"]).str.split(expand=True) # split value from `$`
# df_1[["total_purchase"]]
# print(ts[0])
df_1["purchase_float"] = ts[0].astype(float) # We need `float` for plotting
df_1 = df_1.reset_index() # We need index as the first column
sns.set_theme(style="whitegrid")
plt.figure(figsize=(16, 16))
plt.subplot(3, 1, 1)
sns.barplot(x='purchase_float', y='employee_name', data=df_1)
plt.subplot(3, 1, 2)
sns.barplot(y='customer_canada', x='employee_name', data=df_1)
plt.subplot(3, 1, 3)
sns.barplot(y='customer_dif_country', x='employee_name', data=df_1)
plt.yticks([4, 8, 12])
plt.subplots_adjust(hspace = 0.8)
plt.show()
您可以以類似的方式調整任何子圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331264.html
標籤:Python matplotlib 阴谋 海生
上一篇:關閉視窗后方法仍在運行
下一篇:如何為每組線添加單個圖例標簽
