Radar chart in plotly

Radar chart in plotly with line_polar

The line_polar function from plotly express allows creating radar charts, also known as spider plots, in Python. You will need to input the values to the r argument and the groups to theta, as column names of a data frame or as arrays. Note that you can set line_close to True in order to draw an extra line between the first and the last point, if needed.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True)

fig.show()

Markers

By default, the lines are displayed without markers, but you can add them by setting the markers argument to True.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    markers = True)

fig.show()

Filled radar chart

It is possible to fill the area inside the lines setting the fill argument from the update_traces function to 'toself'.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True)
fig.update_traces(fill = 'toself')

fig.show()

Labels

The line_polar function provides an argument named text where you can pass an array or the name of a column of a data frame to add labels to each point. In addition, you can customize the position of the labels with textposition. In the following example we are using the values as labels.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    text = 'value')
fig.update_traces(fill = 'toself', textposition = 'top center')

fig.show()

Color

The default blue color from plotly can be customized passing a color inside an array to the color_discrete_sequence argument, as in the example below.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    color_discrete_sequence = ['red'])
fig.update_traces(fill = 'toself')

fig.show()

Line type

The line style can be customized making use of the line_dash_sequence argument. You will need to input an array with a single element specifying the desired line type.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    line_dash_sequence = ['dash'])

fig.show()

Radar chart with several groups

Groups with different colors

It is possible to create a radar chart by group or individual with the line_polar function. You will need to have your data divided in several groups, individuals or categories and then you will be able to input that categorical variable to color. In the sample data frame of the following example the first five represent rows the first group while the last five represent the second.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10,
             12, 8, 13, 6, 5],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5',
               'V1', 'V2', 'V3', 'V4', 'V5'],
    group = ['A', 'A', 'A', 'A', 'A',
            'B', 'B', 'B', 'B', 'B']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    color = 'group')
fig.update_traces(fill = 'toself')

fig.show()

Note that you can customize the color for each group. There are two possible options: passing an array with as many colors as groups to color_discrete_sequence or passing a dictionary to color_discrete_map specifying a color for each group as in the example below.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10,
             12, 8, 13, 6, 5],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5',
               'V1', 'V2', 'V3', 'V4', 'V5'],
    group = ['A', 'A', 'A', 'A', 'A',
            'B', 'B', 'B', 'B', 'B']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    color = 'group', color_discrete_map = {'A': 'dodgerblue', 'B': 'gold'})
fig.update_traces(fill = 'toself')

fig.show()

Groups with different line types

An alternative to the previous is creating a radar chart where each group will have a different line style. For this purpose you will need to pass the categorical variable representing the groups to line_dash. You can customize the line type for each group with line_dash_sequence or with line_dash_map.

import plotly.express as px
import pandas as pd

# Sample data
df = pd.DataFrame(dict(
    value = [8, 12, 7, 14, 10,
             12, 8, 13, 6, 5],
    variable = ['V1', 'V2', 'V3', 'V4', 'V5',
               'V1', 'V2', 'V3', 'V4', 'V5'],
    group = ['A', 'A', 'A', 'A', 'A',
            'B', 'B', 'B', 'B', 'B']))
           
fig = px.line_polar(df, r = 'value', theta = 'variable', line_close = True,
                    line_dash = 'group')

fig.show()
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon

See also