2D histogram with Python

15,041

Solution 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.

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

Share:
15,041
Brian
Author by

Brian

Updated on June 29, 2022

Comments

  • Brian
    Brian almost 2 years

    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
    Brian almost 13 years
    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?
  • Bruno Feroleto
    Bruno Feroleto almost 13 years
    @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. :)