第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly-Dash:希望從單個 df 列并排放置兩個堆積條形圖

Plotly-Dash:希望從單個 df 列并排放置兩個堆積條形圖

紫衣仙女 2021-12-09 15:35:42
我正在嘗試并排放置兩個堆疊的條形圖,但無法弄清楚。這是一個示例 df:Field       IssuePolice      Budget cutsResearch    Budget cutsPolice      Time consumingBanking     Lack of oversightHealthcare  Lack of supportResearch    BureaucracyHealthcare  BureaucracyBanking     Mistrust我想要的是第一個字段的堆積條形圖。它的高度為 8,由 2 個警察、2 個研究等細(xì)分。然后我想要第一個圖表旁邊的問題堆積條形圖。第二個將有 8 的高度,并被 2 次預(yù)算削減、1 次耗時、1 次缺乏監(jiān)督等堆疊在一起。我試過了:獲取所有字段的堆積條形圖:trace1 = go.Bar(    x = df.Field.unique(),    y = df.Field.value_counts(),    name='Total Amount of roles')獲得預(yù)算削減的堆積條形圖(然后復(fù)制其他問題):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 中,而不是單獨(dú)策劃,但老實(shí)說,這將是次要的。將不勝感激任何幫助!
查看完整描述

2 回答

?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個贊

編輯 - 在評論中進(jìn)行簡短對話后,這是我的最新建議:


這是一個可能的解決方案,其中包含每列堆疊的每個類別的每個出現(xiàn)次數(shù)(字段或問題):

陰謀:

http://img1.sycdn.imooc.com//61b1b1e9000177cc07380441.jpg

代碼:


如您所見,它不是很靈活,因?yàn)槟仨歡o.Bar為每個類別(銀行、警察等)添加一個對象。但如果上面的情節(jié)是你要找的,我也會整理出那部分。


# 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')


查看完整回答
反對 回復(fù) 2021-12-09
?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個贊

工作代碼片段,以防萬一這是您需要的:


import plotly.graph_objects as go


x=['a','b','c','d']

fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal',

                   base = 0, width = 0.2, offset = 0.0,

                   marker = dict(color = 'rgb(0,120,255)')))


fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa',

                width = 0.2, offset = -0.2,

                marker = dict(color = 'rgb(250,60,0)')))

fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto',

                 width = 0.2, offset = -0.2,

                 marker = dict(color = 'rgb(250,130,0)')))


fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})

fig.show()

替代布局: change: base,offset of second figure


import plotly.graph_objects as go


x=['a','b','c','d']

fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal',

                   base = 0, width = 0.2, offset = 0.0,

                   marker = dict(color = 'rgb(0,120,255)')))


fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa',

                width = 0.2, offset = -0.4, base=0,

                marker = dict(color = 'rgb(250,60,0)')))

fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto',

                 width = 0.2, offset = -0.2,

                 marker = dict(color = 'rgb(250,130,0)')))


fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})

fig.show()


查看完整回答
反對 回復(fù) 2021-12-09
  • 2 回答
  • 0 關(guān)注
  • 278 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號