Pyplot轮廓沿xy翻转 [英] Pyplot contour is flipped along xy

查看:82
本文介绍了Pyplot轮廓沿xy翻转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在散点图上有很多数据-最终结果将有几百万点。这太多了,无法在散点图上理解-我想将其转换为2D直方图并绘制等高线图,如此处所述文档说:


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


还显示了一个示例,其中 H 在绘制前先转置, H = HT 。因此,您可能想绘制

  ax.contour(HT,extent = extent)


I have a lot of data on a scatter graph- the end result will have several million points. This is too many to make sense of on a scatter graph- I want to turn it into a 2D histogram, and plot a contour plot, as described here https://micropore.wordpress.com/2011/10/01/2d-density-plot-or-2d-histogram/

This is similar to the code I'm using, and has the same problem

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()

But the contour plot is flipped- it looks like it's probably flipped along xy:

I realise this is probably easily solved, but I cannot work out what the problem is!

I put the convert_edges_to_centres function in, as I also tried plotting using the syntax pyplot.contour(x,y,z), but that didn't work either

解决方案

The numpy.histogram2d documentation says:

Please note that the histogram does not follow the Cartesian convention where x values are on the abscissa and y values on the ordinate axis. Rather, x is histogrammed along the first dimension of the array (vertical), and y along the second dimension of the array (horizontal).

There is also an example shown, where H is transposed before being plotted, H = H.T. So you may want to plot

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

这篇关于Pyplot轮廓沿xy翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆