2

This is embarrassing, but I am unable to plot this:

  import numpy as np
  import matplotlib.pyplot as plt
  datf=np.loadtxt(filename, dtype=float,delimiter=" ")
  print((datf))
  plt.plot(datf[:0], datf[:1])
  plt.show()

This is datf:

[[  1.         19.778986 ]
 [  1.3625678  -1.9363698]
 [  1.4142136   6.5144132]
 [  1.6901453   3.8092139]
 [  2.         -4.0222051]]

And the error is:

ValueError: x and y must have same first dimension

2 Answers 2

2

It looks like you are trying to plot the first column as x and the second column as y. You made a mistake in indexing. To get the first column of datf, you need to do datf[:, 0] (note the comma).

Your final code will look like:

  import numpy as np
  import matplotlib.pyplot as plt
  datf=np.loadtxt(filename, dtype=float,delimiter=" ")
  print((datf))
  plt.plot(datf[:, 0], datf[:, 1])  # note the commas here
  plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I missed that, and had no clue why this trivial thing is not working
0

to get the 1st and 2nd column, indexing must be

plt.plot(datf[:,0],datf[:,1])

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.