Search code examples
pythonplotlyhistogramplotly-dashkernel-density

Plotly v5.3.1 combined count histogram and KDE curve


I currently have this code:

import dash_core_components as dcc #dash version 2.0.0
import plotly.figure_factory as ff #plotly version 5.3.1

...dash app code

dcc.Graph(id = 'rug_plot_count_region_biosynthetic_protein_homologs',
          figure = ff.create_distplot([filtered_df['count_region_biosynthetic_protein_homologs'].tolist()], 
                                      group_labels = ['count_region_biosynthetic_protein_homologs'])),

...more dash app code

Which makes this figure, as part of a dash app:

plotly distplot

I'd like to have two y-axis, one showing the probability density for the KDE curve (which is the y-axis already there) and one showing the count frequency. I'd then like the KDE curve to be linked to the KDE y-axis and the histogram to be linked to the count y-axis.

Is there a way to do this using plotly?


Solution

    • you can replace the histogram or normal distribution with standard histogram
    • focus on building figure before integrating into dash
    • can't find any sample data for your domain, so have used random
    import plotly.figure_factory as ff
    import plotly.express as px
    import numpy as np
    import pandas as pd
    
    np.random.seed(1)
    
    filtered_df = pd.DataFrame({"count_region_biosynthetic_protein_homologs": np.random.randn(1000)})
    
    fig = ff.create_distplot(
        [filtered_df["count_region_biosynthetic_protein_homologs"].tolist()],
        group_labels=["count_region_biosynthetic_protein_homologs"],
        show_hist=False,
    ).add_traces(
        px.histogram(filtered_df, x="count_region_biosynthetic_protein_homologs")
        .update_traces(yaxis="y3", name="histogram")
        .data
    ).update_layout(yaxis3={"overlaying": "y", "side": "right"}, showlegend=False)
    
    fig
    

    enter image description here