How to subplot radar charts with 2 traces each

I want to make a radar chart for each row in a data frame,
and each chart should have 2 data traces each as a filled line (different colors)

Can I do this somehow by faceting on the timestamp I have/row, or will I need to write 2 fig.add_trace() per chart? I’m getting the funny output with my code like below.

  1. trace 1 should be columns in index position 1-5 and color blue for example
  2. trace 2 columns 6-10 and color red

Both sets of columns have the same names due to the way the data comes in from a form, matching the categories here for each axis and label outside. My thought process was to use the column indexes.

Here’s a screenshot of what I’m getting. I’m calling the fig in a Dash component if that matters: dcc.Graph(figure=fig)

data values come from an API but basically, look like this:

values = {'Timestamp':['5/20/2022 19:44:43', '5/20/2022 20:14:36', '5/20/2022 20:14:31'],
        'Design':[6, 4, 6],
        'cat002':[6, 3, 6],
        'cat003':[6, 6, 5],
        'cat004':[6, 2, 6],
        'cat005':[4, 1, 5],
        'Design':[3, 6, 6],
        'cat002':[4, 3, 6],
        'cat003':[6, 6, 5],
        'cat004':[5, 2, 6],
        'cat005':[5, 6, 6]
        }
# store values in dataframe
df = pd.DataFrame(values, columns=values[0])

### build the radar graph figure
categories = ['cat1','cat2','cat3','cat4','cat5']

fig = go.Figure()

fig.add_trace(go.Scatterpolar(
      r=df.iloc[1:2, 1:5],
      theta=categories,
      fill='toself',
      name='Value',
    #   facet_col='Timestamp'
))
fig.add_trace(go.Scatterpolar(
      r=df.iloc[1:2, 6:10],
      theta=categories,
      fill='toself',
      name='Strength',
    #   facet_col='Timestamp'
))

fig.update_layout(
  polar=dict(
    radialaxis=dict(
      visible=True,
      range=[0, 6]
    )),
  showlegend=False
)

I can make a single chart easily with the px polar options, but I’m having trouble connecting all the documentation to multiples with multiple traces. I’m still not sure which way to go after reading the polar and radar docs.

It seems that there are no faceting options with polar traces and plotly express (yet). In the meantime you’ll need to use graph_objects.

Here’s a manual example:

from plotly.subplots import make_subplots

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

fig.add_trace(
    go.Scatterpolar(
        theta=["cat002", "cat003", "cat004", "cat005", "cat002"],
        r=[5,6,3,8,5],
    ),
    row=1,
    col=1,
)
fig.add_trace(
    go.Scatterpolar(
        theta=["cat002", "cat003", "cat004", "cat005", "cat002"],
        r=[3,9,4,5,3]
    ),
    row=1,
    col=1,
)
fig.add_trace(
    go.Scatterpolar(
        theta=["cat002", "cat003", "cat004", "cat005", "cat002"],
        r=[5,6,3,8,5],
    ),
    row=1,
    col=2,
)
fig.add_trace(
    go.Scatterpolar(
        theta=["cat002", "cat003", "cat004", "cat005", "cat002"],
        r=[3,9,4,5,3]
    ),
    row=1,
    col=2,
)

1 Like

Thank you @RenaudLN for writing this out. I was afraid this was the case. Now I’m hoping I don’t get too many form responses. I’m going to need a copy/paste robot for this project! :mechanical_arm: