I know I can change the color using fig.patch.set_facecolor("#ccdece")
but how do I have an image instead of a solid color? Like using img = plt.imread()
and ax.imshow(img)
but for the outer border.
Any help is welcome.
Advertisement
Answer
You can create a dummy ax
for the full size of the surrounding figure and add an image to that ax
. Giving the ax
a low enough zorder
makes sure it appears behind the actual plots.
For an additional effect, the facecolor of the actual plots can be made semi-transparent.
Here is an example starting from a stock image.
JavaScript
x
19
19
1
import matplotlib.pyplot as plt
2
import matplotlib.cbook as cbook
3
import numpy as np
4
5
imageFile = cbook.get_sample_data('ada.png')
6
image = plt.imread(imageFile)
7
8
fig, ax = plt.subplots(figsize=(6, 8))
9
10
bg_ax = fig.add_axes([0, 0, 1, 1], zorder=-1)
11
bg_ax.axis('off')
12
bg_ax.imshow(image)
13
t = np.linspace(0, 4 * np.pi, 200)
14
x = 2 * np.cos(t / 2)
15
y = np.sin(t)
16
ax.plot(x, y)
17
ax.set_facecolor('#FFFFFFEE')
18
plt.show()
19