Search code examples
pythongraphplotlyplotly-dash

Plotly pie graph not showing all data


I have noticed that my go.Pie graph only shows 2 of the 3 values held in the dataframe column. I noticed this when creating a px.treemap referencing the exact same column in the dataframe and it shows all 3 values.

Below is my code for the pie chart and then the treemap

#docCategory count pie graph
valuesDocCat = df['docCategory'].value_counts()
figDocCat = go.Figure(data=[go.Pie(labels = df['docCategory'], values = valuesDocCat)])
figDocCat.update_traces(textposition = 'inside')
figDocCat.update_layout(uniformtext_minsize=14, uniformtext_mode='hide', title='Document Category breakdown')

#treeMap test graph
valuesTreemap = df['Kind'].value_counts()
figTreemap = px.treemap(df, path = ['docCategory', 'Kind'], color='docCategory')
figTreemap.update_traces(root_color='lightgrey')
figTreemap.update_layout(margin = dict(t=50, l=25, r=25, b=25)

You can see my code above referencing the df['docCategory'] in both instances but as you can see in the images below the pie chart doesnt have the 'Unknown' field whereas the treemap does.

Any ideas on why? I have other pie charts that have more than 2 fields being referenced and no issues, it is just happening on this one.

piechart

treemap


Solution

    • your question "Plotly pie graph not showing all data", it is showing everything.
    • figDocCat = go.Figure(data=[go.Pie(labels = df['docCategory'], values = valuesDocCat)])
      • you are passing different length arrays for labels and values. plotly is taking first 3 items from labels, some of which are the same.
      • to be consistent this line would be figDocCat = go.Figure(data=[go.Pie(labels=valuesDocCat.index, values=valuesDocCat)]). i.e. both labels and values come from the same pandas series
    • have simulated data frame to demonstrate

    full solution

    import plotly.graph_objects as go
    import plotly.express as px
    import pandas as pd
    import numpy as np
    
    cats = {
        "Structured": ["Spreadsheet"],
        "Unknown": ["System File", "Unrecognised"],
        "Unstrcutured": ["Document", "Email", "Image", "Calendar Entry"],
    }
    
    df = pd.DataFrame(
        [
            {"docCategory": c, "Kind": np.random.choice(cats[c], 2)[0]}
            for c in np.random.choice(list(cats.keys()), 25)
        ]
    )
    # docCategory count pie graph
    valuesDocCat = df["docCategory"].value_counts()
    figDocCat = go.Figure(data=[go.Pie(labels=valuesDocCat.index, values=valuesDocCat)])
    figDocCat.update_traces(textposition="inside")
    figDocCat.update_layout(
        uniformtext_minsize=14, uniformtext_mode="hide", title="Document Category breakdown"
    )
    figDocCat.show()
    
    # treeMap test graph
    valuesTreemap = df["Kind"].value_counts()
    figTreemap = px.treemap(df, path=["docCategory", "Kind"], color="docCategory")
    figTreemap.update_traces(root_color="lightgrey")
    figTreemap.update_layout(margin=dict(t=50, l=25, r=25, b=25))
    

    enter image description here