3

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()

image1

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}]
user9532692
  • 584
  • 7
  • 28
  • @DerekO Thanks! I'm leaving [a reference](https://plotly.com/python/facet-plots/?_gl=1*1fays9m*_ga*MTEzMjcwOTExMS4xNjcyNDMzNzEw*_ga_6G7EE0JNSC*MTY3Mjc2NzUxNC41LjEuMTY3Mjc2NzkwOS4wLjAuMA..#controlling-facet-spacing) on how to use `facet_row()` for anyone who's interested in the future. – user9532692 Jan 03 '23 at 18:09

0 Answers0