Python 在for循环中绘制位置为的多个饼图

Python 在for循环中绘制位置为的多个饼图,python,pandas,plotly,Python,Pandas,Plotly,我想用Plotly做一个2*7的子图 我想使用foor循环来迭代我的数据,为子图中的每个位置生成不同的饼图。 我面临两个问题,我不知道在迭代时如何给出位置。 我的体形上也没有任何东西,即使我称之为“show”方法 问题是您没有更改子批次的行和列。考虑到你没有提供答案,我举了一个例子 导入plotly.graph\u objs as go 从plotly.subplot导入make_子地块 #此示例的数据 将plotly.express导入为px df=px.data.gapminder().qu

我想用Plotly做一个2*7的子图

我想使用foor循环来迭代我的数据,为子图中的每个位置生成不同的饼图。 我面临两个问题,我不知道在迭代时如何给出位置。 我的体形上也没有任何东西,即使我称之为“show”方法


问题是您没有更改子批次的
。考虑到你没有提供答案,我举了一个例子

导入plotly.graph\u objs as go
从plotly.subplot导入make_子地块
#此示例的数据
将plotly.express导入为px
df=px.data.gapminder().query(“年份==2007”)
#我们希望每个大陆都有一个子地块
lst=列表(df.groupby(‘大陆’))
#这里我们希望我们的网格是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[“pop”],
showlegend=False,
textposition='inside',
textinfo='label+percent'),
行=行,
col=col
)
图更新布局(title=“按大陆划分的人口”,title\u x=0.5)
图2(图3)

请提供一份详细的报告。
import plotly.graph_objs as go
from plotly.subplots import make_subplots
labels = stock_by_portals_private.index

spec=[[{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, 
{'type':'domain'}, {'type':'domain'}],
  [{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, 
{'type':'domain'}, {'type':'domain'}]]

fig = make_subplots(rows=2, cols=7, specs=spec, print_grid=True)
i = 1
j = 1
for label in labels:
    private = stock_by_portals_private[stock_by_portals_private.index == label]['id']
    pro = stock_by_portals_pro[stock_by_portals_pro.index == label]['id']
    fig.add_trace(go.Pie(labels=['PRO', 'PRIVATE'], values=[pro , private ],\
                     name="Répartition des annonceurs par portail: " + str(label)), 1,1)

fig.update_traces(hoverinfo='label+percent+name', textinfo='none')
fig = go.Figure(fig)
fig.show()