2017-07-20 75 views
0

我在散點圖上有很多數據 - 最終結果將有幾百萬個點。這太多了,無法在散點圖上理解 - 我想將它變成二維直方圖,並繪製輪廓圖,如此處所述https://micropore.wordpress.com/2011/10/01/2d-density-plot-or-2d-histogram/Pyplot輪廓沿xy翻轉

這與我正在使用的代碼類似,並且具有同樣的問題

import numpy 
import matplotlib.pyplot as pyplot 

def convert_edges_to_centres(edges): 
    centres = numpy.empty(len(edges)-1) 
    for i in range(len(centres)): 
     centres[i] = (edges[i+1] - edges[i])/2 + edges[i] 
    return centres 

x = numpy.random.normal(0,1,50000) 
print numpy.amin(x),numpy.amax(x) 
y = numpy.random.beta(2,5,50000) 
print numpy.amin(y),numpy.amax(y) 

H,xedges,yedges=numpy.histogram2d(x,y,bins=25) 
xcentres,ycentres=convert_edges_to_centres(xedges),convert_edges_to_centres(yedges) 

print numpy.shape(H) 
print numpy.shape(xedges),numpy.shape(xcentres) 
print numpy.shape(yedges),numpy.shape(ycentres) 

extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] 

ax = pyplot.axes() 
ax.scatter(x,y) 
ax.contour(H,extent=extent) 

pyplot.show() 

但輪廓標繪圖flipped-它看起來像它可能沿XY翻轉:

Scatter and contour plot

我意識到這可能是很容易解決,但我不知道是什麼的親布姆是!

我把convert_edges_to_centres功能,因爲我也嘗試使用語法pyplot.contour(x,y,z)繪圖,但沒有工作,要麼

+0

你看着[這](https://stackoverflow.com/questions/18835037/matplotlib-contour-input-array-order)? – BrenBarn

+0

謝謝,我沒有看到。我不確定它是否解決了它 – user30865

回答

0

numpy.histogram2d文件說:

請注意,直方圖不遵循笛卡爾慣例,其中x值在橫座標上,y值在縱座標軸上。而是,x沿着數組的第一維(垂直)以及沿着數組的第二維(水平)的y被直方圖化。

還有一個例子顯示,其中H在被繪製之前轉置,H = H.T。所以,你可能要繪製

ax.contour(H.T,extent=extent)