[英]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.