3

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.

Brian
  • 13,996
  • 19
  • 70
  • 94

2 Answers2

5

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.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • Thanks, I'm really new with Python. Can I ask you if it is possible to have a 3d histogram? Also including the z coordinate? – Brian Jul 27 '11 at 08:19
  • @Matteo: I added a note on how to create a "3D histogram". Please mark this answer as accepted (on the left) if it answered your original question. :) – Eric O. Lebigot Jul 27 '11 at 10:22
3

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 pointscontains the points used for the scatter plot. enter image description here

Community
  • 1
  • 1
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234