Hello,
I’m trying to display a line chart with one line, and I’d like for that chart to display a label / legend for that line the same way it would if there were multiple lines.
Here’s a quick example:
CHART WITH LABELS
import pandas as pd
import numpy as np
import plotly.graph_objects as go
dates = pd.date_range(start='2020-01-01', periods=100)
random = np.random.RandomState(0)
random2 = np.random.RandomState(1)
data1 = random.random(100)
data2 = random2.random(100)
a_values = ['A' for i in range(100)]
b_values = ['B' for i in range(100)]
df1 = pd.DataFrame({
'data': data1,
'value': a_values
}, index=dates)
df2 = pd.DataFrame({
'data': data2,
'value': b_values
}, index=dates)
df = pd.concat([df1, df2])
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index, y=df[df.value == 'A']['data'], name='Label 1'))
fig.add_trace(go.Scatter(x=df.index, y=df[df.value == 'B']['data'], name='Label 2'))
fig.show()
This will yield the following chart:
My issue is that I’d like the same types of labels to appear in the legend even if there’s one line.
If I re-create the same chart but with only one line I’ll get the following:
CHART WITHOUT LABELS
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index, y=df[df.value == 'A']['data'], name='Label 1'))
fig.show()
What can I do to get the value ‘Label 1’ to appear on the legend on the right hand side?