事情被“拉伸”了,因为你正在使用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()
我们可以通过添加调用aspect="auto"
来修复它:imshow
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()