Search code examples
pythonarrayshistogramcolorbar

Make a 2d histogram show if a certain value is above or below average?


I made a 2d histogram of two variables(x and y) and each of them are long, 1d arrays. I then calculated the average of x in each bin and want to make the colorbar show how much each x is above or below average in the respective bin.

So far I have tried to make a new array, z, that contains the values for how far above/below average each x is. When I try to use this with pcolormesh I run into issues that it is not a 2-D array. I also tried to solve this issue by following the solution from this problem (Using pcolormesh with 3 one dimensional arrays in python). The length of each array (x, y and z) are equal in this case and there is a respective z value for each x value.

My overall goal is to just have the colorbar not dependent on counts but to have it show how much above/below average each x value is from the average x of the bin. I suspect that it may make more sense to just plot x vs. z but I do not think that would fix my colorbar issue.


Solution

  • As LoneWanderer mentioned some sample code would be useful; however let me make an attempt at what you want.

    import numpy as np
    import matplotlib.pyplot as plt
    
    N = 10000
    x = np.random.uniform(0, 1, N)
    y = np.random.uniform(0, 1, N) # Generating x and y data (you will already have this)
    
    # Histogram data
    xbins = np.linspace(0, 1, 100)
    ybins = np.linspace(0, 1, 100)
    hdata, xedges, yedged = np.histogram2d(x, y, bins=(xbins, ybins))
    
    # compute the histogram average value and the difference
    hdataMean = np.mean(hdata)
    hdataRelDifference = (hdata - hdataMean) / hdataMean
    
    # Plot the relative difference
    fig, ax = plt.subplots(1, 1)
    cax = ax.imshow(hdataRelDifference)
    fig.colorbar(cax, ax=ax)
    

    If this is not what you intended, hopefully there are enough pieces here to adapt it for your needs.