使用Python的2D直方图

时间:2011-07-26 15:03:29

标签: python 2d histogram matplotlib

我正在尝试使用这些代码在Python中绘制2D直方图

from math import *
import pylab as p
import matplotlib.pyplot as plt
import numpy as np

x=part.points[:,0]
y=part.points[:,1]
z=part.points[:,2]

H, xedges, yedges = np.histogram2d(x, y, bins=(128,128))
H.shape, xedges.shape, yedges.shape

extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]

plt.imshow(H, extent=extent, interpolation='nearest')

plt.colorbar()
plt.xlabel("x")
plt.ylabel("y")
plt.show()

每件事情都很好:我有一个颜色条代表每个单元格中的计数。问题是我想得到计数的日志,但函数histrogram2d没有任何选项。

2 个答案:

答案 0 :(得分:5)

我想你可以简单地做

H_log = np.log(H)
…
plt.imshow(H_log,…)

(假设您没有空值)。

如果您想要3D条形图,则可以调整Matplotlib文档中提供的example

更一般地说,当您正在寻找一些特定的图形功能时,我衷心建议您检查非常有用的Matplotlib gallery

答案 1 :(得分:3)

In this answer有一个2D和3D散点图和气泡直方图的解决方案。

points, sub = hist2d_scatter( radius, density, bins=4 )

points, sub = hist3d_scatter( temperature, density, radius, bins=4 )

其中submatplotlib "Subplot"个实例(3D或不是3D),points包含用于散点图的点。 enter image description here

相关问题