Plotly:如何在条形图中使用for循环或list for name属性?

2024-09-30 12:23:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我的条形图目前的样子:

enter image description here

我的数据就是这样的:

Link to my data sheet

enter image description here

这是我的代码:

import pandas as pd
import plotly.graph_objects as go
from plotly.graph_objs import Pie, Layout,Figure
import plotly.offline as py
df = pd.read_excel('data/dummymaster.xlsx',sheet_name=0)
ds = df['Course1 completion']
df['Domain'].fillna('No Specific Domain',inplace=True)
df['Course1 completion'].fillna('Not Applicable',inplace=True)
portfolios=df['Domain'].unique().tolist()
statuses = df['Course1 completion'].unique().tolist()
print()
print(statuses)
print()

data=[]

for portfolio in portfolios:
    df_temp=df.loc[df['Domain']==portfolio].copy()
    ds=df_temp['Course1 completion']
    count = ds.value_counts()
    status = ds.value_counts(normalize=True).mul(100).round(2)
    status_df = pd.DataFrame({'Course1 completion':status.index,'percentage':status.values})
    trace1=go.Bar(x=status_df['Course1 completion'],y=status.values,name=portfolio,text=status.values)
    data.append(trace1)
    del df_temp


layout = go.Layout(
    title='ISTQB Foundation Stats',
    barmode='group',
     xaxis=dict(title='Status'),yaxis=dict(title='percentage')
)

fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='istqb_bar_prac.html').

所需的图形在x轴上应具有['Domain A' ,'Domain B', 'Domain C', ...],并且诸如['Completed' , 'Not completed', ...]之类的状态应出现在条形图的图例或名称属性中

我希望在for循环中实现这一点。可能吗

提前谢谢

附言:我对熊猫和熊猫是完全陌生的


Tags: importtruegodfdatadomainasstatus
1条回答
网友
1楼 · 发布于 2024-09-30 12:23:38

Expected graph is like the legends(ie the name attribute 'Domain A' ,'Domain B', 'Domain C' etc should come in x-axis

and the status such as 'Completed' , 'Not completed' etc should come in the legends or the name attribute for the bar chart.

像这样

enter image description here

I would like to achieve this within the for loop, is that possible ?

可能是的。但是有很多更简单的方法可以得到你想要的。看看问题Pandas: How to find percentage of group members type per subgroup?的众多优秀答案中的一个,看看如何将代码缩减到一两行。然后用几条额外的线构建一个完整的plotly图形,如下所示:

# data restructuring
df_ply = pd.crosstab(df['Domain'], df['Course1 completion'], normalize='index')

# plotly setup
fig = go.Figure()

# add trace for eat
for col in df_ply.columns:
    #print(col)
    fig.add_trace(go.Bar(x=df_ply.index, y=df_ply[col], name = col))

fig.update_layout(title=dict(text='Completion status per domain'))
fig.show()

这将遍历重新构造的数据帧的列,并在每个Course1 Completion状态中添加一列,并在每个Domain中显示它们

用数据完成代码:

# imports
import pandas as pd
import plotly.graph_objects as go
from plotly.graph_objs import Pie, Layout,Figure
import plotly.offline as py

# data
df = pd.DataFrame({'HC No.': {0: 1,
  1: 2,
  2: 3,
  3: 4,
  4: 5,
  5: 6,
  6: 7,
  7: 8,
  8: 9,
  9: 10,
  10: 11,
  11: 12,
  12: 13,
  13: 14,
  14: 15,
  15: 16,
  16: 17,
  17: 18,
  18: 19,
  19: 20,
  20: 21,
  21: 22,
  22: 23,
  23: 24,
  24: 25,
  25: 26},
 'Domain': {0: 'Domain A',
  1: 'Domain A',
  2: 'Domain A',
  3: 'Domain A',
  4: 'Domain A',
  5: 'Domain B',
  6: 'Domain B',
  7: 'Domain B',
  8: 'Domain B',
  9: 'Domain B',
  10: 'Domain B',
  11: 'Domain C',
  12: 'Domain C',
  13: 'Domain C',
  14: 'Domain C',
  15: 'Domain C',
  16: 'Domain D',
  17: 'Domain D',
  18: 'Domain D',
  19: 'Domain D',
  20: 'Domain D',
  21: 'Others',
  22: 'Others',
  23: 'Others',
  24: 'Others',
  25: 'Others'},
 'Project': {0: 'Dog',
  1: 'Dog',
  2: 'Cat',
  3: 'Cat',
  4: 'Bird',
  5: 'Tree',
  6: 'Tree',
  7: 'Plant',
  8: 'Seed',
  9: 'Seed',
  10: 'Soil',
  11: 'Liquid',
  12: 'Solid',
  13: 'Solid',
  14: 'Solid',
  15: 'Gas',
  16: 'Gas',
  17: 'Gas',
  18: 'Gas',
  19: 'Slime',
  20: 'Slime',
  21: 'Metal',
  22: 'Metal',
  23: 'wood',
  24: 'wood',
  25: 'Plastic'},
 'Sub Project\n': {0: '',
  1: '',
  2: '',
  3: '',
  4: '',
  5: '',
  6: '',
  7: '',
  8: '',
  9: '',
  10: '',
  11: '',
  12: '',
  13: '',
  14: '',
  15: '',
  16: '',
  17: '',
  18: '',
  19: '',
  20: '',
  21: '',
  22: '',
  23: '',
  24: '',
  25: ''},
 'Emp Name': {0: 'Associate 1',
  1: 'Associate 2',
  2: 'Associate 3',
  3: 'Associate 4',
  4: 'Associate 5',
  5: 'Associate 6',
  6: 'Associate 7',
  7: 'Associate 8',
  8: 'Associate 9',
  9: 'Associate 10',
  10: 'Associate 11',
  11: 'Associate 12',
  12: 'Associate 13',
  13: 'Associate 14',
  14: 'Associate 15',
  15: 'Associate 16',
  16: 'Associate 17',
  17: 'Associate 18',
  18: 'Associate 19',
  19: 'Associate 20',
  20: 'Associate 21',
  21: 'Associate 22',
  22: 'Associate 23',
  23: 'Associate 24',
  24: 'Associate 25',
  25: 'Associate 26'},
 'Education mark': {0: '1,46975374',
  1: '0,4285622',
  2: '1,13064316',
  3: '1,29683695',
  4: '1,18009194',
  5: '1,99',
  6: '0,73110463',
  7: '1,08737382',
  8: '1,72600086',
  9: '0,35357572',
  10: '0,19593062',
  11: '1,96790904',
  12: '1,02216422',
  13: '1,92464914',
  14: '1,57124406',
  15: '1,65805295',
  16: '0,19593062',
  17: '0',
  18: '0,93860653',
  19: '0,41443375',
  20: '0,90421186',
  21: '1,54062763',
  22: '1,3367975',
  23: '0,41977105',
  24: '1,99',
  25: '1,99'},
 'Course1 completion': {0: 'Completed',
  1: 'Completed',
  2: 'Completed',
  3: 'Not Completed',
  4: 'Completed',
  5: 'Not Completed',
  6: 'Completed',
  7: 'Completed',
  8: 'Not Completed',
  9: 'Completed',
  10: "Planned in Q4 FY'20",
  11: 'Completed',
  12: 'Completed',
  13: 'Not Completed',
  14: 'Not Required',
  15: 'Completed',
  16: 'Completed',
  17: 'Not Required',
  18: 'Completed',
  19: 'Completed',
  20: 'Completed',
  21: 'Not Completed',
  22: 'Not Completed',
  23: 'Completed',
  24: 'Completed',
  25: 'Not Completed'},
 'Course2 completion': {0: 'Completed',
  1: 'Completed',
  2: '',
  3: '',
  4: '',
  5: '',
  6: '',
  7: '',
  8: '',
  9: '',
  10: '',
  11: '',
  12: '',
  13: '',
  14: '',
  15: '',
  16: 'Completed',
  17: '',
  18: '',
  19: 'Completed',
  20: '',
  21: '',
  22: '',
  23: '',
  24: '',
  25: ''}})

# data restructuring
df_ply = pd.crosstab(df['Domain'], df['Course1 completion'], normalize='index')

# plotly setup
fig = go.Figure()

# add trace for eat
for col in df_ply.columns:
    #print(col)
    fig.add_trace(go.Bar(x=df_ply.index, y=df_ply[col], name = col))

fig.update_layout(title=dict(text='Completion status per domain'))
fig.show()

相关问题 更多 >

    热门问题