Reputation: 3
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
Upvotes: 0
Views: 1568
Reputation: 339580
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)
Upvotes: 2