[英]Pyplot Scatter to Contour plot
我正在使用pyplot從python中的數千個點制作散點圖。 我的問題是他們傾向於集中在一個地方,而這只是一大堆積分。
是否有某種功能可以使pyplot繪圖點達到某個臨界密度,然后使其成為等高線圖?
我的問題類似於這個問題,其中示例圖具有輪廓線,其中顏色代表繪制點的密度。
謝謝
首先,您需要對數據進行密度估算。 根據您選擇的方法,可以獲得不同的結果 。
假設你想要進行高斯密度估計 ,基於scipy.stats.gaussian_kde
的例子,你可以得到密度高度:
def density_estimation(m1, m2):
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
return X, Y, Z
然后,您可以使用contour
繪制它
X, Y, Z = density_estimation(m1, m2)
fig, ax = plt.subplots()
# Show density
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax])
# Add contour lines
plt.contour(X, Y, Z)
ax.plot(m1, m2, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
plt.show()
作為替代方案,你可以改變基於它們的密度如圖所示,標識的顏色在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.