Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

plotly loop in loop #173

Open
ilyinss opened this issue Jan 23, 2021 · 1 comment
Open

plotly loop in loop #173

ilyinss opened this issue Jan 23, 2021 · 1 comment

Comments

@ilyinss
Copy link

ilyinss commented Jan 23, 2021

Hello. At first, thank you for Plotly. Its great.
Help me, if you can.

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

# here my plotly-try:

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

for i in test.index.levels[0]:
    local_df = test.loc[test.index.get_level_values(0)==i]
    trace = go.Bar(x = local_df.index.get_level_values(1),
                  y = local_df.values)
    test_fig.add_trace(trace)
    
test_fig.show()

plotly shows 3 subplots, but 2 of subplots are empty.
what am i doing wrong?

i would like to get 3 subplots: for each month in dataset

stackoverflow cant helps me.

thank you

p.s. sorry for my english speech - its not my natural

@ilyinss
Copy link
Author

ilyinss commented Jan 24, 2021

try to add second loop (but its not working again):

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

for i in test.index.levels[0]:
    local_df = test.loc[test.index.get_level_values(0)==i]
    
    for j in local_df.index.levels[1]:
        local_df_2 = local_df.loc[local_df.index.get_level_values(1)==j]
    
        trace = go.Bar(x = local_df_2.index.get_level_values(1),
                       y = local_df_2.values)

        test_fig.append_trace(trace, i-8, 1)
    
test_fig.show()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant