Home > other >  How to set ticks for one of the three subplots in Seaborn?
How to set ticks for one of the three subplots in Seaborn?

Time:10-21

I'm looking for a solution: how to set ticks for one of the three subplots in Seaborn?

I'm aware that I can set xticks and/or yticks for all subplots

#specify positions of ticks on x-axis and y-axis
plt.xticks([15, 20, 25])
plt.yticks([4, 8, 12])

...but I don't know how can I do it for just one of the three subplots.

I want to set different yticks for the third subplot in this example code (I mean 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()

The output shows a lack of details in the third subplot now:

CodePudding user response:

I found a complete fix of my problem using an idea from #Elena_Kosourova.

My code:

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

enter image description here

CodePudding user response:

This should work:

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, ax=ax1)

plt.subplot(3, 1, 2)
sns.barplot(y='customer_canada', x='employee_name', data=df_1, ax=ax2)

plt.subplot(3, 1, 3)
sns.barplot(y='customer_dif_country', x='employee_name', data=df_1, ax=ax3)
plt.yticks([4, 8, 12])

plt.subplots_adjust(hspace = 0.8)
plt.show()

You can adjust any of the subplots in a similar way.

  • Related