I am trying to draw two subplots (top and bottom) that contains category of 3 colors and values for each date. With the dataset below, I can draw a single plot with data_A (as shown below)
fig = px.line(data_A,
x="Date", y="Percent",
color='Color',
color_discrete_sequence=['green','red','gold'],
markers=True)
fig.show()
However, I can't seem to draw two plots using add_trace
from plotly.subplots import make_subplots
import plotly.express as px
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.05)
fig.add_trace(x = data_A['Date'],
y= data_A['Percent'],
# color='Color',
# color_discrete_sequence=['green','red','gold'],
markers=True)
fig.add_trace(x = data_B['Date'],
y= data_B['Percent'],
# color='Color',
# color_discrete_sequence=['green','red','gold'],
markers=True)
Here is the sample data:
data_A = [{'Date': '2022-01-01', 'Color': "Green", 'Percent': 40},
{'Date': '2022-01-01', 'Color': "Red", 'Percent': 20},
{'Date': '2022-01-01', 'Color': "Yellow", 'Percent': 30},
{'Date': '2022-01-02', 'Color': "Green", 'Percent': 45},
{'Date': '2022-01-02', 'Color': "Red", 'Percent': 30},
{'Date': '2022-01-02', 'Color': "Yellow", 'Percent': 25},
{'Date': '2022-01-03', 'Color': "Green", 'Percent': 40},
{'Date': '2022-01-03', 'Color': "Red", 'Percent': 20},
{'Date': '2022-01-03', 'Color': "Yellow", 'Percent': 30},
{'Date': '2022-01-04', 'Color': "Green", 'Percent': 45},
{'Date': '2022-01-04', 'Color': "Red", 'Percent': 25},
{'Date': '2022-01-04', 'Color': "Yellow", 'Percent': 30}]
data_B = [{'Date': '2022-01-01', 'Color': "Green", 'Percent': 30},
{'Date': '2022-01-01', 'Color': "Red", 'Percent': 50},
{'Date': '2022-01-01', 'Color': "Yellow", 'Percent': 20},
{'Date': '2022-01-02', 'Color': "Green", 'Percent': 65},
{'Date': '2022-01-02', 'Color': "Red", 'Percent': 10},
{'Date': '2022-01-02', 'Color': "Yellow", 'Percent': 25},
{'Date': '2022-01-03', 'Color': "Green", 'Percent': 40},
{'Date': '2022-01-03', 'Color': "Red", 'Percent': 30},
{'Date': '2022-01-03', 'Color': "Yellow", 'Percent': 20},
{'Date': '2022-01-04', 'Color': "Green", 'Percent': 55},
{'Date': '2022-01-04', 'Color': "Red", 'Percent': 35},
{'Date': '2022-01-04', 'Color': "Yellow", 'Percent': 10}]