Things are "stretched" because you're using imshow
. By default, it assumes that you want to display an image where the aspect ratio of the plot will be 1 (in data coordinates).
If you want to disable this behavior, and have the pixels stretch to fill up the plot, just specify aspect="auto"
.
For example, to reproduce your problem (based on your code snippet):
import matplotlib.pyplot as plt
import numpy as np
# Generate some data
x, y = np.random.random((2, 500))
x *= 10
# Make a 2D histogram
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25))
# Plot the results
fig, ax = plt.subplots(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k')
extent = [-50, +50, 0, 10]
im = ax.imshow(H, extent=extent, interpolation='nearest')
fig.colorbar(im)
plt.show()
And we can fix it by just adding aspect="auto"
to the imshow
call:
import matplotlib.pyplot as plt
import numpy as np
# Generate some data
x, y = np.random.random((2, 500))
x *= 10
# Make a 2D histogram
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25))
# Plot the results
fig, ax = plt.subplots(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k')
extent = [-50, +50, 0, 10]
im = ax.imshow(H, extent=extent, interpolation='nearest', aspect='auto')
fig.colorbar(im)
plt.show()