I want to create a plot in seaborn with a custom background color and on top of it to plot some white boxplots. Instead, when they are set to white they are basically transparent.
This is my code:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas
with open('Data_Figure.csv', 'r') as handle:
data_plot = pandas.read_csv(handle, sep='\t')
fig, ax = plt.subplots(figsize=(4,6), dpi=600)
ax.axvspan(-0.5, 0.5, facecolor='#FFFFCC', alpha=0.3)
ax.axvspan(0.5, 1.5, facecolor='#FFE600', alpha=0.3)
ax.axvspan(1.5, 2.5, facecolor='#999900', alpha=0.3)
g1 = sns.boxplot(x="Group", y="ORIG – MTP 405nm average", hue="Type", palette = ['w'],
fliersize=0, width=0.5, medianprops=dict(color="red", alpha=1), boxprops=dict(linewidth=1, facecolor='w', edgecolor='k', alpha=1),
whiskerprops=dict(linewidth=0.75, color='k', alpha=1), capprops=dict(linewidth=0.5, color='k', alpha=1),
data=data_plot, ax=ax)
plt.legend()
Instead, I get the following. With the inside of the boxes being as the background. Maybe I should use a different method to create the background? Or there should be a specification on the order of the layers?
CodePudding user response:
The boxes have zorder
0 whereas the yellow background patches have zorder
1 and hence will be plotted over the boxes.
You can specifiy the zorder of the patches, e.g. set it to -1:
ax.axvspan(-0.5, 0.5, facecolor='#FFFFCC', alpha=0.3, zorder=-1)
ax.axvspan(0.5, 1.5, facecolor='#FFE600', alpha=0.3, zorder=-1)
ax.axvspan(1.5, 2.5, facecolor='#999900', alpha=0.3, zorder=-1)