Save/read data to/from textfile
<p>I have the following two series:</p>

<pre><code>self.MW_x = ..
self.MW_x = .. (from previous calculations)
</code></pre>

<p>and I zip them together like this:</p>

<pre><code>self.MW_final = list(zip(self.MW_x, self.MW_y))
</code></pre>

<p>and try to save them with <code>numpy.savetxt</code></p>

<pre><code>np.savetxt("testfile.txt", self.MW_final, delimiter = ";", header = "x_Value, y_Value")
</code></pre>

<p>If I plot it directly (self.MW_x, self.MW_y) it looks like this, which is perfect:</p>

<p><img src="http://i.stack.imgur.com/XhBYt.png" alt="perfect plot"></p>

<p>but if I try to plot the saved textfile again, it looks like this:</p>

<p><img src="http://i.stack.imgur.com/DRzFt.png" alt="entirely wrong"></p>

<p>I just don't know what happened on the way to the file and back.</p>

<p>Have you tried looking at the first 5 rows of the data at every step (first 5 rows of self.MW_x, self.MW_final, /Users/sping/Desktop/testfile.txt, df, x_val, and y_val) to figure out where the data is changing?</p>

<p>I think you have a problem with how you're using ix</p>

<pre><code>x_val = df.ix[0:]
y_val = df.ix[1:]
</code></pre>

<p>Doesn't grab the first and second column. It grabs the first row until the end  <code>[0:]</code> and the second row until the end <code>[1:]</code>. As a result, x_val will be 1 row longer than y_val. You want to use</p>

<pre><code>x_val = df.ix[:,0]
y_val = df.ix[:,1]
</code></pre>

<p>The first argument to ix gives you the rows and the second argument gives you the columns.</p>

<p>Also you want to set header=0 or just leave it as default if you want to use the column headers. Python uses zero indexing so the first row is row 0, not row 1.</p>

<p>I looks like you have some typos in the code you've posted. You're also using ';' as a delimiter when saving with np.savetxt and ',' as a delimiter when reading using pd.read_csv.</p>

python numpy save text-files