当另存为.pdf时,Matplotlib图会失去透明度 [英] Matplotlib plots lose transparency when saving as .pdf

查看:60
本文介绍了当另存为.pdf时,Matplotlib图会失去透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的问题与此完全相同:保存为.ps/.eps 时,> Matplotlib图会失去透明度,但我尝试输出为PDF而不是ps/eps.

I'm seeing exactly the same issue as this one: Matplotlib Plots Lose Transparency When Saving as .ps/.eps except I am trying to output to PDF rather than ps/eps.

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

The answers to the previous question point out that eps does not support transparency and suggest either rasterising or saving as a PDF. I do get correct looking output when I save as png, so it seems as though matplotlib is dealing with the transparency correctly, but the backend is failing. Clearly PDF does support transparency, so it's not a problem with the format I'm trying to use.

我在 OS X (Yosemite) 中运行,使用默认的 MacOSX matplotlib 后端并使用 matplotlib 1.4.1.有什么原因使该设置无法产生透明的PDF输出?

I'm running in OS X (Yosemite) with the default MacOSX matplotlib backend and using matplotlib 1.4.1. Is there any reason why this setup should fail to produce transparent PDF output?

过去曾经使用过(与OS X Mavericks和更早版本的matplotlib一起使用),但我不确定到底是什么变化导致了此问题.

This used to work in the past (with OS X Mavericks and an earlier version of matplotlib) but I'm not sure exactly what change has caused this problem.

如果运行以下代码,则问题可以在with_hatch.pdf中看到,但在其他任何输出文件中都看不到.

If the following code is run, the problem can be seen in with_hatch.pdf, but not in any of the other output files.

#! /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/问题/3841

Now submitted to matplotlib bug tracker at: https://github.com/matplotlib/matplotlib/issues/3841

推荐答案

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

Add this line after setting up the axis you want to have transparency and before you save the figure:

ax.set_rasterized(True)
plt.savefig("fig.pdf")

这对我在MacOS Big Sur上的Python 3中有效.

This worked for my in Python 3 on MacOS Big Sur.

可以在此处.

这篇关于当另存为.pdf时,Matplotlib图会失去透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆