简体   繁体   中英

Python Matplotlib doesn't show figure facecolor when saving figure / plot

I'm currently having an issue with saving colors and matplotlib. When I run the following code I get the expected result

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('black')
plt.title("test")
ax = plt.gca()
ax.patch.set_facecolor('black')
fig.patch.set_facecolor('xkcd:mint green')

plt.show()

Here is a screenshot of what is shown, which is the result I'm expecting.

正确的输出

However, when I run the save function either through python or by manually clicking save on the figure it results in the following image, without the colored borders.

在此处输入图像描述

Code to reproduce image above

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('black')
plt.title("test")
ax = plt.gca()
ax.patch.set_facecolor('black')
fig.patch.set_facecolor('xkcd:mint green')

# Also doesn't work with fig.savefig
plt.savefig("test.png", dpi=200)

@JohanC pointed out in the comments it takes a face color arguement.

Fixed code

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('black')
plt.title("test")
ax = plt.gca()
ax.patch.set_facecolor('black')

# Also doesn't work with fig.savefig
plt.savefig("test.png", facecolor='xkcd:mint green', dpi=200)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM