问题
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()
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.
回答1:
I guess that you could simply do
H_log = np.log(H)
…
plt.imshow(H_log,…)
(assuming that you don't have null counts).
If you want a 3D bar chart instead, you can adapt the example provided in the Matplotlib documentation.
More generally, I heartily recommend that you check the very useful Matplotlib gallery, when you are looking for some specific graphing capabilities.
回答2:
In this answer there is a solution for 2D and 3D Scatter and Bubble Histograms.
points, sub = hist2d_scatter( radius, density, bins=4 )
points, sub = hist3d_scatter( temperature, density, radius, bins=4 )
Where sub
is a matplotlib
"Subplot"
instance (3D or not) and points
contains the points used for the scatter plot.

来源:https://stackoverflow.com/questions/6832168/2d-histogram-with-python