1

I'm trying to scatter or plot 2 sets of arrays using numpy and matplotlib. Everything is ok with the code except when I try to have lines instead of dots in my plot The plot is ok when I use :

from numpy import *
import matplotlib.pyplot as plt
positions=open('test.txt','r')

lines=positions.readlines()

for i in range(0,len(lines)):
    line=lines[i] 
    values=line.split("     ")

    x_val = [float(values[0])]
    y_val = [float(values[1])]
   # plt.scatter(x_val,y_val)
    #Or
    plt.plot(x_val,y_val,'ro')
    plt.title(' Data')
    plt.xlabel('x ')
    plt.ylabel('y')
    plt.show()

positions.close()

enter image description here

But when I replace plt.plot(x_val,y_val,'ro') with plt.plot(x_val,y_val,'r') , or plt.plot(x_val,y_val,'-') What I get is merely a blank page! enter image description here I have no idea what the problem is, because I tried it with many many different options and yet the only option which works properly is having 'o'.

1 Answer 1

2

The reason that you see no lines when you ask for a plot without setting the markers is because you are plotting each (x,y) point individually, which can have a point position, but would create a line of length zero.

If instead of plotting each point immediately upon reading it, you put those values into an array, and called the plot function just once, you could also show a line:

x_vals = []
y_vals = []

for i in range(0,len(lines)):
    line=lines[i] 
    values=line.split("     ")

    x_vals.append(float(values[0]))
    y_vals.append(float(values[1]))

plt.plot(x_vals, y_vals,'ro-')

And you could still use the data in a scatter plot if required.

Sign up to request clarification or add additional context in comments.

Comments

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.