pythonplotly

Basic stacked data point histogram in Plotly Express


I have such an unbelievably basic question that I can't figure out and it's making me mad.

How can I plot a simple graph of stacked data points as X's in Plotly Express? I have tried using px.histogram but can't get it styled the way I want.

Example sketch below. X-values 1.5 and 4 both have three observations.

Edit #1 to add MWE

 import pandas as pd
 import plotly.express as px

 d = {'Name':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], 
      'Score':[1, 1.5, 4, 1.5, 1.5, 4, 2.5, 3, 2.5, 4]}

 df = pd.DataFrame(data=d)
 df

  Name  Score
0   A   1.0
1   B   1.5
2   C   4.0
3   D   1.5
4   E   1.5
5   F   4.0
6   G   2.5
7   H   3.0
8   I   2.5
9   J   4.0

sketch of histogram on a post-it note

Edit #2 to add Galton board diagram as example of desired output:

Galton board


Solution

  • If you want to process the histogram representation, you can do so with scatter plots. To process the data, add the frequency counts to the columns. Specify the frequency on the y-axis. After that, you can change the color and shape of the markers to your liking. I set the interval value for the y-axis and limit the height of the graph so that the elements overlap in order to have a stacked appearance on the scatter plot. I also add a hide y-axis.

    import pandas as pd
    import numpy as np
    import plotly.graph_objects as go
    
    d = {'Name':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], 
          'Score':[1, 1.5, 4, 1.5, 1.5, 4, 2.5, 3, 2.5, 4]}
    
    df = pd.DataFrame(data=d)
    df['cum_cnt'] = df.groupby('Score').cumcount() + 1
    
    fig = go.Figure()
    
    for p in df.itertuples():
        fig.add_trace(go.Scatter(x=[p.Score], y=[p.cum_cnt], mode='markers', showlegend=False))
    
    fig.update_traces(
        marker=dict(
            size=25,
            color='LightSkyBlue',
            line=dict(
                color='gray',
                width=2
                )
            ))
    fig.update_yaxes(tickmode='array', tickvals=np.arange(0,11,1), range=[0,11], visible=False)
    fig.update_layout(height=450, width=700, template='plotly_white')
    
    fig.show()
    
    

    enter image description here