【发布时间】:2017-07-20 18:35:26
【问题描述】:
我在散点图上有很多数据 - 最终结果将有几百万个点。这在散点图上太多了,无法理解-我想将其转换为 2D 直方图,并绘制等高线图,如此处所述https://micropore.wordpress.com/2011/10/01/2d-density-plot-or-2d-histogram/
这和我使用的代码类似,也有同样的问题
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()
但是等高线图被翻转了——看起来它可能是沿着 xy 翻转的:
我意识到这可能很容易解决,但我无法弄清楚问题所在!
我将 convert_edges_to_centres 函数放入其中,因为我也尝试使用语法 pyplot.contour(x,y,z) 进行绘图,但这也不起作用
【问题讨论】:
-
你看过this吗?
-
谢谢-我没见过。不知道能不能解决
标签: python matplotlib contour