【问题标题】:How do you save a matplotlib figure with a transparent outer background but colored subplots?如何保存具有透明外部背景但彩色子图的 matplotlib 图形?
【发布时间】:2020-11-09 03:36:33
【问题描述】:

我想保存一个具有透明背景的图形,其中刻度线和轴标签是透明的,但子图面是彩色的。 我可以使用savefigtransparent=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()

Correct colored subplots

Transparent on slide

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    不设置透明为真,而是通过使用设置不透明度=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()
    

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 2013-04-14
      • 1970-01-01
      • 2021-01-09
      • 2011-12-05
      • 2021-08-24
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多