簡體   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