简体   繁体   English

在MATPLOTLIB中的1张图中绘制2个数据集+线性回归

[英]Plotting 2 data sets in 1 graph + linear regression in MATPLOTLIB

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. 我是Python的新手,并且具有任何编程背景。.我试图为相同的x数据集绘制y的2个数据集,使用scipy线性回归并获得R ^ 2值。 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)" ...我没有得到任何绘图,但得到错误:“ UnicodeDecodeError:'ascii'编解码器无法解码位置12的字节0xce:序数不在范围(128)中”

which i don't understand. 我不明白。 can somebody help and tell me what's wrong with my code? 有人可以帮我告诉我代码有什么问题吗? thank youuu 谢谢你

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 y1labely2label不是pl模块的对象

  3. In Python, # blah blah is different from '''blah blah''' . 在Python中, # blah blah'''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. 您可以将第二个分配给变量( a = '''blah blah''' ),但不能将第一个分配给变量: a = # blah blah产生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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM