【问题标题】:Pyplot contour is flipped along xyPyplot 轮廓沿 xy 翻转
【发布时间】: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


【解决方案1】:

numpy.histogram2d 文档说:

请注意,直方图不遵循笛卡尔约定,其中 x 值在横坐标上,y 值在纵坐标轴上。相反,x 是沿数组的第一个维度(垂直)绘制的直方图,y 是沿数组的第二个维度(水平)绘制的。

还显示了一个示例,其中H 在被绘制之前被转置,H = H.T。所以你可能想绘制

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

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多