Custom color for plotly python radar chart labels

84 Views Asked by At

I want to colorize each label_column with label_color using fig.update_polars(angularaxis_color=color) or fig.update_polars(angularaxis=dict(color=color)) but I can't.

How can I color each column_label the same color as the label_color? I want aa to be written in pink, bb in blue, cc in pink and dd in blue.

color_palette = {'Target': 'red', 'Current': 'blue'}


fig = px.line_polar(answers_avg, 
                    r = 'value',
                    theta = 'label_column', 
                    line_close = True, 
                    line_dash = 'group')


for group, color in color_palette.items():
    fig.update_traces(
        line=dict(color=color),
        selector=dict(name=group))
    
fig.show()

enter image description here

1

There are 1 best solutions below

0
On

Yeah just update the color of the axis label via fig.layout.polar.angularaxis.color. But this changes the color of all axis labels for that trace so if you want to have two different colors then you'll have to plot your chart using two different traces each specifying their axis label color.

import plotly.express as px
import pandas as pd

color_palette = {'Target': 'red', 'Current': 'blue'}
answers_avg = pd.DataFrame([
    dict(label_column='aa', label_color='pink', group='Current', value=3.),
    dict(label_column='aa', label_color='pink', group='Target', value=4.538462),
    dict(label_column='bb', label_color='blue', group='Current', value=2.818182),
    dict(label_column='bb', label_color='blue', group='Target', value=4.5),
    dict(label_column='cc', label_color='pink', group='Current', value=3.230769),
    dict(label_column='cc', label_color='pink', group='Target', value=4.25),
    dict(label_column='dd', label_color='blue', group='Current', value=2.923077),
    dict(label_column='dd', label_color='blue', group='Target', value=4.583333),
])

fig = px.line_polar(answers_avg, 
                    r = 'value',
                    theta = 'label_column', 
                    line_close = True, 
                    line_dash = 'group')


for group, color in color_palette.items():
    fig.update_traces(
        line=dict(color=color),
        selector=dict(name=group))
    
fig.layout.polar.angularaxis.color = 'blue'    
fig.show()

enter image description here