Matplotlib 等高线没有闭合

Matplotlib contour lines are not closing up

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

问题是,我的等值线图(如下所示)的线条没有闭合,而且我的图像被切成两半。我想知道我是否可以强制 Matplotlib 关闭等高线,或者我的数据统计数据是否太差而无法这样做。但是,我选择了所有 x 轴值 > 10.0 的对象,并获得了 10e6 个要绘制的对象。

我正在使用:

代码如下:

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. 首先是简单的视觉外观。 白色背景上的白线 可能会使看起来像消失。此效果显示在第一张图像中数据左侧的下图中。这种效果可以通过给轮廓线着色(第二张图片)来可视化。

除了更改线条或背景的颜色外,您对此无能为力。

  1. 第二个原因,可以在数据右侧的前两个图像中观察到,这些线确实没有闭合,因为它们位于数据的边缘。这当然与线条的颜色无关,是由于 matplotlib 不知道在数据之外要做什么(只有人脑认为这些线条应该闭合,但数据中没有闭合线条的证据)。

为了解决第二个问题,可以扩展数据或将数据的边缘值指定为最小值,这样线就需要闭合。这显示在第三个和第四个示例图像中,我将数据矩阵的最后一列设置为零。

作为参考,这是生成上图的代码。

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()