Plotly中的一些子图循环

3

请帮忙,我需要在子图中获取一个月的3个子图。 在我的数据框中,我得到了多层索引(月份和名称)和值列, 我想将这些数据放入循环中并绘制Plotly图表。
如果您能帮忙,谢谢。

import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go

my_months = [9,9,9,10,10,10,11,11,11]
my_vls = [10, 20, 30, 20, 15, 25, 20, 30, 20]
my_names = ['one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three',]
test = pd.DataFrame(zip(my_months, my_names, my_vls), columns=['month','names', 'values'])
test = test.groupby(['month', 'names']).sum()

test_fig = make_subplots(
    rows = len(test.index.levels[0]),
    cols = 1)

months = test.index.levels[0].unique()

for i, month in months:
    vals = test.loc[test.index.get_level_values(0)==month]['values'].unique()
    for j in local_df.index.levels[1]:
        local_df_2 = local_df.loc[local_df.index.get_level_values(1)==j]
        test_fig.append_trace(
            go.Bar(
                x = local_df_2.index.get_level_values(1),
                y = local_df_2.values), 
            row=i+1, 
            col=1)
        
test_fig.show()

我找到了一些Stack Overflow链接,可能会有所帮助:
第一个链接
第二个链接
1个回答

0

解决方案非常简单:

y = local_df_2.values[0]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接