0

I read the question Matplotlib connect scatterplot points with line - Python but it doesn't work for me...

I tried to use plt.scatter(x, y) and after that plt.show(), but all that I see is a set of separated dots from each other.

Like that:

I think, that problem consists in that I use a loop for generating each point on the plot and after that loop, I use plt.show().

All my code looks like this:

x = 0
y = 0
def eye_gaze1(a,b):
    global x,y
    image_plot1 = plt.imshow(image1)
    for i in range(len(a)):
        if stimulus_name[x+y+1] == '5_01.jpg' and a[x+y+1] != '-':
            plt.scatter([a[x+y+1]], [b[x+y+1]]) #here I putting new point on the image_plot1 and this process repeats (something like 1000 times) before in my massive of data cycle will find '-' symbol
            x += 1
        else:
            x += 1
            break

    plt.show() #final image
    y += x
    x = 0

j = 0    
while j < 15: #just repeat this function for another cells in data-massive 
    eye_gaze1(col_values_X_right,col_values_Y_right) 
    j += 1

So, question is, how can I connect points?


If I try to use the comment of JohanC, I see this error: TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

x = 0
y = 0
def eye_gaze1(a,b):
    global x,y
    image_plot1 = plt.imshow(image1)
    for i in range(len(a)):
        if stimulus_name[x+y+1] == '5_01.jpg' and a[x+y+1] != '-':
            X_coordinates = list().append(a[x+y+1])
            Y_coordinates = list().append(b[x+y+1])

            x += 1
        else:
            x += 1
            break
    plt.scatter(X_coordinates, Y_coordinates, '-o')    
    plt.show()
    y += x
    x = 0

j = 0    
while j < 15:
    eye_gaze1(col_values_X_right,col_values_Y_right)
    print(x)
    print(y)
    j += 1

5
  • 1
    Instead of calling plt.scatter inside your for loop, you could gather all the x and all the y values into new arrays (e.g. xvals and yvals). After the loop, you can call plt.plot(xvals, yvals, ...). Commented May 3, 2020 at 16:28
  • @JohanC If try to do it, I see this error TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' Commented May 3, 2020 at 20:25
  • @JohanC Or if I delete -o parameter --- no dots on the images Commented May 3, 2020 at 20:58
  • 1
    Well, you should set X_coordinates = [] before the forloop and call X_coordinates.append(a[x+y+1]) inside the loop. Commented May 3, 2020 at 22:05
  • @JohanC Yeah, it works, thank you Commented May 4, 2020 at 7:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.