保存时Matplotlib图facecolor alpha(背景颜色,透明度)

时间:2014-07-02 23:21:06

标签: python matplotlib scipy

之前的一个问题涉及使用savefig()使用与屏幕上显示的相同的facecolor(背景颜色)进行保存,即:

fig = plt.figure()
fig.patch.set_facecolor('blue')
fig.savefig('foo.png', facecolor=fig.get_facecolor())

Matplotlib figure facecolor (background color)

(使用savefig()要求我们重新指定背景颜色。)

我还可以指定透明度的alpha,例如:How to set opacity of background colour of graph wit Matplotlib

fig.patch.set_alpha(0.5)

我无法找到一种方法来使用透明的facecolor保存图形,因为它出现在屏幕上。文档似乎不完整:http://matplotlib.org/faq/howto_faq.html#save-transparent-figures - 实际的保存没有显示。将transparent=Truesavefig()一起使用不会产生使facecolor透明的效果,相反它似乎会使除了图例之外的所有内容在该颜色(包括图形背景)之上变得透明。

编辑:代码的一些相关摘录:

def set_face_color(fig, facecolor):
    if facecolor is False:
        # Not all graphs get color-coding
        facecolor = fig.get_facecolor()
        alpha = 1
    else:
        alpha = 0.5
    color_with_alpha = colorConverter.to_rgba(
        facecolor, alpha)
    fig.patch.set_facecolor(color_with_alpha)

def save_and_show(plt, fig, save, disp_on, save_as):
    if save:
        plt.savefig(save_as, dpi=dpi_file, facecolor=fig.get_facecolor(),
                    edgecolor='none')
    if disp_on is True:
        figManager = plt.get_current_fig_manager()
        figManager.window.showMaximized()
        plt.show()
    else:
        plt.close('all')

有可能将这些结合起来,但我经常在图形函数的开头调用set_face_color(),然后构建子图的网格,然后save_and_show()到最后。我想它应该适用于任何一个地方,但最佳的是我更喜欢将这些功能分开,并能够从最终的图中提取alpha以传递给savefig()

编辑2 - 值得千言万语

左边是Alpha = 0.5,右边是1。

image of code below

t = [1, 2, 3, 4, 5]
fig = plt.figure()
fig.patch.set_alpha(0.5)
fig.set_facecolor('b')
plt.plot(t, t)
fig2 = plt.figure()
fig2.set_facecolor('b')
plt.plot(t,t)

1 个答案:

答案 0 :(得分:4)

我在Matplotlib 1.5上运行了你的代码,发现它为我产生了预期的输出。对于将来发生的所有事情,我将在下面提供两种简明的方法来实现这一目标。

快速说明,您绝对不想将transparent=True设置为savefig的选项,因为这会覆盖facecolors,如matplotlib.figure.savefig source中所示。

要真正解决您的问题,您发布的第二个链接How to set opacity of background colour of graph wit Matplotlib实际上解决了问题。问题中代码段的问题是使用fig.set_facecolor而不是fig.patch.set_facecolor

修复1:

从上面的链接问题中,使用facecolor参数来savefig

import matplotlib.pyplot as plt

fig = plt.figure()
fig.patch.set_facecolor('b') # instead of fig.patch.set_facecolor
fig.patch.set_alpha(0.5)

plt.plot([1,3], [1,3])
plt.tight_layout()
plt.show()
plt.savefig('method1.png', facecolor=fig.get_facecolor())

修复2:

您还可以通过rcParams指定savefig facecolor。

import matplotlib.pyplot as plt
import matplotlib as mpl

fig = plt.figure()

col = 'blue'
#specify color of plot when showing in program. 
#fig.set_facecolor(col) also works
fig.patch.set_facecolor(col)
#specify color of background in saved figure
mpl.rcParams['savefig.facecolor'] = col 

#set the alpha for both plot in program and saved image
fig.patch.set_alpha(0.5)
plt.plot([1,3], [1,3])
plt.tight_layout()
plt.show()
plt.savefig('method2.png')

如果您希望您的轴具有背景,那么这些解决方案应该保留该背景(例如由Seaborn在Erotemic的评论中生成)。如果你想更加明确地添加:

ax.patch.set_color('palegoldenrod') # or whatever color you like
ax.patch.set_alpha(.7)

轴补丁alpha将转移到savefig而无需额外的努力。

请注意,在这两种情况下,我都使用plt.tight_layout()来消除已保存图形中无用的额外空间。您可以在matplotlib documentation中阅读更多相关内容。

相关问题