Search code examples
pythonplotlyscatter-plot

Python PLOTLY I want to make the circles clearer


import plotly.express as px
import pandas as pd
  

data = pd.read_csv("Book1.csv")
  
fig = px.scatter(data, y="Category", x="Mean", color="Change")
fig.update_layout(
    xaxis=dict(title="Title",range=[2,3],),
    yaxis=dict(title="Mean"),
    title="Title"
) 

fig.update_traces(marker=dict(size=30,
                              line=dict(width=2,
                                        color='DarkSlateGrey')),
                  selector=dict(mode='markers'))
fig.show()

I want to make the circles clearer, like more spaced out or scattered. Do you have any suggestions?

Here is the plot:

enter image description here


Solution

  • There is a technique called jittering where you add a small amount of noise to make it less likely for the circles to overlap as much as in your sample figure. It's not perfect, but here is an example of what you can accomplish. You can also try regenerating the plot with a different amount of jittering, as well as different random seeds until you are happy with the result.

    import plotly.express as px
    import numpy as np
    import pandas as pd
    
    # data = pd.read_csv("Book1.csv")
    data = pd.DataFrame({
        'Category': ['More than usual']*5 + ['About the same']*5 + ['Less than usual']*5,
        'Mean': [2.2,2.4,2.22,2.24,2.6] + [2.4,2.41,2.5,2.1,2.12] + [2.81,2.1,2.5,2.45,2.42],
        'Change': [1]*5 + [2]*5 + [3]*5
    })
    
    category_to_value_map = {
        'Less than usual': 1,
        'About the same': 2,
        'More than usual': 3
    }
    data['y'] = data['Category'].map(category_to_value_map)
    
    ## apply jittering
    max_jittering = 0.15
    np.random.seed(4)
    data['y'] = data['y'] + np.random.uniform(
        low=-1*max_jittering, 
        high=max_jittering, 
        size=len(data)
    )
    
    fig = px.scatter(data, y="y", x="Mean", color="Change")
    fig.update_layout(
        xaxis=dict(title="Title",range=[2,3],),
        yaxis=dict(title="Mean"),
        title="Title"
    ) 
    
    fig.update_traces(marker=dict(size=20,
                                  line=dict(width=2,
                                            color='DarkSlateGrey')),
                      selector=dict(mode='markers'))
    fig.update_layout(
        yaxis = dict(
            tickmode = 'array',
            tickvals = [1, 2, 3],
            ticktext = ['Less than usual', 'About the same', 'More than usual']
        )
    )
    fig.show()
    

    enter image description here