首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用MATPLOTLIB绘制1个图+线性回归的2个数据集

用MATPLOTLIB绘制1个图+线性回归的2个数据集
EN

Stack Overflow用户
提问于 2016-09-11 10:36:23
回答 1查看 656关注 0票数 1

我是Python新手,有任何编程背景。我试图为相同的x数据集绘制y的2个数据集,用get对它进行线性回归,得到R^2值。我就是这样走到现在的:

代码语言:javascript
运行
复制
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没有得到任何情节,我得到了错误:"UnicodeDecodeError:'ascii‘编解码器无法在第12位解码字节0xce :序号不在范围(128)“

但我不明白。有人能帮忙告诉我我的密码出了什么问题吗?谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-11 10:47:03

此代码中有多个错误。

  1. 您不能简单地在情节标签和标题中键入希腊字母,以下是您可以这样做的方法: Pl.xlabel(r‘理论$\lambda$ ( nm)')
  2. y1labely2label不是pl模块的对象
  3. 在Python中,# blah blah不同于'''blah blah'''。第一个是注释,第二个是表达式。可以将第二个变量分配给变量(a = '''blah blah'''),但不能将第一个变量分配给变量:a = # blah blah生成一个SyntaxError。

下面是一个应该有效的代码:

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39435177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档