How I can remove legend completely from sub boxplots?

Viewed 44

I actually have difficulty erasing the legend completely from subplots. After using .legend_.remove() command, I can still see the invisible legend space in every subplots. Could you let me know what is wrong in my code? I attached the picture below. And if anyone can reduce the coding, please go ahead and help me for easier codes!

fig, axes = plt.subplots(nrows=1, ncols=4, figsize=(18,6))   
a = sns.boxplot(data = df, ax=axes[0], x='Building', y='CDL-08', hue = 'Week', palette="Set3")
b = sns.boxplot(data = df, ax=axes[1], x='Building', y='CDL-09', hue = 'Week', palette="Set3")
c = sns.boxplot(data = df, ax=axes[2], x='Building', y='CDL-10', hue = 'Week', palette="Set3")
d = sns.boxplot(data = df, ax=axes[3], x='Building', y='CDL-11', hue = 'Week', palette="Set3")

a.legend_.remove()
b.legend_.remove()
c.legend_.remove()
d.legend_.remove()


#placing legend outside
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)


plt.subplots_adjust(left= 0.11, bottom=0.11, right=0.9, top=0.85, wspace=0.3, hspace=0.3)

a.xaxis.label.set_visible(False)
b.xaxis.label.set_visible(False)
c.xaxis.label.set_visible(False)
d.xaxis.label.set_visible(False)

a.yaxis.label.set_visible(False)
b.yaxis.label.set_visible(False)
c.yaxis.label.set_visible(False)
d.yaxis.label.set_visible(False)

fig.text(0.5, 0.04, 'Building', ha='center', va='center', fontsize = 14)
fig.text(0.06, 0.5, 'CO2 concentration (ppm)', ha='center', va='center', rotation='vertical', fontsize = 14)

#threshold
a.axhline(y=1000, color='red', linestyle='--', linewidth = 1.5)
a.axhline(y=800, color='#8B2323', linestyle='--', linewidth = 1.5)

b.axhline(y=1000, color='red', linestyle='--', linewidth = 1.5)
b.axhline(y=800, color='#8B2323', linestyle='--', linewidth = 1.5)

c.axhline(y=1000, color='red', linestyle='--', linewidth = 1.5)
c.axhline(y=800, color='#8B2323', linestyle='--', linewidth = 1.5)

d.axhline(y=1000, color='red', linestyle='--', linewidth = 1.5)
d.axhline(y=800, color='#8B2323', linestyle='--', linewidth = 1.5)

#common title
fig.suptitle('CO2 exposures', fontsize = 18)

#title for subplots
a.set_title("Subject 01", fontsize=14)
b.set_title("Subject 02", fontsize=14)
c.set_title("Subject 03", fontsize=14)
d.set_title("Subject 04", fontsize=14)


plt.rcParams['xtick.labelsize'] = 14

min([], default="EMPTY")
# returns EMPTY

# Defining custom 'xlim' and 'ylim' values.
custom_ylim = (300, 1500)

# Setting the values for all axes.
plt.setp(axes, ylim=custom_ylim, yticks=[300, 600, 900, 1200, 1500])
a.set_yticklabels([300, 600, 900, 1200, 1500], fontsize=15)
b.set_yticklabels([300, 600, 900, 1200, 1500], fontsize=15)
c.set_yticklabels([300, 600, 900, 1200, 1500], fontsize=15)
d.set_yticklabels([300, 600, 900, 1200, 1500], fontsize=15)

plt.show()

Still I can see invisible

0 Answers
Related