Search code examples
pythonpandasplotly

Independently sized buckets for multi-faceted histogram plot


in plotly express:

I want to plot the histograms of multiple columns, that are on different scales.

I need the buckets of each subolot to be indepent of the others.

here is the code:

import plotly.express as px
df = px.data.tips()
df['total_bill_times_100'] = df['total_bill']*100
df_plot = df.melt(value_vars = ['total_bill','total_bill_times_100' ])
fig = px.histogram(df_plot, x="value", facet_col = 'variable', template='simple_white')
fig.update_yaxes(matches=None)
fig.update_xaxes(matches=None)
fig.show()

Current output: enter image description here


Solution

  • By using px.histogram() with facet_col, the facetted subplots will share the same bingroup

    bingroup: Set a group of histogram traces which will have compatible bin settings [...]

    ... which will make them have the same bin size, that is, 200.

    You can set the bingroup to None so that the bin size in each histogram is computed individually :

    fig = px.histogram(df_plot, x="value", facet_col = 'variable', template='simple_white')
    fig.update_yaxes(matches=None)
    fig.update_xaxes(matches=None)
    
    fig.update_traces(bingroup=None)
    
    fig.show()
    

    The bin size of 'total_bill' and 'total_bill_times_100' is now 2 and 200 respectively so the distribution is exactly the same :

    screenshot

    If fine tuning is needed, use plotly.graph_objects instead of plotly.express, see Custom binning.