Bruno Peixoto
Bruno Peixoto

Reputation: 69

Stacked subplots with same legend color and labels

I have been trying to plot a stacked plot with the same legend color and non-duplicate labels, without much of a success.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Sample data
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 4, 8, 16]
y2 = [1, 3, 6, 10, 15]
y3 = [1, 4, 8, 12, 16]

# Create subplot figure with two subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=('Subplot 1', 'Subplot 2'))

# Add stacked area plot to subplot 1
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', stackgroup='one', name='A'), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', stackgroup='one', name='B'), row=1, col=1)

# Add stacked area plot to subplot 2
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', stackgroup='two', name='A'), row=1, col=2)
fig.add_trace(go.Scatter(x=x, y=y3, mode='lines', stackgroup='two', name='C'), row=1, col=2)

# Update layout
fig.update_layout(title_text='Stacked Area Plots on Subplots', showlegend=True)

# Show figure
fig.show()

The resulting plot is as follows:

enter image description here

Any help is appreciated.

Upvotes: 2

Views: 61

Answers (1)

UnicornOnAzur
UnicornOnAzur

Reputation: 185

You can set the fill color directly for each trace like so:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

# Sample data
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 4, 8, 16]
y2 = [1, 3, 6, 10, 15]
y3 = [1, 4, 8, 12, 16]

# Create subplot figure with two subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=("Subplot 1", "Subplot 2"))

# Add stacked area plot to subplot 1

fig.add_trace(go.Scatter(x=x,
                         y=y1,
                         line={"color":"blue"},
                         fillcolor="blue",
                         mode="lines",
                         stackgroup="one",
                         name="A",
                         legendgroup="A"),
              row=1,
              col=1)
fig.add_trace(go.Scatter(x=x,
                         y=y2,
                         line={"color":"red"},
                         fillcolor="red",
                         mode="lines",
                         stackgroup="one",
                         name="B"),
              row=1,
              col=1)

# Add stacked area plot to subplot 2
fig.add_trace(go.Scatter(x=x,
                         y=y1,
                         line={"color":"blue"},
                         fillcolor="blue",
                         mode="lines",
                         stackgroup="two",
                         name="A",
                         legendgroup="A",
                         showlegend=False),
              row=1,
              col=2)
fig.add_trace(go.Scatter(x=x,
                         y=y3,
                         line={"color":"red"},
                         fillcolor="red",
                         mode="lines",
                         stackgroup="two",
                         name="C"),
              row=1,
              col=2)
# Update layout
fig.update_layout(title_text="Stacked Area Plots on Subplots",
                  showlegend=True)
# Show figure
fig.show(renderer="browser") # or any other renderer

Or you can make a mapping like described [here].(https://community.plotly.com/t/automatically-pick-colors-when-using-add-trace/59075/2)

I've used the argument legendgroup to group them and showlegend=False to hide one. The argument for line makes the color of the line match the fill.

Upvotes: 1

Related Questions