Histogram where the counts come from a field

I would like to create a 2D histogram (using graph objects) where the counts should come from a separate sequence.
So instead of counting the number of elements for each interval on the x axis, the plot should show the sum of all the counts from the second sequence for the elements corresponding to the values in the x interval.

x: 1.0, 2.0, 2.1, 3.0, 5.1, 5.2, 5.3
c: 2, 1, 9, 3, 1, 1, 1

So for bins [1,3) [3, 5) [5, 7)

the normal histogram would show counts 3, 1, 3

the histogram I want would show counts 12, 3, 3

Is this possible with plotly out of the box or do I have to calculate this myself and then show as bar chart?

@johann.petrak

This is the list of binning functions for go.Histogram2d:

help(go.Histogram2d.histfunc)
Help on property:

    Specifies the binning function used for this histogram trace.
    If "count", the histogram values are computed by counting the
    number of values lying inside each bin. If "sum", "avg", "min",
    "max", the histogram values are computed using the sum, the
    average, the minimum or the maximum of the values lying inside
    each bin respectively.
    
    The 'histfunc' property is an enumeration that may be specified as:
      - One of the following enumeration values:
            ['count', 'sum', 'avg', 'min', 'max']
    
    Returns
    -------
    Any

I donโ€™t understand very well your binning rule, but you can implement a function that computes the number, z, of points in each bin, according to this rule, and plot the Heatmap corresponding to your derived z-data, and x, y.

1 Like

Thanks, that hint was very helpful!
The help text is IMO a bit confusing as it does not say what the โ€œvaluesโ€ are here: the histogram values could be the xโ€™s which get binned and counted per bin, or, what I need: additional values for each x which should get averaged or summed.

Here is the working code which creates the histogram 12,3,3, I need

x = [ 1.0, 2.0, 2.1, 3.0, 5.1, 5.2, 5.3]
y = [2, 1, 9, 3, 1, 1, 1]
fig = pgo.Figure()
fig.add_histogram(x=x, y=y,histfunc="sum",xbins=dict(start=1,end=7,size=2),autobinx=False)
fig.show()

Thanks again!