Search code examples
pythonpython-3.xplotlydata-analysisplotly-python

Multiple histograms for each value in column with graph object using Plotly


I want to make a multiple histogram using the make_subplot with multiples histogram inside.

So, this is what I want to do:

fig = px.histogram(df, x="% INVESTIMENTO",color='Cluster')
fig.show()

Image of the hist plot

But I want to do using make_subplot. I know that px.histogram is not supposed to work with .add_trace so how can I make the go.Histogram create several histograms in one graph_object just like px.histogram?

I tried this:

fig = make_subplots(rows=1, cols=1)
fig.add_trace(go.Histogram(x=df["% INVESTIMENTO"],color=df['Cluster']), row=1, col=1)

But I get an error

ValueError: Invalid property specified for object of type plotly.graph_objs.Histogram: 'color'


Solution

  • You can groupby Cluster and add a trace using each DataFrame group:

    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.DataFrame({
        "% INVESTIMENTO": [10, 10, 10, 11, 12, 10, 11, 11, 12, 12],
        "Cluster": [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
    })
    
    fig = make_subplots(rows=1, cols=1)
    for group, df_group in df.groupby('Cluster'):
        fig.add_trace(go.Histogram(x=df_group["% INVESTIMENTO"], name=group), row=1, col=1)
    fig.update_layout(legend_title_text='Cluster')
    
    fig.show()
    

    enter image description here