GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Background color in matplotlib

Search for a graph

HOME MATPLOTLIB BACKGROUND COLOR

Background color in matplotlib

Background color of the axes with set_facecolor

The background color of a matplotlib chart can be customized with the set_facecolor function. The only parameter required as input is the desired color for the background.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
X, Y = np.mgrid[-4:4, -4:4]
ax.quiver(X, Y)

# Background color
ax.set_facecolor('lavender')

# plt.show()

Background color in matplotlib

Border color

In case you want to customize the border color of the graph you will need to set a color for each of the sides of the box. You can set colors one by one or use a for loop to set all borders at once as in the example below.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
X, Y = np.mgrid[-4:4, -4:4]
ax.quiver(X, Y)

# Border color:
for axis in ['top', 'bottom', 'left', 'right']:
  ax.spines[axis].set_color('red')

# plt.show()

Border color in matplotlib

Background color of the figure

The background color of the figure can also be customized. For this purpose, you will need to set a new color to the figure (fig) with fig.set_facecolor, e.g. fig.set_facecolor("aliceblue") if you want to use the aliceblue color, or just input the desired color to the facecolor argument of the subplots function, as shown below.

Background color of the figure in matplotlib with facecolor

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(facecolor = 'aliceblue')
X, Y = np.mgrid[-4:4, -4:4]
ax.quiver(X, Y)

# plt.show()

Change the background colors using predefined styles

Matplotlib provides several predefined styles or themes to customize the background colors. You will need to pass the desired style name to the style.use function, as in the following example. If you want to know the names of all the available styles type print(plt.style.available) after importing matplotlib.pyplot as plt.

import numpy as np
import matplotlib.pyplot as plt

# Using a style
plt.style.use('Solarize_Light2')

fig, ax = plt.subplots()
X, Y = np.mgrid[-4:4, -4:4]

# Plot
ax.quiver(X, Y)

# plt.show()

Setting a style in matplotlib

R CODER website
Data Sketches

Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon
Storytelling with Data

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also

Matplotlib style sheets

Matplotlib style sheets

Texts and annotations in matplotlib

Texts and annotations in matplotlib

Date axis in matplotlib

Date axis in matplotlib

Save figures in matplotlib

Save figures in matplotlib

Reference lines in matplotlib

Reference lines in matplotlib

Legend in matplotlib

Legend in matplotlib

Line styles in matplotlib

Line styles in matplotlib

Figure size in matplotlib

Figure size in matplotlib

Colorbar in matplotlib

Colorbar in matplotlib

Animations in matplotlib

Animations in matplotlib

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.