Matplotlib绘图在另存为.pd时将失去透明度

2024-05-16 07:09:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我看到的问题与这个完全相同:Matplotlib Plots Lose Transparency When Saving as .ps/.eps除了我试图输出到PDF而不是ps/eps。在

前一个问题的答案指出eps不支持透明性,并建议将其光栅化或另存为PDF格式。当我保存为png时,我确实得到了外观正确的输出,所以看起来matplotlib正确地处理了透明性,但是后端失败了。很明显,PDF支持透明性,所以我尝试使用的格式没有问题。在

我在OSX(Yosemite)中运行,默认MacOSX matplotlib后端,使用matplotlib 1.4.1。有什么原因导致这个设置不能产生透明的PDF输出?在

这在过去是可行的(在OSX小牛和早期版本的matplotlib中使用),但我不确定是什么变化导致了这个问题。在

如果运行以下代码,则可以在_图案填充.pdf,但不在任何其他输出文件中。在

#! /usr/bin/env python
import matplotlib.pyplot as plt

fig = plt.figure( figsize=(6, 6), dpi=100, facecolor='white' )
ax = fig.add_axes( [0.15, 0.1, 0.8, 0.85] )

bin_edges = [0.0, 0.5, 0.5, 1.0, 1.0, 1.5, 1.5, 2.0, 2.0, 2.5, 2.5, 3.0, 3.0, 3.5, 3.5, 4.0, 4.0, 4.5, 4.5, 5.0, 5.0, 5.5, 5.5, 6.0, 6.0, 7.0, 7.0, 8.0]
y_low     = [0.9581631739289882, 0.9581631739289882, 0.8966054746563691, 0.8966054746563691, 0.8369962202270926, 0.8369962202270926, 0.7824880045351325, 0.7824880045351325, 0.7231695683685057, 0.7231695683685057, 0.6673767757896321, 0.6673767757896321, 0.6083447707111752, 0.6083447707111752, 0.5602384968623321, 0.5602384968623321, 0.5109567893600544, 0.5109567893600544, 0.4707872827805316, 0.4707872827805316, 0.4304527769718274, 0.4304527769718274, 0.39024135798617826, 0.39024135798617826, 0.3593458738615755, 0.3593458738615755, 0.3275704585658484, 0.3275704585658484]
y_high    = [0.9762065896798683, 0.9762065896798683, 0.9227253843496172, 0.9227253843496172, 0.8738222849514, 0.8738222849514, 0.8299500683866315, 0.8299500683866315, 0.7810616940586165, 0.7810616940586165, 0.7357506442258693, 0.7357506442258693, 0.6852756294051707, 0.6852756294051707, 0.6441575476130643, 0.6441575476130643, 0.5987788803224889, 0.5987788803224889, 0.5630257208701298, 0.5630257208701298, 0.5274860424153797, 0.5274860424153797, 0.4915335923551736, 0.4915335923551736, 0.46502435263727837, 0.46502435263727837, 0.43196895235913746, 0.43196895235913746]

ax.fill_between( bin_edges, y_low, y_high, facecolor='green', edgecolor='white', alpha=0.4 )

plt.savefig( 'without_hatch.pdf' )
plt.savefig( 'without_hatch.png' )

ax.fill_between( bin_edges, y_low, y_high, facecolor='green', edgecolor='white', hatch='xxxx', alpha=0.4 )

plt.savefig( 'with_hatch.pdf' )
plt.savefig( 'with_hatch.png' )

现在提交给matplotlib错误跟踪程序:https://github.com/matplotlib/matplotlib/issues/3841


Tags: binpdfpngmatplotlibpltepsaxlow
2条回答

PDF文件不支持透明度。尝试将其导出为SVG文件,然后如果您迫切需要PDF格式,请使用LaTeX进行转换。在

解决问题?在

(一) 你也许可以导出一个支持PNG的alpha格式,然后把它包装成PDF格式,但是你仍然失去了所有的线条艺术,转而使用光栅化的图像。。。也许你不想要什么。在

(二) 如果不同的层有不同的alpha设置,您可以将各个层渲染为单独的PDF,然后导入它们并用PDF原生alpha包装它们。这需要一些适度的PDF-fu,但应该是非常可行的。另一方面,如果alpha值在给定的区域内变化,这是行不通的。在

相关问题 更多 >