简体   繁体   English

绘制饼图未显示所有数据

[英]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.我注意到我的 go.Pie 图只显示了数据框列中的 3 个值中的 2 个。 I noticed this when creating a px.treemap referencing the exact same column in the dataframe and it shows all 3 values.我在创建引用数据框中完全相同的列的 px.treemap 时注意到了这一点,它显示了所有 3 个值。

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.您可以看到我上面的代码在这两种情况下都引用了df['docCategory']但正如您在下面的图片中看到的,饼图没有“未知”字段,而树状图有。

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.我还有其他饼图,它们引用了 2 个以上的字段并且没有问题,它只是发生在这个上。

饼形图

树状图

  • 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 .您正在为labelsvalues传递不同长度的数组。 plotly is taking first 3 items from labels, some of which are the same. plotly从标签中取出前 3 个项目,其中一些是相同的。
    • to be consistent this line would be figDocCat = go.Figure(data=[go.Pie(labels=valuesDocCat.index, values=valuesDocCat)]) .为了保持一致,这条线将是figDocCat = go.Figure(data=[go.Pie(labels=valuesDocCat.index, values=valuesDocCat)]) ie 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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM