gpt4 book ai didi

python - Plotly-Dash : Want two stacked bar charts side by side from single df column

转载 作者:行者123 更新时间:2023-12-01 08:11:16 25 4
gpt4 key购买 nike

我试图并排获得两个堆叠的条形图,但无法弄清楚。

这是一个 df 示例:

Field       Issue

Police Budget cuts
Research Budget cuts
Police Time consuming
Banking Lack of oversight
Healthcare Lack of support
Research Bureaucracy
Healthcare Bureaucracy
Banking Mistrust

我想要的是第一个字段的堆积条形图。它的高度为 8,分为 2 个警察、2 个研究等。然后我想要第一个图表旁边的问题堆叠条形图。第二个项目的高度为 8,并受到 2 x 预算削减、1 x 耗时、1 x 缺乏监督等因素的影响。

我已经尝试过:

获取所有字段的堆积条形图:

trace1 = go.Bar(
x = df.Field.unique(),
y = df.Field.value_counts(),
name='Total Amount of roles'
)

获取预算削减的堆积条形图(然后复制其他问题):

trace2 = go.Bar(
x = df.Field.unique(),
y = df[df['Issue'] == 'Budget cuts'].Field.value_counts(),
name='Budget cuts'
)

data = [trace1, trace2]
layout = go.Layout(barmode='stack')

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

但是上面的代码将两个图堆叠到一个上。我想要堆叠迹线 1 和堆叠迹线 2。我也希望将其集成到 Dash 中,而不是单独设计,但说实话,这是次要的。将不胜感激任何帮助!

最佳答案

编辑 - 在评论中进行简短对话后,这是我的最新建议:

<小时/>

这是一个可能的解决方案,其中每列(字段或问题)堆叠每个类别的每次出现次数:

plotly :

enter image description here

代码:

正如您所看到的,它不是很灵活,因为您必须为每个类别(银行、警察等)添加一个 go.Bar 对象。但如果上面的 plotly 是您正在寻找的,我也会整理该部分。

# import
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

#%qtconsole

# sample data
Field = ['Police', 'Research', 'Police', 'Banking', 'Healthcare', 'Research', 'Healthcare', 'Banking']
Issue = ['Budget cuts', 'Budget cuts', 'Time consuming', 'Lack of oversight', 'Lack of support', 'Bureaucracy', 'Bureaucracy', 'Mistrust']

# Put the lists in a pandas dataframe for
# easy grouping and indexing
df = pd.DataFrame([Field, Issue]).T
df.columns = ['Field', 'Issue']
grField = df.groupby('Field').count()
grIssue = df.groupby('Issue').count()
dfgr = pd.concat([grField, grIssue], axis = 1, sort = False)
dfgr = dfgr.T

# Make one go.Bar() object for each category
# for corresponing Field / Issue
trace1 = go.Bar(
x = ['Issue'],
#y = [dfgr['Field']],
y = [dfgr['Banking'].loc['Issue']],
name='Banking')

trace2 = go.Bar(
x = ['Issue'],
#y = [dfgr['Field']],
y = [dfgr['Healthcare'].loc['Issue']],
name='Healthcare')

trace3 = go.Bar(
x = ['Issue'],
#y = [dfgr['Field']],
y = [dfgr['Police'].loc['Issue']],
name='Police')

trace4 = go.Bar(
x = ['Issue'],
#y = [dfgr['Field']],
y = [dfgr['Research'].loc['Issue']],
name='Research')

trace5 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Budget cuts'].loc['Field']],
name='Budget cuts')

trace6 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Bureaucracy'].loc['Field']],
name='Bureaucracy')

trace7 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Lack of oversight'].loc['Field']],
name='Lack of oversight')

trace7 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Lack of oversight'].loc['Field']],
name='Lack of oversight')

trace8 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Lack of support'].loc['Field']],
name='Lack of support')

trace9 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Mistrust'].loc['Field']],
name='Mistrust')

trace10 = go.Bar(
x = ['Field'],
#y = [dfgr['Field']],
y = [dfgr['Time consuming'].loc['Field']],
name='Time consuming')

# gather data and set up layout
#data = [trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8, trace9, trace10]
data = [trace10, trace9, trace8, trace7, trace6, trace5, trace4, trace3, trace2, trace1]
layout = go.Layout(barmode='stack', title = 'Stacked bar chart from single column')

# Build figure
fig = go.Figure(data=data, layout=layout)

# PLot figure
iplot(fig, filename='test.html')

关于python - Plotly-Dash : Want two stacked bar charts side by side from single df column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55238122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com