plotly_express
plotly_express copied to clipboard
plotly loop in loop
trafficstars
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
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()