【问题标题】:Plot 2D Histogram as heat map in matplotlib在 matplotlib 中将 2D 直方图绘制为热图
【发布时间】:2014-02-17 19:30:03
【问题描述】:

我有想要绘制为以下直方图 的 3 维数据。 对于每个 bin,我都有一个包含两列的文本文件,例如

1.12    0.65
1.41    0.95
1.78    1.04
2.24    2.12

等等。第一列的第一个条目(在 .txt 中)给了我第一个图块中心的值,第一列的第二行给了我第二个图块中心的值,等等。第二列是指颜色条上的值。第一列中的值以及 bin 大小是对数间隔的。我想在 matplotlib 中尽可能接近上面的内容(忽略箭头)。

【问题讨论】:

  • 到目前为止你尝试过什么?我建议使用plt.imshowplt.pcolor
  • 您可能希望将barbottom kwarg + color kwarg 一起使用。 matplotlib.org/examples/pylab_examples/bar_stacked.html
  • 当数据只占图形的一部分即不是完整的网格时,我不确定如何使用pcolor
  • 为什么1.41 0.95有两行?
  • 那是一个错误,所以我现在已经修复了。第一列现在应该是对数间隔。

标签: python matplotlib plot histogram


【解决方案1】:

我建议你使用 PolyCollection:

import numpy as np
import pylab as pl
import matplotlib.collections as mc

x = np.logspace(1, 2, 20)
polys = []
values = []
for xs, xe in zip(x[:-1], x[1:]):
    y = np.logspace(1.0, 2+np.random.rand()+2*np.log10(xs), 30)
    c = -np.log(xs*y)
    yp = np.c_[y[:-1], y[:-1], y[1:], y[1:]]
    xp = np.repeat([[xs, xe, xe, xs]], len(yp), axis=0)
    points = np.dstack((xp, yp))
    polys.append(points)
    values.append(c[:-1])

polys = np.concatenate(polys, 0)
values = np.concatenate(values, 0)

pc = mc.PolyCollection(polys)
pc.set_array(values)
fig, ax = pl.subplots()
ax.add_collection(pc)
ax.set_yscale("log")
ax.set_xscale("log")
ax.autoscale()    
pl.colorbar(mappable=pc)

这是输出:

【讨论】:

    猜你喜欢
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2016-06-09
    • 2015-10-25
    相关资源
    最近更新 更多