事物被“拉伸”,因为你正在使用imshow
。默认情况下,它假设您要显示绘图的纵横比为 1(在数据坐标中)的图像。
如果您想禁用此行为,并使像素拉伸以填充绘图,只需指定aspect="auto"
.
例如,要重现您的问题(基于您的代码片段):
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()
![enter image description here](https://i.stack.imgur.com/tT5Fn.png)
我们只需添加即可修复它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()
![enter image description here](https://i.stack.imgur.com/EXXiE.png)