简体   繁体   中英

How do I made a Scatterpolar subplot with multiple traces using plotly

Here is my code.

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

fig.add_trace(go.Scatterpolar(
      r = [df_wy['Successful defensive actions per 90'].mean(),df_wy['Duels per 90'].mean(),df_wy['Passes per 90'].mean(),df_wy['Short / medium passes per 90'].mean()],
      theta = ['Successful defensive actions per 90','Duels per 90','Passes per 90','Short / medium passes per 90'],
      fill = 'toself',
      name = 'Average Statistics of Position CB Player'),row=1, col=1)

fig.add_trace(go.Scatterpolar(
      r = [x['Successful defensive actions per 90'].values[0],x['Duels per 90'].values[0],x['Passes per 90'].values[0],x['Short / medium passes per 90'].values[0]],
      theta = ['Successful defensive actions per 90','Duels per 90','Passes per 90','Short / medium passes per 90'],
      fill = 'toself',
      name = x.Player.values[0]),row=1, col=1)

fig.add_trace(go.Scatterpolar(
      r = [df_wy['Duels won, %'].mean(),df_wy['Accurate passes, %'].mean(),df_wy["Accurate short / medium passes, %"].mean()],
      theta = ['Duels won, %','Accurate passes, %','Accurate short / medium passes, %'],
      fill = 'toself',
      name = 'Average Statistics of Position CB Player'),row=1, col=2)

fig.add_trace(ggo.Scatterpolar(
      r = [x['Duels won, %'].values[0],x['Accurate passes, %'].values[0],x["Accurate short / medium passes, %"].values[0]],
      theta = ['Duels won, %','Accurate passes, %','Accurate short / medium passes, %'],
      fill = 'toself',
      name = x.Player.values[0]),row=1, col=2)

fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
#offline.plot(fig,filename="subplots.html")
fig.show()

And I get an error like this.

The 'specs' argument to make_subplots must be a 2D list of dictionaries with dimensions (3 x 1). Received value of type <class 'list'>: [[{'type': 'polar'}], [{'type': 'polar'}], [{'type': 'polar'}], [{'type': 'polar'}]]

How do I figure this out?

  • have synthesized data to make your code run
  • two issues
    1. specs parameter needs to match dimensions of rows and cols parameters. Have used list comprehensions to construct a 3x2 list.
    2. your add_trace() parameters have row=1, col=2 but you had defined these as 3 and 1. Have changed to 3 and 2 to make your code work
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import numpy as np

df_wy = pd.DataFrame(
    {
        c: np.random.uniform(1, 20, 20)
        for c in [
            "Successful defensive actions per 90",
            "Duels per 90",
            "Passes per 90",
            "Short / medium passes per 90",
            "Duels won, %",
            "Accurate passes, %",
            "Accurate short / medium passes, %",
        ]
    }
).assign(Player=np.random.choice(list("ABCD"), 20))
x = df_wy.select_dtypes(include=np.number) * 1.1
x["Player"] = df_wy["Player"]

fig = make_subplots(
    rows=3,
    cols=2,
    specs=[[{"type": "polar"} for _ in range(2)] for _ in range(3)],
)

fig.add_trace(
    go.Scatterpolar(
        r=[
            df_wy["Successful defensive actions per 90"].mean(),
            df_wy["Duels per 90"].mean(),
            df_wy["Passes per 90"].mean(),
            df_wy["Short / medium passes per 90"].mean(),
        ],
        theta=[
            "Successful defensive actions per 90",
            "Duels per 90",
            "Passes per 90",
            "Short / medium passes per 90",
        ],
        fill="toself",
        name="Average Statistics of Position CB Player",
    ),
    row=1,
    col=1,
)

fig.add_trace(
    go.Scatterpolar(
        r=[
            x["Successful defensive actions per 90"].values[0],
            x["Duels per 90"].values[0],
            x["Passes per 90"].values[0],
            x["Short / medium passes per 90"].values[0],
        ],
        theta=[
            "Successful defensive actions per 90",
            "Duels per 90",
            "Passes per 90",
            "Short / medium passes per 90",
        ],
        fill="toself",
        name=x.Player.values[0],
    ),
    row=1,
    col=1,
)

fig.add_trace(
    go.Scatterpolar(
        r=[
            df_wy["Duels won, %"].mean(),
            df_wy["Accurate passes, %"].mean(),
            df_wy["Accurate short / medium passes, %"].mean(),
        ],
        theta=[
            "Duels won, %",
            "Accurate passes, %",
            "Accurate short / medium passes, %",
        ],
        fill="toself",
        name="Average Statistics of Position CB Player",
    ),
    row=1,
    col=2,
)

fig.add_trace(
    go.Scatterpolar(
        r=[
            x["Duels won, %"].values[0],
            x["Accurate passes, %"].values[0],
            x["Accurate short / medium passes, %"].values[0],
        ],
        theta=[
            "Duels won, %",
            "Accurate passes, %",
            "Accurate short / medium passes, %",
        ],
        fill="toself",
        name=x.Player.values[0],
    ),
    row=1,
    col=2,
)

fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
# offline.plot(fig,filename="subplots.html")
fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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