py作图

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['FangSong']
plt.rcParams['axes.unicode_minus'] = False

xs = np.linspace(-1, 1, 11)
ys = np.array([-0.3, -0.5, -0.2, -0.3, 0, 0.4, 0.2, -0.3, 0.2, 0.5, 0.4])
    
xm = np.linspace(-1, 1, 201)
ym = ((xm**2-1)**3 + 0.5)*np.sin(2*xm) + np.random.random(201)/10 - 0.1

fs4 = np.poly1d(np.polyfit(xs, ys, 4))
fs5 = np.poly1d(np.polyfit(xs, ys, 5))
fs6 = np.poly1d(np.polyfit(xs, ys, 6))
fs7 = np.poly1d(np.polyfit(xs, ys, 7))
fs8 = np.poly1d(np.polyfit(xs, ys, 8))
fs9 = np.poly1d(np.polyfit(xs, ys, 9))
fs10 = np.poly1d(np.polyfit(xs, ys, 10))

fm2 = np.poly1d(np.polyfit(xm, ym, 2))
fm3 = np.poly1d(np.polyfit(xm, ym, 3))
fm4 = np.poly1d(np.polyfit(xm, ym, 4))
fm5 = np.poly1d(np.polyfit(xm, ym, 5))
fm6 = np.poly1d(np.polyfit(xm, ym, 6))
fm7 = np.poly1d(np.polyfit(xm, ym, 7))

plt.subplot(211)
plt.plot(xs, ys, c='r', ls='', marker='o')
plt.plot(xs, fs4(xs), label=u'四次多项式')
plt.plot(xs, fs5(xs), label=u'五次多项式')
plt.plot(xs, fs6(xs), label=u'六次多项式')
plt.plot(xs, fs7(xs), label=u'七次多项式')
plt.plot(xs, fs8(xs), label=u'八次多项式')
plt.plot(xs, fs9(xs), label=u'九次多项式')
plt.legend()

plt.subplot(212)
plt.plot(xm, ym, c='g', ls='', marker='.')
plt.plot(xm, fm2(xm), label=u'二次多项式')
plt.plot(xm, fm3(xm), label=u'三次多项式')
plt.plot(xm, fm4(xm), label=u'四次多项式')
plt.plot(xm, fm5(xm), label=u'五次多项式')
plt.plot(xm, fm6(xm), label=u'六次多项式')
plt.plot(xm, fm7(xm), label=u'七次多项式')
plt.legend()
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值