使用 plotly express 绘制多个极线图

Plotting multiple polar line graphs using plotly express

我正在尝试使用 plotly express 在一个图中绘制 3 个极线图。虽然我可以让每个单独的图形工作,但在尝试将所有 3 个图形绘制为子图时出现错误。我是情节表达的新手,所以对子图的工作方式有点困惑。

下面是两个单独的尝试,每个尝试都有自己的错误。如果有人可以帮助解决其中的一个问题,我将不胜感激。

作为参考,我的 df 是这样的

Indicator   variable    value
0   Extreme Poverty Agriculture 0.154194
1   Extreme Poverty WASH    0.103432
2   Extreme Poverty Governance  0.383699
3   Extreme Poverty Demographics    0.164442
4   Extreme Poverty Education   0.017159
5   Extreme Poverty Conflict    0.177073

第一次尝试

fig, ax = plt.subplots(311)

#Figure 1
fig = px.line_polar(Xmelt,
                   r="value", 
                   theta="variable",
                   color="Indicator",
                   line_close=True,
                   range_r = [0, 0.4],
                   title ='2025',
                   width=800,
                   height=500,
                   color_discrete_sequence=('#520e05','#898989','#dbd91d'))
                   #color_discrete_sequence= px.colors.sequential.Plasma_r)
fig.update_layout(
    polar_radialaxis_ticksuffix='%',
    polar_angularaxis_rotation=90)


#Figure 2
fig = px.line_polar(Hmelt,
                   r="value", 
                   theta="variable",
                   color="Indicator",
                   line_close=True,
                   range_r = [0, 0.4],
                   title ='2025',
                   width=800,
                   height=500,
                   color_discrete_sequence=('#520e05','#898989','#dbd91d'))
                   #color_discrete_sequence= px.colors.sequential.Plasma_r)
fig.update_layout(
    polar_radialaxis_ticksuffix='%',
    polar_angularaxis_rotation=90)


#Figure 3
#ax3 = fig.add_subplot(133)
fig = px.line_polar(Cmelt, 
                   r="value", 
                   theta="variable",
                   color="Indicator",
                   line_close=True,
                   range_r = [0, 0.6],
                   title ='2025',
                   width=800,
                   height=500,
                   color_discrete_sequence=('#520e05','#898989','#dbd91d'))
                   #color_discrete_sequence= px.colors.sequential.Plasma_r)
fig.update_layout(
    polar_radialaxis_ticksuffix='%',
    polar_angularaxis_rotation=90)

尝试1:此处没有错误,但只出现最后一张图

第二次尝试

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=3, cols=1, specs=[{'type': 'polar'}])
trace1 = go.Scatterpolar(
                   r=Xmelt["value"], 
                   theta=Xmelt["variable"],
                   color=Xmelt["Indicator"],
                   line_close=True,
                   range_r = [0, 0.4],
                   title ='2025',
                   width=800,
                   height=500,
                   color_discrete_sequence=('#520e05','#898989','#dbd91d') , row=3, col=1)
                   #color_discrete_sequence= px.colors.sequential.Plasma_r)), 3, 1)                             
                            
trace2 = go.Scatterpolar(
                   r=Hmelt["value"],  
                   theta=Hmelt["variable"],
                   color=Hmelt["Indicator"],
                   line_close=True,
                   range_r = [0, 0.4],
                   title ='2025',
                   width=800,
                   height=500,
                   color_discrete_sequence=('#520e05','#898989','#dbd91d') , row=3, col=2)
                              
                              
trace3 = go.Scatterpolar(
                   r=Cmelt["value"], 
                   theta=Cmelt["variable"],
                   color=Cmelt["Indicator"],
                   line_close=True,
                   range_r = [0, 0.6],
                   title ='2025',
                   width=800,
                   height=500,
                   color_discrete_sequence=('#520e05','#898989','#dbd91d') , row=3, col=3)

fig.show()

尝试 2:此处的错误消息是:make_subplots 的 'specs' 参数必须是维度为 (3 x 1) 的二维字典列表。 收到类型 的值:[{'type':'polar'}]

谢谢

首先,你不需要指定plotly库吗,我不知道你是否可以在express中做子图。 其次,make_subplot的图形类型需要为列表格式,需要指定所需的图数。

第一次尝试:

import plotly.expess as px

第二次尝试:

fig = make_subplots(rows=3, cols=1, specs=[[{'type': 'polar'}]*3])