【问题标题】:How to set ticks for one of the three subplots in Seaborn?如何为 Seaborn 中的三个子图之一设置刻度?
【发布时间】: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 MultipleLocatorax3.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


【解决方案1】:

我使用#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()

【讨论】:

    【解决方案2】:

    这应该可行:

    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()
    

    您可以以类似的方式调整任何子图。

    【讨论】:

    • 您好,您的回答是(几乎正确)。问题是,不应该有ax=axn 部分,请编辑您的回复(我将在下面发布完整答案作为参考)。 ;) 我将使用您提到的这种方法。这似乎很简单。谢谢。
    猜你喜欢
    • 2021-04-08
    • 2022-06-10
    • 1970-01-01
    • 2018-12-06
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多