簡體   English   中英

將數據插入條形圖 plotly graph_objects 的問題

[英]problem with inserting data into the bar chart plotly graph_objects

所以我試圖將列表(內部,外部..)添加到條形圖中,但數據進入“內部”一列

條形圖

它應該與此類似

如同

這是整個 df 和我所做的工作

整個df

社會提到了最小可復制的例子,那么你必須考慮這意味着什么。 在您的情況下,問題是:

  1. 你想要 plot 條形圖和plotly.graph_objects
  2. go.Bar()需要的數據到底是什么。 statusescalation

由以上問題推斷,
a) 你如何在 StackOverflow 中為演示獲取最小部分的數據?
b) 如何從您的df准備數據並將它們轉換為 plot。


准備我們想要的數據 plot:

import random
import pandas as pd
import plotly.graph_objects as go
random.seed(42)
sample_quntity = 50
status = [random.choice(['Not Done','Something Else','Done']) for i in range(sample_quntity)]
escalation = [random.choice(['Internal','External','Unspecified']) for i in range(sample_quntity)]
df = pd.DataFrame({
    'status':status,
    'escalation':escalation
})
df
###
            status   escalation
0             Done  Unspecified
1         Not Done     External
2         Not Done     Internal
3             Done  Unspecified
4   Something Else     External
5         Not Done  Unspecified
⋮                ⋮            ⋮
43  Something Else  Unspecified
44        Not Done  Unspecified
45        Not Done     Internal
46  Something Else     Internal
47        Not Done     External
48  Something Else     External
49  Something Else     External



Plot:

# plot bar chart grouping with status and escalation and color by status
group_list = df['escalation'].unique()
palette = {"Not Done": "#d89a9e","Something Else":"#e0c1b3", "Done": "#aeb4a9"}

fig = go.Figure(data=[
    go.Bar(name='Not Done',
           x=group_list,
           y=df[df['status'] == 'Not Done'].groupby('escalation').size(),
           marker_color=palette['Not Done']),
    go.Bar(name='Something Else',
           x=group_list,
           y=df[df['status'] == 'Something Else'].groupby('escalation').size(),
           marker_color=palette['Something Else']),
    go.Bar(name='Done',
           x=group_list,
           y=df[df['status'] == 'Done'].groupby('escalation').size(),
           marker_color=palette['Done'])])

fig.update_layout(title='Group by Escalation, Color by Status', barmode='stack')
fig.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM