Plotly:如何在 Barpolar 图上添加 Scatterpolar?

Posted

技术标签:

【中文标题】Plotly:如何在 Barpolar 图上添加 Scatterpolar?【英文标题】:Plotly : How can I add a Scatterpolar over a Barpolar plot? 【发布时间】:2021-12-11 16:46:52 【问题描述】:

我正在尝试在 Barpolar 图上绘制 Scatterpolar 图(Barpolar 用于构建 Scatterpolar 图的布局)。

我正在使用以下代码来绘制图形:

########################################################################
# Make background layout
########################################################################
# https://plotly.github.io/plotly.py-docs/generated/plotly.graph_objects.Barpolar.html
layout_radius = 2.8
num_slices = 5
theta = [(i + 1.5) * 360 / num_slices for i in range(num_slices)]
width = [360 / num_slices for _ in range(num_slices)]
vals = [layout_radius for _ in range(num_slices)]
# blue = rgba(50, 144, 196, 0.2) = Governance
# orange = rgba(183, 87, 39, 0.2) = Design
# yellow = rgba(217, 166, 25, 0.2) = Implementation
# green = rgba(55, 121, 62, 0.2) = Verification
# brown = rgba(121, 31, 23, 0.2) = Operations
colors = ['rgba(50, 144, 196, 0.2)', 'rgba(183, 87, 39, 0.2)', 'rgba(217, 166, 25, 0.2)',
          'rgba(55, 121, 62, 0.2)', 'rgba(121, 31, 23, 0.2)']
labels = ["Governance",  "Design", "Implementation",
          "Verification", "Operations"]
barpolar_plots = [go.Barpolar(r=[r], theta=[t], width=[w], name=n, marker_color=[c])
                  for r, t, w, n, c in zip(vals, theta, width, labels, colors)]
layout = go.Figure()

# Align backgorund
angular_tickvals = [(i + 1) * 360 / num_slices for i in range(num_slices)]
layout.update_layout(polar_angularaxis_tickvals=angular_tickvals)
# Remove legends
layout_options = 
layout_options["showlegend"] = True
layout_options["polar_angularaxis_showticklabels"] = False
layout.update_layout(**layout_options)
########################################################################
########################################################################
# Add plots on Layout
########################################################################
layout.add_traces(barpolar_plots)

layout.add_traces([go.Scatterpolar(r=current_maturity_score_dataframe,
                                   theta=maturity_score_labels_dataframe),
                   go.Scatterpolar(r=latest_maturity_score_dataframe,
                                   theta=maturity_score_labels_dataframe)])

在渲染图像上,我看到 Plotly 将两个图都添加到图例中,但 Scatterpolar 没有出现:

我还能够在没有 Barpolars 的情况下绘制 Scatterpolars : 我该如何解决这个问题?

【问题讨论】:

【参考方案1】: 您的示例代码不包括构建scatter polar。因此我使用了参考示例https://plotly.com/python/polar-chart/#polar-chart-with-plotly-express 在极坐标顶部创建散射极坐标轨迹
    散射极轴它自己的轴fig.update_traces(subplot="polar2") 确保两个极轴的相同update_layout(ax:"domain":"x":[0,1] for ax in ["polar","polar2"]) 使scatter polar的背景透明。 update_layout(polar2="bgcolor":"rgba(0,0,0,0)")

完整代码如下

import plotly.graph_objects as go
import plotly.express as px

########################################################################
# Make background layout
########################################################################
# https://plotly.github.io/plotly.py-docs/generated/plotly.graph_objects.Barpolar.html
layout_radius = 2.8
num_slices = 5
theta = [(i + 1.5) * 360 / num_slices for i in range(num_slices)]
width = [360 / num_slices for _ in range(num_slices)]
vals = [layout_radius for _ in range(num_slices)]
# blue = rgba(50, 144, 196, 0.2) = Governance
# orange = rgba(183, 87, 39, 0.2) = Design
# yellow = rgba(217, 166, 25, 0.2) = Implementation
# green = rgba(55, 121, 62, 0.2) = Verification
# brown = rgba(121, 31, 23, 0.2) = Operations
colors = ['rgba(50, 144, 196, 0.2)', 'rgba(183, 87, 39, 0.2)', 'rgba(217, 166, 25, 0.2)',
          'rgba(55, 121, 62, 0.2)', 'rgba(121, 31, 23, 0.2)']
labels = ["Governance",  "Design", "Implementation",
          "Verification", "Operations"]
barpolar_plots = [go.Barpolar(r=[r], theta=[t], width=[w], name=n, marker_color=[c])
                  for r, t, w, n, c in zip(vals, theta, width, labels, colors)]
layout = go.Figure()

# Align backgorund
angular_tickvals = [(i + 1) * 360 / num_slices for i in range(num_slices)]
layout.update_layout(polar_angularaxis_tickvals=angular_tickvals)
# Remove legends
layout_options = 
layout_options["showlegend"] = True
layout_options["polar_angularaxis_showticklabels"] = False
layout.update_layout(**layout_options)
########################################################################
########################################################################
# Add plots on Layout
########################################################################
layout.add_traces(barpolar_plots)

df = px.data.wind()
fig = px.scatter_polar(df, r="frequency", theta="direction")
fig.update_traces(subplot="polar2")

layout = layout.add_traces(fig.data).update_layout(ax:"domain":"x":[0,1] for ax in ["polar","polar2"])
layout.update_layout(polar2="bgcolor":"rgba(0,0,0,0)")

【讨论】:

以上是关于Plotly:如何在 Barpolar 图上添加 Scatterpolar?的主要内容,如果未能解决你的问题,请参考以下文章

如何在绘图条形图上左对齐文本(包含示例图像)[Plotly-Dash]

Plotly:如何在子图上绘制烛台图?

Plotly:如何使用 plotly.graph_objects 将两个 3D 图放在同一个图上?

Plotly:如何对条形图上显示的“文本”值求和?

Plotly 散点图趋势线出现在散点下方。如何让趋势线出现在散点图上? [Python]

在条形图上的 Plotly 中更改轴/图例名称