我需要一些Matplotlib等高线图的帮助。
问题是,我的轮廓图(如下图所示)的线条没有闭合,而且我的图像有两种切割方式。我想知道我是否可以强制使用Matplotlib来关闭轮廓,或者我的数据是否有太差的统计数据。但是,我选择了x轴值> gt的所有对象。 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')
答案 0 :(得分:0)
原则上,轮廓线不能闭合有两个原因。
关于这一点没有太多可以做的,更改线条或背景颜色。
为了解决第二个问题,可以扩展数据或将数据的边缘值分配给最小值,以便线路需要关闭。这在第三和第四个示例图像中显示,其中我将数据矩阵的最后一列设置为零。
作为参考,这是产生上述图像的代码。
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()