1

I have a figure of 9 sibplots (3 rows x 3 columns). I would like to plot the background color of the figure (not the subplots!) in a different color for each row. This is what I have so far:

# Imports
import matplotlib.pyplot as plt
import numpy as np

# Plot the Figure

fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(9, 9))

for r in np.arange(3):
    for c in np.arange(3):
        axes[r, c].plot(np.arange(10), np.random.randint(10, size=10))
        if r == 0:
            axes[r, c].patch.set_facecolor('azure')
        if r == 1:
            axes[r, c].patch.set_facecolor('hotpink')
        if r == 2:
            axes[r, c].patch.set_facecolor('lightyellow')
plt.show()

This figure is wrong in the sense that it colors the background inside each subplot. But what I want is to color the figure background (outside the subplots) differently for each row. How can I do this?

kirerik
  • 167
  • 1
  • 15

1 Answers1

1

something like this?

fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(9, 9))

for r in np.arange(3):
    for c in np.arange(3):
        axes[r, c].plot(np.arange(10), np.random.randint(10, size=10))

colors = ['azure','hotpink','lightyellow']
for ax,color in zip(axes[:,0],colors):
    bbox = ax.get_position()
    rect = matplotlib.patches.Rectangle((0,bbox.y0),1,bbox.height, color=color, zorder=-1)
    fig.add_artist(rect)
plt.show()

enter image description here

Code for matplotlib.__version__<3.0

The following code works in older version of matplotlib where Figure.add_artist() does not exist. However, I found that adding the rectangle to one of the axes causes problem for that axes background patch, so I had to hide all the backgrounds for a consistent look.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

fig, axes = plt.subplots(nrows=3, ncols=3)

for r in np.arange(3):
    for c in np.arange(3):
        axes[r, c].plot(np.arange(10), np.random.randint(10, size=10))

fig.tight_layout()

colors = ['azure','hotpink','lightyellow']
for ax,color in zip(axes[:,0],colors):
    bbox = ax.get_position()
    rect = Rectangle((0,bbox.y0),1,bbox.height, color=color, zorder=-1, transform=fig.transFigure, clip_on=False)
    ax.add_artist(rect)
for ax in axes.flat:
    ax.patch.set_visible(False)
plt.show()

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Thank you! This may work, but I get an error saying: AttributeError: 'Figure' object has no attribute 'add_artist'. Do you know how I can get around that error? – kirerik Jan 15 '20 at 14:22
  • Thank you for your suggestion @JohanC but I don't think that works, because then we do not plot any colored backgrounds/rectangles. – kirerik Jan 15 '20 at 15:28
  • 1
    @kirerik The above code works correctly for me using `fig.add_artist`. Maybe you're running an old version of matplotlib, or maybe your code somewhere overwrote the `fig` from `plt.subplots` – JohanC Jan 15 '20 at 15:42
  • I am using python 3.4 and I have tried to copy the code suggested by Diziet Asahi line-by-line. – kirerik Jan 15 '20 at 15:48
  • What version of matplotlib? Figure.add_artist() was added in matplotlib 3.0 apparently – Diziet Asahi Jan 15 '20 at 20:31
  • @kirerik I've added a solution that works without Figure.add_artist() – Diziet Asahi Jan 15 '20 at 21:17
  • That's perfect! That was precisely the outcome I wanted. Thank you very much! – kirerik Jan 16 '20 at 09:51