Search code examples
pythonplotscatter-plot

Scatter plot - how to do it


I would like to reproduce this plot in Python: (https://i.sstatic.net/6CRfn.png) Any idea how to do this?

I tried to do a normal plt.scatter() but I can't draw this axes on the zero, for example.


Solution

  • That's a very general question... Using plt.scatter() is certainly a good option. Then just add the two lines to the plot (e.g. using axhline and axvline).

    Slightly adapting this example:

    import numpy as np
    import matplotlib.pyplot as plt
    # don't show right and top axis[![enter image description here][1]][1]
    import matplotlib as mpl
    mpl.rcParams['axes.spines.right'] = False
    mpl.rcParams['axes.spines.top'] = False
    
    # some random data
    N = 50
    x = np.random.randint(-10, high=11, size=N, dtype=int)
    y = np.random.randint(-10, high=11, size=N, dtype=int)
    colors = np.random.rand(N)
    area = (30 * np.random.rand(N))**2  # 0 to 15 point radii
    
    # creating a vertical and a horizontal line
    plt.axvline(x=0, color='grey', alpha=0.75, linestyle='-')
    plt.axhline(y=0, color='grey', alpha=0.75, linestyle='-')
    # scatter plot
    plt.scatter(x, y, s=area, c=colors, alpha=0.5)
    
    plt.show()
    

    scatter plot