Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib打印在另存为.pdf时失去透明度_Python_Pdf_Matplotlib_Plot - Fatal编程技术网

Python Matplotlib打印在另存为.pdf时失去透明度

Python Matplotlib打印在另存为.pdf时失去透明度,python,pdf,matplotlib,plot,Python,Pdf,Matplotlib,Plot,我看到了与此完全相同的问题:除了我试图输出为PDF而不是ps/eps 对上一个问题的回答指出,eps不支持透明度,建议光栅化或保存为PDF。当我保存为png时,我确实得到了正确的输出,所以看起来matplotlib正确地处理了透明度,但是后端失败了。显然,PDF支持透明性,所以我尝试使用的格式没有问题 我使用默认的MacOSX matplotlib后端和matplotlib 1.4.1在OSX(约塞米蒂)中运行。是否有任何原因导致此设置无法生成透明的PDF输出 这在过去(OSXMavericks

我看到了与此完全相同的问题:除了我试图输出为PDF而不是ps/eps

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

我使用默认的MacOSX matplotlib后端和matplotlib 1.4.1在OSX(约塞米蒂)中运行。是否有任何原因导致此设置无法生成透明的PDF输出

这在过去(OSXMavericks和matplotlib的早期版本)是可行的,但我不确定到底是什么变化导致了这个问题

如果运行以下代码,则可以在_hatch.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 bug tracker,地址为:

解决问题

(一) 您可能可以导出为支持alpha的PNG格式,然后将其包装为PDF格式,但您仍然会失去所有线条艺术,转而使用光栅化图像。。。可能不是你想要的

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

在设置要具有透明度的轴后以及保存图形之前,添加此行:

ax.set_rasterized(True)
plt.savefig("fig.pdf")
这对我在MacOS Big-Sur上的Python 3很有效


可以找到此方法的文档。

这看起来像是pdf后端中的回归,您能在matplotlib github跟踪器上将其报告为错误吗?有关于此问题的更新吗?我也面临同样的问题。我确认它在Windows上的Python2.7.1上工作。