Change the background color in a Matplotlib plot

Python

Matplotlib

Figure

Change background color

Luc B.

Python

Matplotlib

Changing the background color is necessary for creating stylized, beautiful plots.

Code Example

Use the facecolor argument to plt.figure() and plt.axes() to change the background colors. Note that the figure background and axes are colored independently.

import matplotlib.pyplot as plt

# Set the plot background colors here
plt.figure(facecolor='#282726')
plt.axes(facecolor='#6a8a82')

# Make text white
plt.title("My Plot", color="white", fontsize=20)
plt.tick_params(colors="white")

plt.show()

png

More Examples

Object Oriented Interface

The same functionality is easily accomplished with Matplotlib's object oriented interface.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes()

# Set the plot background colors here
fig.set_facecolor('#282726')
ax.set_facecolor('#6a8a82')

# Make text white
ax.set_title("My Plot", color="white", fontsize=20)
ax.tick_params(colors="white")

plt.show()

png