I've two plots created in the same figure using subplots
in plotly.
import plotly.graph_objects as go
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
df = pd.DataFrame(np.random.randint(0, 100, size=(20, 5)), columns=list('tABCD'))
df2 = pd.DataFrame(np.random.randint(0, 100, size=(20, 5)), columns=list('tABCD'))
fig = go.Figure()
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(
x=df.t,
y=df['A'],
name="1",
line_color='deepskyblue',
opacity=0.8,
legendgroup='group1'),
row=1, col=1
)
fig.add_trace(go.Scatter(
x=df.t,
y=df['B'],
name="2",
line_color='dimgray',
opacity=0.8,
legendgroup='group2'),
row=1, col=1
)
fig.add_trace(go.Scatter(
x=df.t,
y=df['C'],
name="3",
line_color='blue',
opacity=0.8,
legendgroup='group3'),
row=1, col=1
)
fig.add_trace(go.Scatter(
x=df.t,
y=df['D'],
name="4",
line_color='red',
opacity=0.8,
legendgroup='group4'),
row=1, col=1
)
fig.add_trace(go.Scatter(
x=df2.t,
y=df2['A'],
name="1",
line_color='deepskyblue',
opacity=0.8,
legendgroup='group1'),
row=1, col=2
)
fig.add_trace(go.Scatter(
x=df2.t,
y=df2['B'],
name="2",
line_color='dimgray',
opacity=0.8,
legendgroup='group2'),
row=1, col=2
)
fig.add_trace(go.Scatter(
x=df2.t,
y=df2['C'],
name="3",
line_color='blue',
opacity=0.8,
legendgroup='group3'),
row=1, col=2
)
fig.add_trace(go.Scatter(
x=df2.t,
y=df2['D'],
name="4",
line_color='red',
opacity=0.8,
legendgroup='group4'),
row=1, col=2
)
fig.write_html('ts.html', auto_open=True)
Output:
I want to share the same legends for both subplots. So, I tried specifying a legend group (ref). It works, but duplicate labels appear
Any suggestions on how to remove the duplicates will be helpful
Adding Traces New traces can be added to a plot_ly figure using the add_trace() method. This method accepts a plot_ly figure trace and adds it to the figure. This allows you to start with an empty figure, and add traces to it sequentially.
Overview. The plotly. express module (usually imported as px ) contains functions that can create entire figures at once, and is referred to as Plotly Express or PX. Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures.
Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments. New to Plotly? Plotly is a free and open-source graphing library for Python.
Adding showlegend = False
in the instance where df2 is accessed will remove the duplicate legends
fig.add_trace(go.Scatter(
x=df2.t,
y=df2['A'],
name="1",
line_color='deepskyblue',
opacity=0.8,
legendgroup='group1',
showlegend=False),
row=1, col=2
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With