J'essaie de tracer plusieurs graphiques à l'aide de plotly et de make_subplot où je souhaite tracer 5 graphiques circulaires créés à l'aide de la boucle d'itération for. Et 1 graphique à barres où le graphique à barres prendra colspan = 2

Exemple de trame de données:

eventtype_name    date    eventmohafaza_name   number       groups
watch movie     8/14/2020   loc1                 25         group1
stay at home    8/13/2020   loc2                 20         group2
swimming        8/13/2020   loc2                 25         group4
camping         8/13/2020   loc3                 20         group3
meeting         8/13/2020   loc4                 40         group5
meeting         8/13/2020   loc5                 20         group2
watch movie     8/14/2020   loc4                 25         group1
stay at home    8/13/2020   loc1                 20         group2
swimming        8/13/2020   loc5                 25         group4
camping         8/13/2020   loc4                 20         group3
meeting         8/13/2020   loc3                 30         group5
meeting         8/13/2020   loc5                 33         group2

Le problème réside dans le fait que lorsque j'affecte les lignes et les colonnes et que j'essaie d'utiliser le paramètre specs, le système plante et affiche l'erreur ci-dessous.

This is the format of your plot grid:
[ (1,1)     ]  [ (1,2)     ]  [ (1,3)     ]
[ (2,1)     ]  [ (2,2)     ]  [ (2,3)     ]
[ (3,1)     ]  [ (3,2) x,y           -    ]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
f:\aienv\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
   1318     try:
-> 1319         subplot_refs = grid_ref[row - 1][col - 1]
   1320     except IndexError:

IndexError: list index out of range

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-108-831d326e87b8> in <module>
     59                          rotation=90),
     60      row=row,
---> 61      col=col
     62     )
     63 

f:\aienv\lib\site-packages\plotly\basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
   1650             rows=[row] if row is not None else None,
   1651             cols=[col] if col is not None else None,
-> 1652             secondary_ys=[secondary_y] if secondary_y is not None else None,
   1653         )
   1654 

f:\aienv\lib\site-packages\plotly\basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
   1764         if rows is not None:
   1765             for trace, row, col, secondary_y in zip(data, rows, cols, secondary_ys):
-> 1766                 self._set_trace_grid_position(trace, row, col, secondary_y)
   1767 
   1768         # Make deep copy of trace data (Optimize later if needed)

f:\aienv\lib\site-packages\plotly\basedatatypes.py in _set_trace_grid_position(self, trace, row, col, secondary_y)
   1847         grid_ref = self._validate_get_grid_ref()
   1848         return _set_trace_grid_reference(
-> 1849             trace, self.layout, grid_ref, row, col, secondary_y
   1850         )
   1851 

f:\aienv\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
   1320     except IndexError:
   1321         raise Exception(
-> 1322             "The (row, col) pair sent is out of "
   1323             "range. Use Figure.print_grid to view the "
   1324             "subplot grid. "

Exception: The (row, col) pair sent is out of range. Use Figure.print_grid to view the subplot grid. 

Code:

import plotly.graph_objs as go
from plotly.subplots import make_subplots
import plotly.express as px
import pandas as pd



df = pd.read_excel(file)
lst = list(df.groupby('eventmohafaza_name'))


rows = 3
cols = 3

subplot_titles = [l[0] for l in lst]


specs=[
           [{"type": "pie"},{"type": "pie"},{"type": "pie"}],
           [{"type": "pie"},{"type": "pie"},{"type": "pie"}],
           [{"type": "pie"},{"type": "bar",'colspan':2},None],
      ]

    
fig = make_subplots(
        rows=rows,
        cols=cols,
        subplot_titles=subplot_titles,
        specs=specs,
        
        print_grid=True
)

for i, l in enumerate(lst):
    row = i // cols + 1
    col = i % (rows + 1) + 1
    d = l[1]
   
    fig.add_trace(go.Pie(labels=d["eventtype_name"],
                         values = d["number"],
                         hovertemplate = "%{label}: <br>Value: %{value} ",
                         showlegend=True,
                         textposition='inside',
                         rotation=90),
     row=row,
     col=col    
    )
    
group_event_groups = df.groupby([df['eventmohafaza_name'],df['eventtype_name']]).size().unstack()    
for x in group_event_groups.columns: 
    fig.add_trace(go.Bar(name=str(x), x=group_event_groups.index, y=group_event_groups[x],showlegend=False), row=3, col=2)
    
fig.update_layout(height = 1000,
                  width = 900,
                  autosize = False,
                  title="Population by Continent", 
                  title_x=0.5)
fig.show()

le résultat attendu est de 5 graphiques à secteurs et un graphique à barres

1
Lelo Lelo 6 nov. 2020 à 10:17

1 réponse

Meilleure réponse

Ajout de nouvelles données d'attribut aux exemples de données et modification de la logique de calcul pour les lignes et les colonnes. De plus, le dernier graphique à barres est désormais un graphique unique car le nombre de graphiques ne correspond pas aux données en boucle.

lst = list(df.groupby('eventmohafaza_name'))

rows = 3
cols = 3

subplot_titles = [l[0] for l in lst]

specs=[[{"type": "pie"},{"type": "pie"},{"type": "pie"}],
       [{"type": "pie"},{"type": "pie"},{"type": "pie"}],
       [{"type": "pie"},{"type": "bar",'colspan':2},None]]

fig = make_subplots(
        rows=rows,
        cols=cols,
        subplot_titles=subplot_titles,
        specs=specs,  
        print_grid=True
)

for i, l in enumerate(lst):
    row = i // cols + 1
    col = (i % rows) + 1
    d = l[1]
    fig.add_trace(go.Pie(labels=d["eventtype_name"],
                         values = d["number"],
                         hovertemplate = "%{label}: <br>Value: %{value} ",
                         showlegend=True,
                         textposition='inside',
                         rotation=90),
     row=row,
     col=col    
    )
    
group_event_groups = df.groupby([df['eventmohafaza_name'],df['eventtype_name']]).size().unstack()    
# for x in group_event_groups.columns: 
fig.add_trace(go.Bar(name='camping', x=group_event_groups.index, y=group_event_groups.columns, showlegend=False), row=3, col=2)
    
fig.update_layout(height = 1000,
                  width = 900,
                  autosize = False,
                  title="Population by Continent", 
                  title_x=0.5)
fig.show()

enter image description here

2
r-beginners 6 nov. 2020 à 09:54