pythonplotlypolar-coordinates

Ensuring consistent formatting across plotly express bar polar subplots


I would like to create subplots of plotly express barpolar plots whereby the layout specifications are the same for each plot.

I have taken a solution found in this thread to build the barpolar subploots: how can i create subplots with plotly express?

In the output from this solution, the setting of each subplots is different: the direction of the angular axes vary by subplot and the labels / tickvalues of the angular radius are also different. I am trying to find a way to override these parameters so that I can ensure both subplots have the same settings.

I have tried to use 'fig.update_layout' to set the formatting across all subplots but this doesn't work.

Example data to reproduce one subplot:

Age_months | Feed_Volume_ml |  Feed_Hour_Degrees
0              120             15
0              160             90
0              100             90
0              50             270
0              75             270
0              40             300

Here is my code:

    import pandas as pd
    import plotly.graph_objects as go
    import plotly.express as px
    from plotly.offline import plot
    from plotly.subplots import make_subplots

    figures = [
                px.bar_polar(fd.loc[fd['Age_months'] == 0].sort_values(by = ['Feed_Volume_ml']), 
                       r = "Feed_Volume_ml", 
                       theta = "Feed_Hour_Degrees", 
                       color = "Feed_Volume_ml",
                       title = "Clock Map of Feeding - Month 0",
                       direction = "clockwise",
                       start_angle = 90,
                       range_r = (0,220)),
               px.bar_polar(fd.loc[fd['Age_months'] == 5].sort_values(by = ['Feed_Volume_ml']), 
                       r = "Feed_Volume_ml", 
                       theta = "Feed_Hour_Degrees", 
                       color = "Feed_Volume_ml",
                       title = "Clock Map of Feeding - Month 5",
                           direction = "clockwise",
                           start_angle = 90,
                           range_r = (0,220) )
            ]

    fig = make_subplots(cols = len(figures), rows=1, 
                        specs= [[{"type": "barpolar"},{"type": "barpolar"}]]) 

    for i, figure in enumerate(figures):
        for trace in range(len(figure["data"])):
            fig.add_trace(figure["data"][trace], row=1, col=i+1,)
        
        
    fig.update_layout(
        polar = dict(
            radialaxis = dict(
                showticklabels = False),
            angularaxis = dict(
                tickvals = np.arange(0,360,15),
                ticktext = ticktexts,
                direction = "clockwise",
            )
        )
    )
        
    plot(fig)`

example subplot output


Solution

  • Multiple subplots of polar coordinates are created, each with as many polar axes as there are subplots. Therefore, fig.update_layout(polar=...,polar2=...) is required.

    import pandas as pd
    import plotly.graph_objects as go
    import plotly.express as px
    from plotly.offline import plot
    from plotly.subplots import make_subplots
    
    figures = [
                px.bar_polar(fd.loc[fd['Age_months'] == 0].sort_values(by = ['Feed_Volume_ml']), 
                       r = "Feed_Volume_ml", 
                       theta = "Feed_Hour_Degrees", 
                       color = "Feed_Volume_ml",
                       title = "Clock Map of Feeding - Month 0",
                       direction = "clockwise",
                       start_angle = 90,
                       range_r = (0,220)),
               px.bar_polar(fd.loc[fd['Age_months'] == 5].sort_values(by = ['Feed_Volume_ml']), 
                       r = "Feed_Volume_ml", 
                       theta = "Feed_Hour_Degrees", 
                       color = "Feed_Volume_ml",
                       title = "Clock Map of Feeding - Month 5",
                           direction = "clockwise",
                           start_angle = 90,
                           range_r = (0,220) )
            ]
    
    fig = make_subplots(cols = len(figures), rows=1, 
                        specs= [[{"type": "barpolar"},{"type": "barpolar"}]]) 
    
    for i, figure in enumerate(figures):
        for trace in range(len(figure["data"])):
            fig.add_trace(figure["data"][trace], row=1, col=i+1,)
        
    polar_dict = polar=dict(radialaxis=dict(showticklabels=False),
                            angularaxis = dict(tickvals = np.arange(0,360,15),
                                               ticktext = [f'{x}' for x in np.arange(0,360,15)],
                                               direction = "clockwise",)
                           )
    
    fig.update_layout(polar=polar_dict, polar2=polar_dict)
    
    fig.show()
    

    enter image description here