Matplotlib等高线没有关闭

时间:2017-01-13 11:59:50

标签: python matplotlib contour contourf

我需要一些Matplotlib等高线图的帮助。

问题是,我的轮廓图(如下图所示)的线条没有闭合,而且我的图像有两种切割方式。我想知道我是否可以强制使用Matplotlib来关闭轮廓,或者我的数据是否有太差的统计数据。但是,我选择了x轴值> gt的所有对象。 10.0 并获得 10e6 对象进行绘图。

Figure shows contour lines and filled contours which are cut at some point and not closing up.

我正在使用:

  • Python 2.7
  • Ubuntu 14.04
  • - >印有PdfPages的地块

代码如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages

x        = np.linspace(min(mydata[:,0]), max(mydata[:,0]), 100)
y        = np.linspace(min(mydata[:,1]), max(mydata[:,1]), 100)

H        = np.histogram2d(mydata[:,0], mydata[:,1], bins=100)       
binsize  = int((H.max()-grid_min)/8)

X,Y      = np.meshgrid(x,y, indexing='ij')

#set_zero=2.0 % for example! Amount of objects which are not included in the plot 
#in order to get a not that wide spread last contour (dark blue in the figure enclosed)!    
grid_min = np.floor(int(H.max()/100.0*set_zero))        

CS=cb_axis.contour(X,Y,H, 8, lw=2.5, ls='-', col='w', 
   levels=np.arange(grid_min, np.ceil(H.max()), binsize))

cb_axis.contourf(X,Y,H, 8, levels=np.arange(grid_min, np.ceil(H.max()), 
   binsize), cmap='coolwarm'))       

plt.colorbar(CS, pad=0.01, aspect=35, use_gridspec=True, format='%0.1e')

1 个答案:

答案 0 :(得分:0)

原则上,轮廓线不能闭合有两个原因。

  1. 第一个是简单的视觉外观。在白色背景上白线可能会使外观消失。此效果显示在下面第一张图片中,左侧是数据。通过着色轮廓线(第二张图像)可以看到这种效果。
  2. 关于这一点没有太多可以做的,更改线条或背景颜色。

    1. 可以在数据右侧的前两个图像中观察到的第二个原因是线条实际上没有关闭,因为它们位于数据的边缘。这当然与线的颜色无关,并且由于matplotlib不知道在数据之外做什么(它只是人脑认为这些线应该被关闭,但没有证据表明数据中有闭合线)。
    2. 为了解决第二个问题,可以扩展数据或将数据的边缘值分配给最小值,以便线路需要关闭。这在第三和第四个示例图像中显示,其中我将数据矩阵的最后一列设置为零。

      enter image description here

      作为参考,这是产生上述图像的代码。

      import numpy as np
      import matplotlib.pyplot as plt
      import matplotlib as mpl
      from matplotlib.backends.backend_pdf import PdfPages
      
      n=90
      x        = np.arange(n)
      y        = np.arange(n)
      X,Y      = np.meshgrid(x,y)
      H        = (np.sinc((X-70)/50.)**2*np.sinc((Y-50)/50.)**2)*3600
      H[:, :50] = np.zeros((n,50))
      
      grid_min = 10
      binsize = H.max()/8.        
      
      fig, (ax, ax2, ax3, ax4) = plt.subplots(ncols=4, figsize=(12,5))
      plt.subplots_adjust(left=0.03, right=0.97, wspace=0.03)
      levels=np.arange(grid_min, np.ceil(H.max()), binsize)
      for a in (ax, ax2, ax3, ax4):
          a.contourf(X,Y,H, levels=levels, cmap='viridis')
          a.set_xlim([40,100])
          a.yaxis.set_visible(False)
          a.xaxis.set_visible(False)
      
      ax.set_title("Open white lines")
      ax.text(.076, .5, "Closed white line ", rotation=90, transform=ax.transAxes, va="center")
      ax.text(.86, .5, "Open white lines", rotation=-90, transform=ax.transAxes, va="center")     
      ax.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', colors=['w' for i in range(len(levels))]) 
      
      ax2.set_title("Open Colored lines")
      ax2.text(.06, .5, "Closed colored lines", rotation=90, transform=ax2.transAxes, va="center")
      ax2.text(.86, .5, "Open colored lines", rotation=-90, transform=ax2.transAxes, va="center")
      ax2.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', cmap="jet") 
      
      # Setting the last column of data to zero in order to close the lines
      H[:, n-1] = np.zeros(n)
      ax3.set_title("Closed White lines")
      ax3.text(.076, .5, "Closed white lines", rotation=90, transform=ax3.transAxes, va="center")
      ax3.text(.86, .5, "Closed white lines", rotation=-90, transform=ax3.transAxes, va="center")    
      ax3.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', colors=['w' for i in range(len(levels))]) 
      
      ax4.set_title("Closed Colored lines")
      ax4.text(.06, .5, "Closed colored lines", rotation=90, transform=ax4.transAxes, va="center")
      ax4.text(.86, .5, "Closed colored lines", rotation=-90, transform=ax4.transAxes, va="center")   
      ax4.contour(X,Y,H, levels=levels, linewidths=3, linestyles='-', cmap="jet") 
      
      plt.show()