0

I am trying to plot a scatter chart using numpy and matplotlib. It is very simple, I have 2 data files, each contains single column set of data. Both files have same number of data (I've checked this over and over again to make sure).

This is what I've done:

import numpy as np import pylab as pl xdata = np.loadtxt('data.txt') ydata = np.loadtxt('data1.txt') pl.plot(xdata, ydata, 'ro') pl.show() 

and it gives me this error

File "C:/1aProjects/Python_Aryo/Plotting/test_plot.py", line 10, in <module> pl.plot(xdata, ydata, 'ro') File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 2987, in plot ret = ax.plot(*args, **kwargs) File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 4144, in plot for line in self._get_lines(*args, **kwargs): File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 319, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 297, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 239, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension 

it says the data does not have the same dimension.

Then I tried to plot only in certain range, and it works but until row no 72402 as shown below.

pl.plot(xdata[0:72402], ydata[0:72402], 'ro') 

if I put anything more than 72402, it gives me the same error msg. Telling that the array does not have the same size. My data actually has up row 72413!, it is just 11 more rows! it is quite annoying isn't it?

Can anyone help?

2
  • 2
    One of your data probably has only 72402 rows... Did you try printing xdata.shapeandydata.shape?
    – Holt
    CommentedJun 16, 2016 at 13:58
  • Thanks, yes it shows different numbers of rows. It is sorted.! some blank spaces in the middle.. :D
    – arsewi
    CommentedJun 17, 2016 at 7:41

1 Answer 1

1

It is attempting to plot x and y variables, as in a scatter plot. The two vectors need to have the same length, which is why you see that you are able to plot up to the smallest of the two lengths. There's no getting around it. The length of the two arrays is clearly not equal.

3
  • 'pl.plot(xdata[0:72403], ydata[0:72403], 'ro')' if I add one more row like this, still does not work. I checked my data in that particular row. Nothing wrong with them.
    – arsewi
    CommentedJun 16, 2016 at 16:34
  • There might be a data entry that does not fit with the type requested, a nan, a quoted string, a blank?
    – Benjamin
    CommentedJun 16, 2016 at 18:26
  • you are right..!! There are 10 spaces blank in the middle of the data. I didnt spot it. I thought it will always something at the end of the data. Thanks! Sorted
    – arsewi
    CommentedJun 17, 2016 at 7:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.