Friday, December 27, 2024
Google search engine
HomeLanguagesHow to set up multiple subplots with grouped legends using Plotly in...

How to set up multiple subplots with grouped legends using Plotly in Python?

Prerequisites: Python Plotly

In this article, we will explore how to set up multiple subplots with grouped legends using Plotly in Python.

It is a useful approach to demonstrate legend for a plot as it allows to reveal a large amount of information about complex information. A legend in the plotly library basically describes the graph elements. One of the most deceptively-powerful features of Plotly data visualization is the ability for a viewer to quickly analyze a sufficient amount of information about data when pointing the cursor over the point label appears.

Syntax: plotly.graph_objects.Scatter(x=data, y=data, legendgroup=str,  showlegend=bool)

Parameters:

  • x: data of x-axis
  • y: data of y-axis
  • legendgroup: name of the legend
  • showlegend: True(default)/False

Example 1:

Here, Two stacked subplot with 3 grouped legends with the help of one of the parameters that are showlegend: True/False. 

for row=1 , col=1 , showlegend: True
for row=1 , col=1, showlegend: True
for row=2 , col=1 , showlegend: False
for row=2 , col=1 , showlegend: True

So 3 different grouped legend is shown.

Python3




from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly import offline
 
fig = make_subplots(rows=2, cols=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue')),
              row=1, col=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 60],
                         name="2018", legendgroup="2018",
                         line=dict(color='red')),
              row=1, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue'),
                         showlegend=False
                         ),
              row=2, col=1)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='green'),
                            showlegend=True),
                 row=2, col=1)
 
 
fig.update_layout(height=600, width=600,
                  title_text="GeeksforLazyroar - Stacked Subplots")
fig.show()


Output:

Example 2:

Here, Four stacked subplot with 4 grouped legends.

for row=1 , col=1 , showlegend: True
for row=1 , col=1, showlegend: True
for row=1 , col=2, showlegend: False
for row=1 , col=2 , showlegend: True
for row=2 , col=1 , showlegend: False
for row=2 , col=2 , showlegend: True

So 4 different grouped legend is shown.

Python3




from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly import offline
 
fig = make_subplots(rows=2, cols=2)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue')),
              row=1, col=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 60],
                         name="2018", legendgroup="2018",
                         line=dict(color='red')),
              row=1, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue'),
                         showlegend=False
                         ),
              row=1, col=2)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='yellow'),
                            showlegend=True),
                 row=1, col=2)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='red'),
                            showlegend=False),
                 row=2, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='pink'),
                         showlegend=True
                         ),
              row=2, col=2)
 
 
fig.update_layout(height=600, width=600,
                  title_text="Stacked Subplots")
fig.show()


Output:

RELATED ARTICLES

Most Popular

Recent Comments