Plotting 2 data sets in 1 graph + linear regression in MATPLOTLIB
<p>I'm new to Python and have any programming background..
I'm trying to plot 2 data sets of y for the same x data set, linear regress it using scipy and get the R^2 value. This is how i've gotten so far:</p>

<pre><code>import matplotlib
import matplotlib.pyplot as pl

from scipy import stats

#first order
'''sin(ÎÎ¸)'''
y1 = [-0.040422445,-0.056402365,-0.060758191]
#second order
'''sin(ÎÎ¸)'''
y2 = [-0.083967708, -0.107420964, -0.117248521]
''''Î», theo (nm)'''
x= [404.66, 546.07, 579.06]
pl.title('Angular displacements vs. Theoretical wavelength')
pl.xlabel('theoretical Î» (in nm)')
pl.y1label('sin(ÎÎ¸) of 1st order images')
pl.y2label('sin(ÎÎ¸) of 2nd order images')
plot1 = pl.plot(x, y1, 'r')
plot2 = pl.plot(x, y2, 'b')
pl.legend([plot1, plot2], ('1st order images', '2nd order images'), 'best', numpoints=1)
slope1, intercept1, r_value1, p_value1, std_err1 = stats.linregress(x,y1)
slope2, intercept2, r_value2, p_value2, std_err2 = stats.linregress(x,y2)
print "r-squared:", r_value1**2
print "r-squared:", r_value2**2
pl.show()
</code></pre>

<p>...i dont' get any plot and i get the error:
   "UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 12: ordinal not in range(128)"</p>

<p>which i don't understand. can somebody help and tell me what's wrong with my code? thank youuu</p>

<p>There are multiple errors in this code.</p>

<ol>
<li><p>You cannot simply type greek letters in plot labels and titles, here
is how you can do it:</p>

<pre><code>pl.xlabel(r'theoretical $\lambda$ (in nm)')
</code></pre></li>
<li><p><code>y1label</code> and <code>y2label</code> are not objects of the <code>pl</code> module</p></li>
<li><p>In Python, <code># blah blah</code> is different from <code>'''blah blah'''</code>. The first one is a comment, the second one is an expression. You can assign the second one to a variable (<code>a = '''blah blah'''</code>) but you cannot assign the first one to a variable: <code>a = # blah blah</code> yields a SyntaxError.</p></li>
</ol>

<p>Here is a code that should work:</p>

<pre><code>import matplotlib
import matplotlib.pyplot as pl
from scipy import stats

y1 = [-0.040422445,-0.056402365,-0.060758191]
y2 = [-0.083967708, -0.107420964, -0.117248521]
x= [404.66, 546.07, 579.06]

pl.title('Angular displacements vs. Theoretical wavelength')
pl.xlabel(r'theoretical $\lambda$ (in nm)')
pl.ylabel(r'sin($\Delta\theta$)')

y1label = '1st order images'
y2label = '2nd order images'

plot1 = pl.plot(x, y1, 'r', label=y1label)
plot2 = pl.plot(x, y2, 'b', label=y2label)

pl.legend()

slope1, intercept1, r_value1, p_value1, std_err1 = stats.linregress(x,y1)
slope2, intercept2, r_value2, p_value2, std_err2 = stats.linregress(x,y2)
print "r-squared:", r_value1**2
print "r-squared:", r_value2**2

pl.show()
</code></pre>

python matplotlib linear-regression