我使用的是以下代码:
fig = plt.figure(num=2, figsize=(8, 8), dpi=80,
facecolor='w', edgecolor='k')
x, y = [xy for xy in zip(*self.pulse_time_distance)]
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25))
extent = [-50, +50, 0, 10]
plt.imshow(H, extent=extent, interpolation='nearest')
plt.colorbar()然而,为了生成2D直方图,这个图是垂直拉伸的,我根本不知道如何正确地设置它的大小:

发布于 2013-06-04 21:03:13
因为您使用的是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()

我们可以通过在imshow调用中添加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', aspect='auto')
fig.colorbar(im)
plt.show()

发布于 2013-06-04 20:45:55
不确定,但我认为你应该试着修改你的historgram2d:
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25), range=[[-50, 50], [0, 10]])编辑:我不知道如何精确地调整比例,但是使用aspect='auto',matplotlib猜对了:
plt.imshow(hist.T, extent=extent, interpolation='nearest', aspect='auto')https://stackoverflow.com/questions/16917836
复制相似问题