【发布时间】:2020-11-09 03:36:33
【问题描述】:
我想保存一个具有透明背景的图形,其中刻度线和轴标签是透明的,但子图面是彩色的。
我可以使用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()
【问题讨论】:
标签: python matplotlib plot