foghorn
foghorn

Reputation: 1027

Resize/stretch 2d histogram in matplotlib with x-y dependent number of bins?

I am making a 2d histogram as follows:

import matplotlib as plt
import numpy as np

hist,xedges,yedges = np.histogram2d(x,y,bins=[50,150],range=[[0,50],[0,150]])
Hmasked = np.ma.masked_where(hist==0,hist) # Mask pixels with a value of zero
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1] ]
plt.imshow(Hmasked.T,extent=extent,interpolation='nearest',origin='lower')
plt.show()

This has 50 bins in x and 150 bins in y. The resulting plot is narrow in x and tall in y. How do a stretch out the plot in x so that the bins in x are allowed to appear much wider? If there is code that automatically stretches it to the normal figure aspect ratio, that would be optimal.

Upvotes: 2

Views: 974

Answers (1)

tacaswell
tacaswell

Reputation: 87376

plt.imshow(Hmasked.T,extent=extent,interpolation='nearest',origin='lower', aspect='auto')

Upvotes: 1

Related Questions