簡體   English   中英

保存時Matplotlib圖facecolor alpha(背景顏色,透明度)

[英]Matplotlib figure facecolor alpha while saving (background color, transparency)

之前的一個問題涉及使用savefig()以與屏幕上顯示的相同的facecolor(背景顏色)進行保存,即:

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

Matplotlib圖facecolor(背景顏色)

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

我還可以為透明度指定alpha,例如: 如何使用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。

下面的代碼圖片

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)

我在Matplotlib 1.5上運行你的代碼,發現它為我產生了預期的輸出。 對於將來發生的所有事情,我將在下面提供兩種簡明的方法來實現這一目標。

快速說明,您絕對不希望將transparent=True設置為savefig的選項,因為這將覆蓋facecolors,如matplotlib.figure.savefig 源中所示

要實際解決您的問題,您發布的第二個鏈接如何設置圖形背景顏色的不透明度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 文檔中閱讀更多相關內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM