【问题标题】:Plotting 2d histogram of data with very different ranges in Python在 Python 中绘制具有非常不同范围的数据的二维直方图
【发布时间】:2021-07-28 17:50:37
【问题描述】:

我尝试使用以下代码绘制具有非常不同范围的数据的二维直方图。但是,由于数据范围不同,x 数据重叠如下图。是否有任何解决方案可以绘制具有相同轴长的 x 和 y 数据?

import numpy as np
from matplotlib import pyplot as plt
plt.clf()
x = np.random.randint(low=0, high=10, size=8873)
y = np.random.randint(low=100000,high=600000, size=8873)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.imshow(heatmap.T, extent=extent, origin='lower')
plt.show()

【问题讨论】:

  • plt.imshow(... aspect='auto')

标签: python matplotlib heatmap


【解决方案1】:

请注意,imshow() 默认情况下将方面设置为1,将其更改为auto 应该可以解决您的问题。您还可以根据范围计算自己的纵横比,以获取例如方形图像。

aspect = (extent[1] - extent[0]) / (extent[3] - extent[2])
plt.imshow(heatmap.T, extent=extent, origin='lower', aspect=aspect)
# plt.imshow(heatmap.T, extent=extent, origin='lower', aspect='auto')

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 2014-10-08
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多