2013-04-08 42 views
4

我试图通过参数extent在几个imshow()调用在各个位置调用渲染图像的图上更改轴背景。使用savefig导出图形()在matplotlib中渲染轴背景

当我保存使用savefig()图的一个pdf,我失去的背景颜色,如果轴显示多于一个图像。请注意,导出同一图形的PNG时不会发生这种情况。

下面是说明问题的最小脚本:

import matplotlib.pyplot as plt 
from numpy.random import rand 

fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True) 

ax[0].imshow(rand(15,15), extent=[0, 2, 15, 0], \ 
      cmap=plt.cm.gray, aspect='auto', interpolation='Nearest') 
ax[0].set_axis_bgcolor('k') 

ax[1].imshow(rand(15,15), extent=[0, 2, 15, 0], \ 
      cmap=plt.cm.gray, aspect='auto', interpolation='Nearest') 
ax[1].imshow(rand(15,15), extent=[4, 6, 15, 0], \ 
      cmap=plt.cm.gray, aspect='auto', interpolation='Nearest') 
ax[1].set_axis_bgcolor('k') 

ax[2].imshow(rand(15,15), extent=[0, 2, 15, 0], \ 
      cmap=plt.cm.gray, aspect='auto', interpolation='Nearest') 
ax[2].imshow(rand(15,15), extent=[4, 6, 15, 0], \ 
      cmap=plt.cm.gray, aspect='auto', interpolation='Nearest') 
ax[2].imshow(rand(15,15), extent=[8, 10, 15, 0], \ 
      cmap=plt.cm.gray, aspect='auto', interpolation='Nearest') 
ax[2].set_axis_bgcolor('k') 

ax[-1].set_xlim([0, 12]) 
fig.savefig('test.pdf', format='PDF') 
fig.savefig('test.png', format='PNG') 

这是脚本的PDF输出(EPS输出是一样的):

test.pdf

,这是预期脚本输出(保存为png):

test.png

我碰到了matplotlib错误,还是有一些命令我错过了将修复pdf输出?

编辑:我已经重新绘制的人物有默认matplotlibrc

+0

我只是想运行脚本和.pdf和png格式的版本都具有正确的背景。你正在使用哪个版本的matplotlib?我的是'1.2.0'。 – 2013-04-08 18:12:48

+0

我目前正在运行1.2.1,但在旧版本上尝试过,结果相同。我也在OS X上运行它。 – fgb 2013-04-08 18:54:50

+1

看起来你正在使用一些与默认不同的花哨的数学风格衬线字体。我想知道在你的'matplotlibrc'文件中定义的一些设置是否会搞乱PDF渲染。也许尝试重命名该文件,以便使用带默认参数的'matplotlib'?还有一个想法 - 它是否能够正确渲染为EPS文件? – 2013-04-08 19:11:27

回答

4

这最终是一个matplotlib错误。

当渲染在同一坐标多于一个的图像,合成图像创建渲染到PDF文件时不具有透明背景,所以轴的背景颜色不显示通过。

这得到了解决为an issue I opened in the matplotlib's GitHub repo一部分。

1

看着你matplotlibrc。有一部分选项从savefig开始,它们定义了您保存的数字的外观。即使默认matplotlibrc有这个部分。

还有一个类似的问题:matplotlib savefig() plots different from show()

+0

虽然这不是问题。我没有将'show()'的输出与'savefig()'的输出进行比较,而是将后者的输出与输出格式的选择进行比较。我的问题最终成为一个matplotlib错误。 – fgb 2013-04-11 21:28:41