【问题标题】:Matplotlib contour lines are not closing upMatplotlib 等高线没有闭合
【发布时间】:2017-05-28 19:12:01
【问题描述】:

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

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

我正在使用:

  • 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')

【问题讨论】:

  • 如果丢弃部分数据,您希望轮廓如何闭合?
  • 你是如何选择数据的? x
  • @RolandSmith Smith:这是我试图找出的一部分。我可以强制轮廓以某种方式关闭以使情节看起来更好,还是不可能。所以我知道我没有做错什么,只是这种选择的数据......
  • @periphreal:不,选择中没有 NaN 值,我之前处理过它们...
  • 您可以使用(对数)线性插值人工生成短范围的值,例如 9.9 到 10 之间。

标签: python matplotlib contour contourf


【解决方案1】:

原则上,等高线不闭合可能有两个原因。

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

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多