这就是我的条形图目前的样子:
我的数据就是这样的:
这是我的代码:
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循环中实现这一点。可能吗
提前谢谢
附言:我对熊猫和熊猫是完全陌生的
像这样
可能是的。但是有很多更简单的方法可以得到你想要的。看看问题Pandas: How to find percentage of group members type per subgroup?的众多优秀答案中的一个,看看如何将代码缩减到一两行。然后用几条额外的线构建一个完整的plotly图形,如下所示:
这将遍历重新构造的数据帧的列,并在每个
Course1 Completion
状态中添加一列,并在每个Domain
中显示它们用数据完成代码:
相关问题 更多 >
编程相关推荐