0

I am trying to plot two data sets into the same graph using matplotlib. No. of data points for the first pair of (x1,y1) is 150. No. of data points for the second pair (x2,y2) is 100. Please note that the range for x1 and x2 is from 0.0 to 1.0. Only the no. of data points are different. When I try to plot these into the same graph for comparison,

The error message that appears is:

"x and y must have same first dimension".

Code snippet below for your reference:

def main():
    x1,y1 = np.loadtxt('txt1.txt',unpack=True)
    x2,y2 = np.loadtxt('txt2.txt',unpack=True)
    plt.figure()
    plt.plot(x1,y1)
    plt.plot(x2,y2)

main()
Srivatsan
  • 9,225
  • 13
  • 58
  • 83
  • 2
    your code works for me on two txt files of different length. Try looking at the shape of you `x1, y1, x2` and `y2`. (i.e. `print x1.shape`, etc) to check that the data file have been read correctly – tmdavison Aug 04 '15 at 14:33
  • 2
    If that is error you get, it is clear that either `x1 and y1` don't have the same `length`, or `x2 and y2` don't have the same length. As @tom points out, check for the shape of all your four variables. `x1 and y1` and `x2 and y2` should have the same shape. – Srivatsan Aug 04 '15 at 14:33
  • @tom thanks for your help. It worked since I mixed up the variable names and hence the shapes of the variables in the actual code. – Nerdy business Aug 05 '15 at 04:54
  • As this was effectively a typo - it's probably a good idea to close it down (as it's been solved in comments and won't be of use to others in the future). – J Richard Snape Aug 05 '15 at 14:04

0 Answers0