pythonmatplotlibscatter-plot

How to fill in the space between each individual scatter points


I am plotting the following data:

fig, ax = plt.subplots()
im = ax.scatter(std_sorted[:, [1]], std_sorted[:, [2]], s=5, c=std_sorted[:, [0]])

With the following result:

How the current scatter plot looks like

My question is: can I fill the space between each point in the plot by extrapolating and then coloring that extrapolated space accordingly, so I get a uniform plot without any points?

So basically I'm looking for this result (This is simply me "squeezing" the above picture to show the desired result and not dealing with the space between the points):

This is simply me "squeezing" the above picture to show the desired result and not dealing with the space between the points


Solution

  • The simplest thing to do in this case, is to use a short vertical line a marker, and set the markersize large enough such that there is no white space left.

    Another option is to use tricontourf to create a filled image of x, y and z.

    Note that neither a scatter plot nor tricontourf need the points to be sorted in any order. If you do have your points sorted into an orderded grid, plt.imshow should give the best result.

    Here is some code to show how it could look like. First some dummy data slightly similar to the example are generated. As the x,y are random, they don't fill the complete space. This might leave some blank spots in the scatter plot. The spots are nicely interpolated for the contourf, except possibly in the corners.

    import numpy as np
    import matplotlib.pyplot as plt
    
    N = 50000
    xmin = 0
    xmax = 0.20
    ymin = -0.01
    ymax = 0.01
    
    std_sorted = np.zeros((N, 3))
    std_sorted[:,1] = np.random.uniform(xmin, xmax, N)
    std_sorted[:,2] = np.random.choice(np.linspace(ymin, ymax, 80), N)
    std_sorted[:,0] = np.cos(3*(std_sorted[:,1] - 0.04 - 100*std_sorted[:,2]**2))**10
    
    fig, ax = plt.subplots(ncols=2)
    # im = ax[0].scatter(std_sorted[:, 1], std_sorted[:, 2], s=20, c=std_sorted[:, 0], marker='|')
    im = ax[0].scatter(std_sorted[:, 1], std_sorted[:, 2], s=5, c=std_sorted[:, 0], marker='.')
    ax[0].set_xlim(xmin, xmax)
    ax[0].set_ylim(ymin, ymax)
    ax[0].set_title("scatter plot")
    ax[1].tricontourf(std_sorted[:, 1], std_sorted[:, 2], std_sorted[:, 0], 256)
    ax[1].set_title("tricontourf")
    
    plt.tight_layout()
    plt.show()
    

    sample plot