确保绘图表达条形极坐标子图的格式一致

问题描述 投票:0回答:1

我想创建情节表达条极图的子图,其中每个图的布局规范都是相同的。

我采用了在此线程中找到的解决方案来构建 barpolular 子图:如何使用plotlyexpress 创建子图?

在此解决方案的输出中,每个子图的设置不同:角轴的方向因子图而异,并且角半径的标签/刻度值也不同。我正在尝试找到一种方法来覆盖这些参数,以便我可以确保两个子图具有相同的设置。

我尝试使用“fig.update_layout”来设置所有子图的格式,但这不起作用。

重现一个子图的示例数据:

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

这是我的代码:

    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

python plotly polar-coordinates
1个回答
0
投票

创建多个极坐标子图,每个子图的极轴数量与子图的数量相同。因此,需要

fig.update_layout(polar=...,polar2=...)

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()

© www.soinside.com 2019 - 2024. All rights reserved.