9

I would like to make a multiple plot using subplot, such that one or more specific subplots have a different background color than the rest, like in this example:

example

Note that I'm interested in setting the background color of the exterior patch of the subplots not the background color inside the plot (which can be done with facecolor='gray'). This is because I want to plot density plots and I want to distinguish some of them from the rest.

I have found similar questions like this for example where each row of subplots has a different background color, but I wasn't able to modify the code so that the color can be applied on specific subplots (say (1,2), (1,3), (2,1) and (2,2) as in the attached figure above).

This is an example code:

import numpy as np
import matplotlib.pyplot as plt

fig, subs = plt.subplots(3,3,figsize=(10,10))

images = []
for i in range(3):
    for j in range(3): 
        data = np.random.rand(20,20)
        images.append(subs[i, j].imshow(data))
        subs[i, j].label_outer()

plt.show()

Any help would be greatly appreciated.

2
  • ax.set_facecolor("red") method of the axes object, For more details, see this link - stackoverflow.com/questions/14088687/…
    – Joe Ferndz
    Commented Mar 26, 2021 at 20:27
  • 1
    My problem is that in contrast to this post, I'm interested in changing the color of the frame around the subplot, not the background of the space inside the subplot. ax.set_facecolor() can only change the inside color of a subplot, fig.patch.set_facecolor changes the color of the exterior but for the full figure, so it cannot be set for each subplot individually.
    – spyros
    Commented Mar 26, 2021 at 20:33

4 Answers 4

6

According [this post] you can use fig.patches.extend to draw a rectangle on the figure. With a high zorder the rectangle will be on top of the subplots, with a low zorder it can be behind.

Now, the exact area belonging to the surroundings of a subplot isn't well-defined. A simple approach would be to give equal space to each subplot, but that doesn't work out well with shared axes nor with the white space near the figure edges.

The example code uses a different number of columns and rows to be sure horizontal and vertical calculations aren't flipped.

import numpy as np
import matplotlib.pyplot as plt

fig, subs = plt.subplots(3, 4, figsize=(10, 8))
images = []
for i in range(3):
    for j in range(4):
        data = np.random.rand(20, 20)
        images.append(subs[i, j].imshow(data))
        subs[i, j].label_outer()

m, n = subs.shape
for _ in range(50):
    i = np.random.randint(m)
    j = np.random.randint(n)
    color = ['r', 'b', 'g'][np.random.randint(3)]
    fig.patches.extend([plt.Rectangle((j / n, (m - 1 - i) / m), 1 / n, 1 / m,
                                      fill=True, color=color, alpha=0.2, zorder=-1,
                                      transform=fig.transFigure, figure=fig)])
plt.show()

each subplot equal size

Another approach would be to use subs[i, j].get_tightbbox(fig.canvas.get_renderer()), but that bounding box just includes the texts belonging to the subplot and nothing more.

A more involved approach calculates the difference between neighboring subplots and uses that to enlarge the area occupied by the axes of the subplots:

m, n = subs.shape
bbox00 = subs[0, 0].get_window_extent()
bbox01 = subs[0, 1].get_window_extent()
bbox10 = subs[1, 0].get_window_extent()
pad_h = 0 if n == 1 else bbox01.x0 - bbox00.x0 - bbox00.width
pad_v = 0 if m == 1 else bbox00.y0 - bbox10.y0 - bbox10.height
for _ in range(20):
    i = np.random.randint(m)
    j = np.random.randint(n)
    color = ['r', 'b', 'g'][np.random.randint(3)]
    bbox = subs[i, j].get_window_extent()
    fig.patches.extend([plt.Rectangle((bbox.x0 - pad_h / 2, bbox.y0 - pad_v / 2),
                                      bbox.width + pad_h, bbox.height + pad_v,
                                      fill=True, color=color, alpha=0.2, zorder=-1,
                                      transform=None, figure=fig)])

add padding depending on distance between subplots

Depending on the layout of the plots, it still isn't perfect. The approach can be refined further, such as special treatment for the first column and lowest row. If overlapping isn't a problem, the bounding box can also be extended by the result of get_tightbbox(), using a lighter color and alpha=1.

This is how it looks like with plots that have tick labels at the four sides:

ticks all around

1
  • 1
    Thank you! The last approach is sufficiently good for my purposes. Especially given that there doesn't seem to be a predefined subplot frame in the subplot function and one apparently needs to compute the right dimensions and draw rectangles.
    – spyros
    Commented Mar 26, 2021 at 23:12
6

I was faced with the same problem and I think the nicest solution is using subfigures. Here is a example changing the color of the diagonal subplots:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,10))
subfigs = fig.subfigures(ncols=3,nrows=3)

images = []
for i in range(3):
    for j in range(3): 
        axs = subfigs[i][j].subplots()
        data = np.random.rand(20,20)
        images.append(axs.imshow(data))
        axs.label_outer()

        if i==j:
            subfigs[i][j].set_facecolor("red")

plt.show()

Output:

enter image description here

2

Set the color of the fig:

fig, subs = plt.subplots(3,3,figsize=(10,10))

images = []
for i in range(3):
    for j in range(3): 
        data = np.random.rand(20,20)
        images.append(subs[i, j].imshow(data))
        subs[i, j].label_outer()

# this    
fig.set_facecolor('red')
plt.show()

Output:

enter image description here

1
  • 1
    Thanks, but what I want is to change the color of the frame around only some of the subplots, not all of them. The purpose is to highlight them. I now made it clearer what I want to do by including the example image in the post (generated with another program).
    – spyros
    Commented Mar 26, 2021 at 20:51
1

The alternative solution I used was to pad the images with a certain color using:

img_padded = cv2.copyMakeBorder(img, border_size, border_size, border_size, border_size, cv2.BORDER_CONSTANT, value=[0, 255, 0]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.