【发布时间】:2021-10-20 23:11:09
【问题描述】:
我正在寻找解决方案:如何为 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()
现在的输出显示第三个子图中缺少细节:
【问题讨论】:
-
ax3.set_yticks([...])?还是from matplotlib.ticker import MultipleLocator和ax3.yaxis.set_major_locator(MultipleLocator(2))?请注意,plt.yticks([...])仅更改“当前绘图”,默认为最后一个创建的绘图。 -
您好,感谢您的反馈。我需要测试您的建议:
from matplotlib.ticker import MultipleLocator with ax3.yaxis.set_major_locator(MultipleLocator(2))。就我的查询而言,这似乎有点矫枉过正,但无论如何我明天都会检查一下,看看我什么时候可以使用这种方法。
标签: python matplotlib plot seaborn