Search code examples
arrayspython-3.xnumpymatplotlibscatter-plot

How to increase point size when using np.arange to get size of points in Matplotlib scatterplot


I'm trying to get the points to gradually increase in size. I believe I need to pass the size argument a list of n values (n representing the size of the array). So to this end, I'm using the linspace function to create the list of values that range from small to larger and passing it into a function as an argument. It's working generally, but I have two questions:

  1. Is there a more optimum way of writing the array that gets passed into the 'color' of the scatterplot? See below where I'm passing in an array from code I found on the Internet but it looks clunky.
  2. How do I increase the size of the points so they're all about 2 points larger? See below size variable where I'm passing in an array and passing in row*col. Every time I try to tinker with the size here, it throws an error.
    I attached an image of what the output should look like for reference.

Thanks for any assistance.enter image description here

# function
def plotdata(data):
    fig = plt.figure(figsize=(10,5))   

# plot
    plt.title("2D Data",size="15") 
    plt.xlabel("x") 
    plt.ylabel("y") 
    plt.scatter(x, y, s=size, c=color, marker='o', cmap='copper')
    plt.xlim(-2, 2)
    plt.ylim(-2, 2)

# Creating variables
x = data[["x"]]
y = data[["y"]]

limits=[-2.25, 2.25, -2.25, 2.25]
row,col=5,5
size = np.arange(1, row*col + 1)
color = np.array([np.linspace(0,1, row*col),
                 np.linspace(0,0.3, row*col),
                 np.linspace(0,0.3, row*col)]).T


Solution

  • 2 possibilities:

    1.

    size = np.arange(1, row*col + 1)
    

    I think this is the error, I don't think it is adding the 1 since the graph is weird.

    1. (likely)

      plt.scatter(x, y, s=size, c=color, marker='o', cmap='copper')
      plt.title("2D Data",size="15")
      size = np.arange(1, row*col + 1) 
      

    you define size many times best of luck, it has been a while since I've used this API though