A custom layout for Barpolar in Plotly/Dash

Hi community,

I am trying to figure out a change in a barpolar plot layout. I have included the example (below) where I would like to change the left plot layout to the one on the right:

I have also included a reproducible code example for the left plot:


rs = [10, 20, 30, 40, 50, 60, 70, 80]
thetas= ["B_right", "A2",  "A1",  "A3", "B_left","C1", "C2", "C3"]
fills = ["red", "red", "grey", "blue", "red", "grey", "blue", "red"]

fig = go.Figure()
fig.add_trace(
    go.Barpolar(
        r=rs,
        theta=thetas,
        marker_color=fills
    )
)

Any ideas how to achieve the right plot’s layout? I would like the bar of A fill the entire β€œdesignated” area (i.e., along the vertical axis of A) and preserve the shape of the plot.

Thanks!

Hi @loomaisand ,

As I understand what you mean.

You need to change theta value with list of degrees where the bar will be positioned.

After doing that the delta theta will not the same, and you can set custom width of each bar to make certain bar wider.

fig.update_traces(
                    r=[10, 90, 50, 60, 70, 80],
                    # set theta as degrees
                    theta = [0,90,180,225,270,315],
                    # set color of each bar
                    marker_color= ["red", "red", "red", "grey", "blue", "red"],
                    # set custom width of each bar
                    width=[40, 120, 40, 40, 40, 40],
                    selector=dict(type='barpolar'))

Replace the tick from degrees to text with update_layout

fig.update_layout(
    polar=dict(
        angularaxis=dict(
            # set custom tick 
            tickvals=[0,90,180,225,270,315],
            # set custom tick text
            ticktext=["B_right", "A", "B_left","C1", "C2", "C3"]
        )
    )
)

Here is the code, when you put together:

import plotly.graph_objects as go 

rs = [10, 20, 30, 40, 50, 60, 70, 80]
thetas= ["B_right", "A2",  "A1",  "A3", "B_left","C1", "C2", "C3"]
fills = ["red", "red", "grey", "blue", "red", "grey", "blue", "red"]

fig = go.Figure()
fig.add_trace(
    go.Barpolar(
        r=rs,
        theta=thetas,
        marker_color=fills
    )
)

fig.update_traces(
                    r=[10, 90, 50, 60, 70, 80],
                    # set theta as degrees
                    theta = [0,90,180,225,270,315],
                    # set color of each bar
                    marker_color= ["red", "red", "red", "grey", "blue", "red"],
                    # set custom width of each bar
                    width=[40, 120, 40, 40, 40, 40],
                    selector=dict(type='barpolar'))

fig.update_layout(
    polar=dict(
        angularaxis=dict(
            # set custom tick 
            tickvals=[0,90,180,225,270,315],
            # set custom tick text
            ticktext=["B_right", "A", "B_left","C1", "C2", "C3"]
        )
    )
)

fig.show()

1 Like

Thanks @farispriadi - works as expected!