【问题标题】:Plotly : How can I add a Scatterpolar over a Barpolar plot?Plotly:如何在 Barpolar 图上添加 Scatterpolar?
【发布时间】: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 : 我该如何解决这个问题?

【问题讨论】:

    标签: python plot plotly


    【解决方案1】:
    • 您的示例代码不包括构建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. 使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)"})
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2020-05-22
      • 2018-11-17
      • 2021-01-13
      • 2020-05-28
      • 2021-01-12
      • 2021-01-05
      相关资源
      最近更新 更多