我正在寻找一个解决方案:如何为Seaborn的三个子图中的一个设置记号?
我知道我可以为所有子图设置xticks和/或yticks
#specify positions of ticks on x-axis and y-axis
plt.xticks([15, 20, 25])
plt.yticks([4, 8, 12])...but,我不知道如何才能只针对三个子情节中的一个来做。
我想为本例代码中的第三个子图(我指的是ax3)设置不同的yticks:
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()输出显示现在第三个子图中缺少详细信息:

发布于 2021-10-21 01:53:29
我从#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()

发布于 2021-10-20 23:58:48
这应该是可行的:
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://stackoverflow.com/questions/69654009
复制相似问题