Python 如何使用透明的外部背景和彩色子地块保存matplotlib地物?
我想用透明背景保存一个图形,其中记号标记和轴标签是透明的,但子地块面是彩色的。 我可以使用Python 如何使用透明的外部背景和彩色子地块保存matplotlib地物?,python,matplotlib,plot,Python,Matplotlib,Plot,我想用透明背景保存一个图形,其中记号标记和轴标签是透明的,但子地块面是彩色的。 我可以使用savefig和transparent=True来完成透明背景,后者可以通过为子地块内的每个轴设置facecolor='red'来完成,但不能同时使两者工作 我包括一个MWE,其中plt.show()将创建所需的面颜色,而保存的透明图显示在注释记号幻灯片上。谢谢你的帮助 import os.path as op import numpy as np import matplotlib.pyplot as p
savefig
和transparent=True
来完成透明背景,后者可以通过为子地块内的每个轴设置facecolor='red'
来完成,但不能同时使两者工作
我包括一个MWE,其中plt.show()将创建所需的面颜色,而保存的透明图显示在注释记号幻灯片上。谢谢你的帮助
import os.path as op
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(10)
y0 = np.random.rand(50)
y1 = np.random.rand(50)
x = range(len(y1))
fig, (axe0, axe1) = plt.subplots(nrows=2, sharex=True)
axe0.scatter(x, y0, c='k')
axe0.set_facecolor('red')
axe1.scatter(x, y1, c='k')
axe1.set_facecolor('blue')
dst = op.join(op.expanduser('~'), 'Desktop', 'Temp.png')
fig.savefig(dst, transparent=True, format='png')
plt.show()
不要将“透明”设置为true,而是使用
fig.patch.set_alpha(0.0)
我已将您的代码修改如下:
fig, (axe0, axe1) = plt.subplots(nrows=2, sharex=True)
axe0.scatter(x, y0, c='k')
axe0.set_facecolor('red')
axe1.scatter(x, y1, c='k')
axe1.set_facecolor('blue')
dst = op.join(op.expanduser('~'), 'Desktop', 'Temp.png')
fig.patch.set_alpha(0.0)
fig.savefig(dst, format='png')
plt.show()
不要将“透明”设置为“真”,但使用将“不透明度”设置为0
fig.patch.set_alpha(0.0)
我已将您的代码修改如下:
fig, (axe0, axe1) = plt.subplots(nrows=2, sharex=True)
axe0.scatter(x, y0, c='k')
axe0.set_facecolor('red')
axe1.scatter(x, y1, c='k')
axe1.set_facecolor('blue')
dst = op.join(op.expanduser('~'), 'Desktop', 'Temp.png')
fig.patch.set_alpha(0.0)
fig.savefig(dst, format='png')
plt.show()