【问题标题】:some subplots in Plotly loopsPlotly 循环中的一些子图
【发布时间】:2021-01-24 03:08:31
【问题描述】:

请帮忙,我需要在子图中获得 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()

我找到了一些 SO 链接,可能有帮助:
first
second

【问题讨论】:

    标签: python loops charts plotly


    【解决方案1】:

    解决方法很简单:

    y = local_df_2.values[0]
    

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2023-03-04
      • 2022-11-21
      • 2012-06-21
      • 2016-11-05
      • 2020-05-15
      • 2020-09-25
      • 2020-03-14
      • 2018-04-25
      相关资源
      最近更新 更多