matplotlib孵化的contourf可见性取决于pdf阅读器

时间:2017-03-05 17:58:58

标签: python pdf matplotlib contourf

我正在努力使用matplotlib的pdf后端和contourf函数。我尝试在2D彩色地图上绘制禁区。禁区用阴影轮廓f表示,透明(alpha = 0.4)黑色。下面给出了使用过的代码,编写了两个类来生成用户定义的图例:

import matplotlib
print matplotlib.__version__
import matplotlib.patches as mpatches

class ConstrainedArea(object):
   def __init__(self,axe,_xdata,_ydata,_zdata,boundaries,fc='none',ec='none',lw=None,alpha=None,hatch='//',ls='-',fill=False,label=None):
      self.bnd = boundaries
      self.fc  = fc
      self.ec  = ec
      self.lw  = lw
      self.ls  = ls
      self.al  = alpha
      self.hh  = hatch
      self.fl  = fill
      self.lb = label
      self.ctr = axe.contour(_xdata,_ydata,_zdata,boundaries,linewidths=lw,colors=ec,linestyles=ls)
      #self.ctf = axe.contourf(_xdata,_ydata,_zdata,boundaries,hatches=hatch,colors=fc,facecolors=fc,alpha=alpha)
      self.ctf = axe.contourf(_xdata,_ydata,_zdata,boundaries,hatches=hatch,colors=fc,alpha=alpha,antialiased=False)
   pass
class ConstrainedAreaHandler(object):
   def legend_artist(self,legend,orig_handle,fontsize,handlebox):
      x0,y0 = handlebox.xdescent,handlebox.ydescent
      wi,he = handlebox.width,handlebox.height
      patch = mpatches.Rectangle([x0,y0],wi,he,facecolor=orig_handle.fc,edgecolor=orig_handle.ec,hatch=orig_handle.hh,lw=orig_handle.lw,ls=orig_handle.ls,fill=orig_handle.fl,transform=handlebox.get_transform(),label=orig_handle.lb)
      handlebox.add_artist(patch)

if __name__ == "__main__":
   matplotlib.rcParams['backend'] = 'PDF'
   import numpy,matplotlib.pyplot as plt

   xs, ys = numpy.mgrid[0:30, 0:40]
   zs = (xs - 15) ** 2 + (ys - 20) ** 2 + (numpy.sin(ys) + 10) ** 2

   fig = plt.figure('test',figsize=(16.0,11.8875))
   axe = fig.add_subplot(111)
   pcm = axe.pcolormesh(xs,ys,zs,shading='gouraud')
   cas = []
   for bnd,hch,ls,lb in zip([[zs.min(),200],[400,zs.max()]],['/','\\'],['-','--'],[r'$f<200$',r'$f>400$']):
      cas.append(ConstrainedArea(axe,xs,ys,zs,bnd,hatch=hch,fc='k',ec='k',lw=3,ls=ls,alpha=0.2,fill=False,label=lb))
   cbr = fig.colorbar(pcm)
   legframe = axe.legend(cas,[c.lb for c in cas],loc=3,handler_map={ConstrainedArea:ConstrainedAreaHandler()},ncol=3,fontsize=matplotlib.rcParams['font.size']*1.2**4,numpoints=1,framealpha=0.8)
   #fig.savefig('test.pdf',bbox_inches='tight',facecolor='none',edgecolor='none',transparent=True)
   fig.savefig('test.pdf',bbox_inches='tight',transparent=True)

在阅读matplotlib问题的曲目后 GitHub matplotlib issue 3023,和 GitHub matplotlib issue 7421,我安装了matplotlib 2.0.0,认为它可以解决我的问题,但它没有。

问题定义

使用pdf后端我将结果保存为pdf,但使用evince,okular或Acrobat Reader读取同一文件会提供不同的屏幕截图,如下图所示:screenshot of picture as read with evince screenshot as read with okular

信息

预期输出是由evince(可见阴影)给出的输出。正如在其他轨道中已经提到的,contourf对象的光栅化确实给出了预期的结果,但我需要矢量图像。此外,如果使用具有高dpi(> 300)的光栅化阴影线,则阴影线宽度倾向于0,从而产生错误的输出。最后,我发现这条跟踪matplotlib generated PDF cannot be viewed with acrobat reader issue产生了这种解决方案:

  1. 使用evince
  2. 打开matplotlib输出pdf文件
  3. 将其打印为pdf
  4. 使用okular对evince打印输出进行vizualise 这给出了以下屏幕截图: enter image description here
  5. 提前感谢您对此问题的任何解释或解决方案。如果需要其他详细信息/信息,请不要犹豫,

    塔里克

0 个答案:

没有答案