1

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:

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()

...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)"

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

1 Answer 1

1

There are multiple errors in this code.

  1. You cannot simply type greek letters in plot labels and titles, here is how you can do it:

    pl.xlabel(r'theoretical $\lambda$ (in nm)')
    
  2. y1label and y2label are not objects of the pl module

  3. In Python, # blah blah is different from '''blah blah'''. The first one is a comment, the second one is an expression. You can assign the second one to a variable (a = '''blah blah''') but you cannot assign the first one to a variable: a = # blah blah yields a SyntaxError.

Here is a code that should work:

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()
Sign up to request clarification or add additional context in comments.

3 Comments

ohhh thanks a lot! but can't i put sin($\Delta\theta$) as the label of my y-axis then change the series labels to just '1st order images' and '2nd order images' for the other?
..also, can't i include the calculated r^2 values in the graph as well? (like how you can in excel? hehe)
not sure what you mean. if you just want to add text, you can add the r^2 values to the legend. or you can use annotations (matplotlib.org/1.5.1/users/annotations_intro.html)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.