使用Python制作2D直方图

3
我正在尝试使用以下代码在Python中绘制一个二维直方图:
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个回答

5
我猜你可以简单地做到这一点。
H_log = np.log(H)
…
plt.imshow(H_log,…)

(假设您没有空值计数。)
如果您想要一个3D条形图,您可以根据Matplotlib文档中提供的示例进行调整。
更普遍地说,我强烈建议在寻找特定的绘图功能时,查看非常有用的Matplotlib图库

谢谢,我对Python很陌生。我能问你一下是否可能拥有一个包含Z坐标的3D直方图吗? - Brian
@Matteo:我添加了一个关于如何创建“三维直方图”的注释。如果它回答了你最初的问题,请在左边将此答案标记为已接受。 :) - Eric O. Lebigot

3

在这个答案中,有一个适用于2D和3D散点图、气泡图和直方图的解决方案。

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

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

在这里,sub是一个matplotlib"Subplot"实例(无论是3D还是非3D),points包含用于散点图的点。 enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接