Search code examples
pythonplotplotlydata-visualizationplotly-dash

Categorical Scatter Plot with Dash/Plotly


I am trying to make a categorical scatter plot like this:

Counts of elements in discrete Locations(Wells) vs Time Points

This is supposed to be a categorical scatter plot with discrete x values represented by counts of elements in locations(Wells) vs Time Point.

I have written this code:

df = pd.read_excel (r'file.xlsx')

fig = go.Figure()
fig.add_trace(
  go.Scatter(
    x = [df['Well A'], df['Well B'],df['Well C'],df['Well D']],
    y = df['Time Point'],
    mode='markers'
  )
)

And I get this as a result:

Result

Which is crazy, I have no idea what is even happening there. Could you please help?..

P.S. My df looks like this:

Dataset

Please, help :(


Solution

  • If this is your desired result:

    enter image description here

    for c in df.columns[1:]:
        fig.add_trace(go.Scatter(x = df['Time Point'], y = df[c],
                                 mode = 'markers',
                                 name = c
                                )
                     )
    

    Complete code:

    import pandas as pd
    import numpy as np
    import plotly.graph_objects as go
    import plotly.express as px
    
    obs = 8
    df = pd.DataFrame({'Time Point': [1,2,3,4,5,6,7,8],
                       'Well A':np.random.randint(10, size=8),
                       'Well B':np.random.randint(10, size=8),
                       'Well C':np.random.randint(10, size=8),
                       'Well D':np.random.randint(10, size=8)})
    
    fig = go.Figure()
    for c in df.columns[1:]:
        fig.add_trace(go.Scatter(x = df['Time Point'], y = df[c],
                                 mode = 'markers',
                                 name = c
                                )
                     )
    fig.show()