[英]How to add a Pie chart and grouped Bar chart on plotly express subplots
以下是使用 plotly 快速子圖獲取餅圖和分組條形圖的語法
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
specs = [[{'type':'pie'}, {"type": "bar"}]]
fig = make_subplots(rows=1, cols=2, specs=specs, shared_yaxes = True,subplot_titles=['Pie Chart',
'Grouped Bar Chart'])
fig.add_trace(go.Pie(
labels = df_pie['index'],
values = df_pie['count'],
hole = 0.6,
marker_colors = ['#353837','#646665', '#8e9492', '#c9d1ce'],
), 1, 1)
fig.add_trace(go.Bar(
x = df_bar['Satisfaction'],
y = df_bar['count'],
base =df_bar['Gender'],
),1,2)
fig.update_layout(showlegend=False,
title=dict(text="Visualization",
font=dict(
family="Arial",
size=20,
color='#283747')
))
fig.show()
下面是我根據上面的代碼得到的結果,
我怎樣才能讓餅圖看起來像這樣
通過 plotly 表達子圖。
您可以通過將textinfo
用於餅圖來實現此目的。 對於條形圖,您需要創建要在每個條形上顯示的文本,然后使用text
來顯示它。 此外,當您正在尋找分組條形圖時,您將需要使用創建兩條軌跡,然后將其組合到子圖 - 1,2。 請注意, textposition=auto
將 select 以正確的方式顯示文本。 在條形的情況下,由於長度,它已經移動了要垂直顯示的文本。
由於沒有提供數據,我創建了一些基本數據。 希望這就是您要找的。
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
specs = [[{'type':'pie'}, {"type": "bar"}]]
fig = make_subplots(rows=1, cols=2, specs=specs, shared_yaxes = True, subplot_titles=['Pie Chart', 'Grouped Bar Chart'])
##My data creation##
df_pie=pd.DataFrame({'index':[1,2,3,4], 'count':[442,459,289,280]})
df_bar=pd.DataFrame({'Satisfaction': ['Excellent', 'Excellent', 'Good', 'Good', 'Poor', 'Poor', 'Neutral', 'Neutral'], 'count': [442, 459, 289, 280, 442, 459, 289, 280], 'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Female', 'Male', 'Female']})
fig.add_trace(go.Pie(
labels = df_pie['index'],
values = df_pie['count'],
hole = 0.6,
marker_colors = ['#353837','#646665', '#8e9492', '#c9d1ce'],
textinfo='percent+value', ## ADD - display both
), 1, 1)
## New column to get percentage of row across all Male/Females
df_bar['percentage'] = df_bar.groupby(['Gender'])['count'].transform(lambda z: round(z / z.sum() * 100))
## New column - text of count and percentage - how you need the annotation shown
df_bar['Text']=df_bar['count'].astype(str) + ',' + df_bar['percentage'].astype(str)+'%'
## Create individual traces for Male and Female
trace1 = go.Bar(
x=df_bar[df_bar.Gender == 'Male']['Satisfaction'],
y=df_bar[df_bar.Gender == 'Male']['count'],
name='Male',
text=df_bar[df_bar.Gender == 'Male']['Text'], ## Display text
textposition='auto',
)
trace2 = go.Bar(
x=df_bar[df_bar.Gender == 'Female']['Satisfaction'],
y=df_bar[df_bar.Gender == 'Female']['count'],
name='Female',
text=df_bar[df_bar.Gender == 'Male']['Text'], ## Display text
textposition='auto'
)
fig.append_trace(trace1, 1,2) ## Add as first set of bars in second subplot
fig.append_trace(trace2,1,2) ## Add as second set of bars in second subplot
##Removed your original code
#fig.add_trace(go.Bar(
# x = df_bar['Satisfaction'],
# y = df_bar['count'],
# base =df_bar['Gender'],
# ),1,2)
fig.update_layout(showlegend=False,
title=dict(text="Visualization",
font=dict(
family="Arial",
size=20,
color='#283747')
))
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.