Home > database >  Seaborn set_style() is changing the axes background color and the figure background color
Seaborn set_style() is changing the axes background color and the figure background color

Time:11-09

I am trying to use different colors for axes background color and figure background color using the matplotlib and seaborn. I got the following graph.

sns.set(rc = {'axes.facecolor': 'yellow', 'figure.facecolor': 'red'})
plt.figure(figsize = (6,6))
sns.scatterplot(x = x, y = y, data = df)
plt.show()

enter image description here

But I don't want those whitegrid lines and want ticks under the axes and called sns.set_style('ticks').

sns.set(rc = {'axes.facecolor': 'yellow', 'figure.facecolor': 'red'})
plt.figure(figsize = (6,6))
sns.set_style('ticks')
sns.scatterplot(x = x, y = y, data = df)
plt.show()

enter image description here

I got the ticks as I wanted but the axes background color is not coming as I set in 'axes.facecolor': 'yellow'. Why is this happening? How can I set the ticks and get the specified background color with no grid lines?

Also how can I adjust the width of the figure background color so that the red color strip in the above diagram is of equal width on all 4 sides?

CodePudding user response:

The axes.facecolor is part of the style definition, so if you want to use a seaborn style but also override some of its parameters, you need to do both at the same time:

sns.set_theme(style='ticks', rc={'axes.facecolor': 'yellow', 'figure.facecolor': 'red'})
plt.figure(figsize=(6,6))
sns.scatterplot(x=x, y=y, data=df)
plt.show()

Note that I switched sns.set to sns.set_theme; they are aliases and do the same thing but set_theme is the preferred interface and sns.set will likely be deprecated at some point in the future.

  • Related