使用 Python 的 2D 直方图 [英] 2D histogram with Python
问题描述
我正在尝试使用这些代码在 Python 中绘制二维直方图
I'm trying to plot a 2D histogram in Python using these code
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 没有任何选项.
Every thing works fine: I have a color bar which represent the counts in each cells. The thing is that I would like to have the log of the count but the function histrogram2d does not have any option for that.
推荐答案
我想你可以简单地做
H_log = np.log(H)
…
plt.imshow(H_log,…)
(假设您没有空计数).
(assuming that you don't have null counts).
如果你想要一个 3D 条形图,你可以调整 exampleMatplotlib 文档中提供.
If you want a 3D bar chart instead, you can adapt the example provided in the Matplotlib documentation.
更一般地说,我衷心建议您查看非常有用的 Matplotlib 画廊,当您寻找一些特定的绘图功能.
More generally, I heartily recommend that you check the very useful Matplotlib gallery, when you are looking for some specific graphing capabilities.
这篇关于使用 Python 的 2D 直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!