【发布时间】:2013-05-09 11:42:59
【问题描述】:
我使用带有颜色条的 matplotlib pcolormesh 绘图,对绘图和颜色条应用光栅化以减小文件大小并将图形保存为 PDF 文件。因此我注意到,在光栅化之后,颜色区域本身相对于轴向上和向左移动了一点,因此在图的下边缘和右边缘出现了一条白色条纹。颜色条也发生了同样的情况,我发现情况更糟:使用细颜色条时,白色条纹非常明显且令人不安。有没有办法避免光栅化图的这种行为,并将光栅化区域保持在与光栅化之前相同的位置?
我试着玩弄rasterization_zorder 和zorder settings。它对pcolormesh 的绘图有所帮助(下部的白色条纹消失了),但我发现无法将其应用于colorbar。
下面有一个简单的例子,有四个图来说明问题。请放大图右下边缘的 PDF 文件以了解我的意思。
import numpy as np
import matplotlib.pyplot as plt
d = np.arange(100).reshape(10, 10)
myfig = plt.figure(figsize=(5, 5))
'''plot 1, no rasterization'''
ax1 = plt.subplot(221)
plot1 = ax1.pcolormesh(d)
cbar1 = plt.colorbar(plot1)
ax1.set_title("no rasterization", fontsize = 10)
'''plot 2, main plot rasterized, colorbar not'''
ax2 = plt.subplot(222)
plot2 = ax2.pcolormesh(d, rasterized=True)
cbar2 = plt.colorbar(plot2)
ax2.set_title("plot rasterized", fontsize = 10)
'''plot 3, main plot and colorbar rasterized'''
ax3 = plt.subplot(223)
plot3 = ax3.pcolormesh(d, rasterized=True)
cbar3 = plt.colorbar(plot3)
cbar3.solids.set_rasterized(True) # !!!!!!!!
ax3.set_title("plot and cbar rasterized", fontsize = 10)
'''plot 4, whole axes of main plot and colorbar rasterized, attempt to use rasterization_zorder'''
ax4 = plt.subplot(224)
ax4.set_rasterization_zorder(-10)
plot4 = ax4.pcolormesh(d, zorder=-20)
'''colorbarbar gets its own axis'''
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
ax_cbar4 = inset_axes(ax4, width="3%", height="100%", loc=6)
ax_cbar4.set_rasterization_zorder(-10)
locator_ax_cbar4 =ax_cbar4.get_axes_locator()
locator_ax_cbar4.set_bbox_to_anchor ((1.0, 0 , 1, 1), ax4.transAxes)
cbar4=plt.colorbar(plot4, cax=ax_cbar4)
#cbar4.solids.set_rasterization_zorder(-10) # ---> NOT WORKING
cbar4.solids.set_rasterized(True)
ax4.set_title("axes rasterized and zorder changed", fontsize = 10)
plt.savefig("D:/test_rasterization_3plots.pdf", dpi=150)
print 'pdf file saved'
plt.show()
任何建议将不胜感激!
【问题讨论】:
-
你用的是什么版本的mpl?我无法重现这个(在主人身上)。
-
我正在使用带有 Win7 64 位的 matplotlib 1.2.0。我正在谈论的问题仅出现在保存的 pdf 图中,而不是 matplotlib 的图形窗口中。谢谢!
-
生成的 pdf 在 1.3.x、2.7、linux 上对我来说看起来不错。您可以发布指向示例 pdf 的链接吗?
-
可以在dropbox.com/sh/138tn5vs28xyur3/Z3rSH4F44b 中找到生成的 pdf 和带有放大效果的图像,tcaswell 对你有用吗?因此,如果 pdf 在 Linux 上看起来不错,还有一个理由切换,但这需要时间......
标签: python matplotlib colorbar