使用Python的2D直方图

Bri*_*ian 3 python 2d matplotlib histogram

我正在尝试使用这些代码在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()
Run Code Online (Sandbox Code Playgroud)

一切都运行正常:我有一个颜色条,代表每个单元格中的计数.问题是我想得到计数的日志,但函数histrogram2d没有任何选项.

Eri*_*got 5

我想你可以干脆做

H_log = np.log(H)
…
plt.imshow(H_log,…)
Run Code Online (Sandbox Code Playgroud)

(假设您没有空计数).

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

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