berti berti - 3 months ago 22
Python Question

How to use multiple background colors for a figure in Python?

I just want to plot several data sets, say 4, using subfigures, i.e. something like

fig = figure(1)
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)


This is working well. But additionally I´d like to set different background colours for the two subplots in the first row and the ones in the second row such that the upper half of the figure´s background is black and the lower half white.
Can anybody tell me how to do this?

Well, what I tried so far was to define two figures, one with a black and another one with a white background adding the first two subfigures to figure 1 and the other ones to figure 2. In the end I merged both figures into a PDF but the results was not satisfying since the PDF file was a mess and the two figures were actually looking like two distinct ones but not like a single figure.

Additionally I tried something like

fig = figure(1)
rect = fig.patch
rect.set_facecolor('black')
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
rect = fig.patch
rect.set_facecolor('white')
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)


but apparently it cannot work like this. Then I tried to create a rectangle as background for each subfigure using matplotlib.patches which seems to be inappropriate as well.

Answer

I had the same problem and came up with the following solution:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig = plt.figure(1)

# create rectangles for the background
upper_bg = patches.Rectangle((0, 0.5), width=1, height=0.5, 
                             transform=fig.transFigure,      # use figure coordinates
                             facecolor='gray',               # define color
                             edgecolor='none',               # remove edges
                             zorder=0)                       # send it to the background
lower_bg = patches.Rectangle((0, 0), width=1.0, height=0.5, 
                             transform=fig.transFigure,      # use figure coordinates
                             facecolor='white',              # define color
                             edgecolor='none',               # remove edges
                             zorder=0)                       # send it to the background

# add rectangles to the figure
fig.patches.extend([upper_bg, lower_bg])

# create subplots as usual
fig.add_subplot(221)
fig.add_subplot(222)
fig.add_subplot(223)
fig.add_subplot(224)

plt.show()

Note that you have to explicitly set zorder, because otherwise the patches are in front of the subplots. The resulting figure looks as follows:

Resulting figure with two different background colors This approach still relies on matplotlib.patches and may therefore not be the clean one you are looking for, but I thought it might be useful for other people having this issue.

More information on manipulating the figure itself can be found here: http://matplotlib.org/users/artists.html#figure-container

Comments