How to change the color background of a matplotlib figure ?

November 20, 2019    /    Viewed: 25583    /    Comments: 0    /    Edit


Examples of how to change the color background of a matplotlib figure:

Using subplot and the option axisbg

from pylab import *

subplot(111, axisbg='#ababab')
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s)
xlabel('x')
ylabel('y')
title('How to change background color in matplotlib ?')
savefig('exemple_01.png')
show()

How to change the color background of a matplotlib figure ?

source: How to set opacity of background colour of graph wit Matplotlib (stackoverflow)

Using patch.set_facecolor()

It is also possible to change the transparency using patch.set_alpha()

import matplotlib.pyplot as plt
from pylab import *

fig = plt.figure()

fig.patch.set_facecolor('#E0E0E0')
fig.patch.set_alpha(0.7)

ax = fig.add_subplot(111)

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s)

ax.plot(t, s) #plot(range(10))

ax.patch.set_facecolor('#ababab')
ax.patch.set_alpha(0.5)

# If we don't specify the edgecolor and facecolor for the figure when
# saving with savefig, it will override the value we set earlier!
fig.savefig('exemple_02.png', facecolor=fig.get_facecolor(), edgecolor='none')

plt.show()

How to change the color background of a matplotlib figure ?

References

Links Site
How to change plot background color? stackoverflow
color example code matplotlib
color_demo matplotlib docs
how-to-change-the-face-color-of-a-plot-using-matplotlib How to change the face color of a plot using Matplotlib (stackoverflow)
python-changing-plot-background-color-in-matplotlib Python - changing plot background color in matplotlib (stackoverflow)
how-to-set-opacity-of-background-colour-of-graph-wit-matplotlib How to set opacity of background colour of graph wit Matplotlib (stackoverflow)
how-do-i-get-the-modern-style-matplotlib-plots-often-seen-in-ipython-notebook-ex How do I get the modern style matplotlib plots often seen in iPython Notebook examples? (stackoverflow)
matplotlib-figure-facecolor-background-color Matplotlib figure facecolor (background color) (stackoverflow)
sane-color-scheme-for-matplotlib Huy Nguyen Blog


Card image cap
profile-image
Daidalos

Je développe le présent site avec le framework python Django. Je m'intéresse aussi actuellement dans le cadre de mon travail au machine learning pour plusieurs projets (voir par exemple) et toutes suggestions ou commentaires sont les bienvenus !