繁体   English   中英

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

[英]Plotly : How can I add a Scatterpolar over a Barpolar plot?

我正在尝试在 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 : 在此处输入图片说明 我怎样才能解决这个问题 ?

  • 您的示例代码不包括构建scatter polar 因此我使用了参考示例https://plotly.com/python/polar-chart/#polar-chart-with-plotly-express
  • 条形极坐标顶部创建散射极坐标轨迹
    1. 散点极坐标它自己的轴fig.update_traces(subplot="polar2")
    2. 确保两个极轴的相同update_layout({ax:{"domain":{"x":[0,1]}} for ax in ["polar","polar2"]})
    3. 使散射极坐标的背景透明。 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)"})

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM