如何使用plotly绘制不同类型的图表,并在python中生成_子图
我想画混合子图的饼图和条形图如何做呢 到目前为止,我能够绘制饼图,但如何添加条形图 下面的代码是执行一个groupby函数并迭代返回的对象,以便根据groupby对象的唯一值生成几个饼图 数据帧: 代码:如何使用plotly绘制不同类型的图表,并在python中生成_子图,python,pandas,plotly,bar-chart,pie-chart,Python,Pandas,Plotly,Bar Chart,Pie Chart,我想画混合子图的饼图和条形图如何做呢 到目前为止,我能够绘制饼图,但如何添加条形图 下面的代码是执行一个groupby函数并迭代返回的对象,以便根据groupby对象的唯一值生成几个饼图 数据帧: 代码: 导入plotly.graph\u objs as go 从plotly.subplot导入make_子地块 #此示例的数据 将plotly.express导入为px lst=列表(df.groupby('event_mohafaza')) #这里我们希望我们的网格是2 x 3 行数=2 col
导入plotly.graph\u objs as go
从plotly.subplot导入make_子地块
#此示例的数据
将plotly.express导入为px
lst=列表(df.groupby('event_mohafaza'))
#这里我们希望我们的网格是2 x 3
行数=2
cols=3
#大陆是l中的第一个元素
子地块标题=[l[0]表示lst中的l]
#你所做的事情的一个简洁而通用的版本
规范=[[{'type':'domain'}]*cols]*行
图=制作子图(
行=行,
cols=cols,
子地块标题=子地块标题,
规格=规格,
打印(网格=真)
对于枚举(lst)中的i,l:
#获取列和列的基本数学
行=i//cols+1
列=i%(行+1)+1
#这是每个大陆的数据框架
d=l[1]
图1添加_轨迹(
go.Pie(标签=d[“事件类型”],
值=d[“人数”],
hovertemplate=“%{label}:
值:%{Value}”,
showlegend=True,
textposition='inside',
旋转=90
),
行=行,
col=col
)
#图添加轨迹(go.Bar(y=df.event_类型,不透明度=0.3),1,1)
图更新布局(title=“按大陆划分的人口”,title\u x=0.5)
图2(图3)
我们修改了代码,理解为添加了第六个条形图。不清楚您要添加什么样的条形图,所以我已经按照您认为合适的方式设置了它。关键是您需要指定一个与子地块结构匹配的图形
import plotly.graph_objs as go
from plotly.subplots import make_subplots
# data for this example
import plotly.express as px
lst = list(df.groupby('event_mohafaza'))
# here we want our grid to be 2 x 3
rows = 2
cols = 3
# continents are the first element in l
subplot_titles = [l[0] for l in lst]
# a compact and general version of what you did
# specs = [[{'type':'domain'}]* cols] * rows
specs = [[{"type": "pie"},{"type": "pie"},{"type": "pie"}],[{"type": "pie"},{"type": "pie"},{"type": "bar"}]]
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,
print_grid=True)
for i, l in enumerate(lst):
# basic math to get col and row
row = i // cols + 1
col = i % (rows + 1) + 1
# this is the dataframe for every continent
d = l[1]
fig.add_trace(go.Pie(labels=d["event_type"],
values = d["number_person"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90),
row=row,
col=col
)
fig.add_trace(go.Bar(x=df.groups, y=df.number_person, opacity=0.3, showlegend=False), row=2, col=3)
fig.update_layout(title="Population by Continent", title_x=0.5)
fig.show()
导入plotly.graph\u objs as go
从plotly.subplot导入make_子地块
#此示例的数据
将plotly.express导入为px
lst=列表(df.groupby('event_mohafaza'))
#这里我们希望我们的网格是2 x 3
行数=2
cols=3
#大陆是l中的第一个元素
子地块标题=[l[0]表示lst中的l]
#你所做的事情的一个简洁而通用的版本
#规范=[[{'type':'domain'}]*cols]*行
规范=[[{“类型”:“饼”},{“类型”:“饼”},{“类型”:“饼”}],{“类型”:“饼”},{“类型”:“饼”},{“类型”:“条”}]]
图=制作子图(
行=行,
cols=cols,
子地块标题=子地块标题,
规格=规格,
打印(网格=真)
对于枚举(lst)中的i,l:
#获取列和列的基本数学
行=i//cols+1
列=i%(行+1)+1
#这是每个大陆的数据框架
d=l[1]
图添加跟踪(go.Pie)(标签=d[“事件类型”],
值=d[“人数”],
hovertemplate=“%{label}:
值:%{Value}”,
showlegend=True,
textposition='inside',
旋转=90),
行=行,
col=col
)
图添加轨迹(go.Bar(x=df.groups,y=df.number\u person,不透明度=0.3,showlegend=False),行=2,列=3)
图更新布局(title=“按大陆划分的人口”,title\u x=0.5)
图2(图3)
请提供一个数据框示例。@vestland我将编辑我的问题并添加数据框示例。我尝试了你的答案,我有几个问题,如果你能向我解释的话。我将尽我所知回答,但这是关于什么的?1:如何更改图表的大小?2:如何根据列的不同值的长度使行和列灵活。我有一个关于groupby函数的问题,因此我认为它与这个问题无关。我将打开另一个关于它的问题。你能帮我吗?你可以使用
fig.update\u布局(高度=800,宽度=1200,…)更改图形的大小
import plotly.graph_objs as go
from plotly.subplots import make_subplots
# data for this example
import plotly.express as px
lst = list(df.groupby('event_mohafaza '))
# here we want our grid to be 2 x 3
rows = 2
cols = 3
# continents are the first element in l
subplot_titles = [l[0] for l in lst]
# a compact and general version of what you did
specs = [[{'type':'domain'}]* cols] * rows
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,
print_grid=True)
for i, l in enumerate(lst):
# basic math to get col and row
row = i // cols + 1
col = i % (rows + 1) + 1
# this is the dataframe for every continent
d = l[1]
fig.add_trace(
go.Pie(labels=d["event_type"],
values = d["number_person"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90
),
row=row,
col=col
)
# fig.add_trace(go.Bar(y=df.event_type, opacity=0.3), 1, 1)
fig.update_layout(title="Population by Continent", title_x=0.5)
fig.show()
import plotly.graph_objs as go
from plotly.subplots import make_subplots
# data for this example
import plotly.express as px
lst = list(df.groupby('event_mohafaza'))
# here we want our grid to be 2 x 3
rows = 2
cols = 3
# continents are the first element in l
subplot_titles = [l[0] for l in lst]
# a compact and general version of what you did
# specs = [[{'type':'domain'}]* cols] * rows
specs = [[{"type": "pie"},{"type": "pie"},{"type": "pie"}],[{"type": "pie"},{"type": "pie"},{"type": "bar"}]]
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=subplot_titles,
specs=specs,
print_grid=True)
for i, l in enumerate(lst):
# basic math to get col and row
row = i // cols + 1
col = i % (rows + 1) + 1
# this is the dataframe for every continent
d = l[1]
fig.add_trace(go.Pie(labels=d["event_type"],
values = d["number_person"],
hovertemplate = "%{label}: <br>Value: %{value} ",
showlegend=True,
textposition='inside',
rotation=90),
row=row,
col=col
)
fig.add_trace(go.Bar(x=df.groups, y=df.number_person, opacity=0.3, showlegend=False), row=2, col=3)
fig.update_layout(title="Population by Continent", title_x=0.5)
fig.show()