Search code examples
python-3.xplotlyplotly-pythonradar-chartplotly-express

Add multiple lines in radar plot - python plotly


I am trying to create a radar plot in plotly. I am not sure how to add multiple lines to the same plot. I could find the add_trace option in plotly.graph_objects as go (https://plotly.com/python/radar-chart/). But I am not sure how to add multiple lines to the same plot while using px.line_polar in plotly_express.

    import plotly.express as px
    import pandas as pd

    df1 = pd.DataFrame(dict(
        r=[1, 5, 2, 2, 3],
        theta=['processing cost', 'mechanical properties', 'chemical stability',
               'thermal stability', 'device integration']))


    df2 = pd.DataFrame(dict(
        r=[.1, .5, .2, .2, .3],
        theta=['processing cost', 'mechanical properties', 'chemical stability',
               'thermal stability', 'device integration']))

    fig = px.line_polar(df1, r='r', theta='theta', line_close=True)
    fig.show()
    

Suggestions on how to include df2 in the same figure and also include corresponding legends will be really helpful.


Solution

  • To display multiple lines in a plotly.express ladder chart, it is possible to set the category variable to a color. For your data, add the model name for example to df1 and df2. If you set that model name to a color, you can create the following chart.

    df1['Model'] = 'Model_A'
    df2['Model'] = 'Model_B'
    df = pd.concat([df1,df2], axis=0)
    
    fig = px.line_polar(df, r='r', color='Model', theta='theta', line_close=True)
    
    fig.show()
    

    enter image description here