Python 作图多项式

Python 作图多项式,python,numpy,graph,Python,Numpy,Graph,在一些帮助下,我生成了以下代码。以下是给定输入的一些期望输出。然而,我在完成这段代码的最后一个任务时遇到了一些困难。在此寻求帮助,非常感谢任何指导或帮助,谢谢 根据以下输入,我得到了以下输出: poly_horner([1,2,3,4,5],2) 129 打印(触发器) 8. poly_naive([1,2,3,4,5,2]) 129 打印(触发器)[![在此处输入图像描述][1][1] 20 np.polyval([5,4,3,2,1],2) 129 我假设您想创建一个数字,尽管您的问题很模

在一些帮助下,我生成了以下代码。以下是给定输入的一些期望输出。然而,我在完成这段代码的最后一个任务时遇到了一些困难。在此寻求帮助,非常感谢任何指导或帮助,谢谢

根据以下输入,我得到了以下输出:

poly_horner([1,2,3,4,5],2)
129
打印(触发器)
8.
poly_naive([1,2,3,4,5,2])
129
打印(触发器)[![在此处输入图像描述][1][1]
20
np.polyval([5,4,3,2,1],2)
129

我假设您想创建一个数字,尽管您的问题很模糊……但我有几分钟的时间在代码运行时消磨时间。不管怎么说,你可能很难策划

import numpy as np
import pylab as pl

x = np.arange(10)
y = x * np.pi

# you can calculate a line of best fit (lobf) using numpy's polyfit function
lobf1 = np.polyfit(x, y, 1)  # first degree polynomial
lobf2 = np.polyfit(x, y, 2)  # second degree polynomial
lobf3 = np.polyfit(x, y, 3)  # third degree polynomial

# you can now use the lines of best fit to calculate the 
# value anywhere within the domain using numpy's polyval function
# FIRST, create a figure and a plotting axis within the fig
fig = pl.figure(figsize=(3.25, 2.5))
ax0 = fig.add_subplot(111)

# now use polyval to calculate your y-values at every x
x = np.arange(0, 20, 0.1)
ax0.plot(x, np.polyval(lobf1, x), 'k')
ax0.plot(x, np.polyval(lobf2, x), 'b')
ax0.plot(x, np.polyval(lobf3, x), 'r')

# add a legend for niceness
ax0.legend(('Degree 1', 'Degree 2', 'Degree 3'), fontsize=8, loc=2)

# you can label the axes whatever you like
ax0.set_ylabel('My y-label', fontsize=8)
ax0.set_xlabel('My x-label', fontsize=8)

# you can show the figure on your screen
fig.show()

# and you can save the figure to your computer in different formats
# specifying bbox_inches='tight' helps eliminate unnecessary whitespace around
# the axis when saving...it just looks better this way.
pl.savefig('figName.png', dpi=500, bbox_inches='tight')
pl.savefig('figName.pdf', bbox_inches='tight')

# don't forget to close the figure
pl.close('all')

完成最后一项任务时,您遇到了哪些具体问题?欢迎使用StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南,在这里申请。StackOverflow不是设计、编码、研究或教程资源。我们需要比“我有麻烦”更具体的东西,特别是因为在线教程和示例中很好地介绍了“绘图”。@martineau我不太确定如何完成这部分内容question@Prune谢谢,我会记住的。用户123:那还是太模糊了。也许下载/安装类似的东西会有所帮助(假设您还没有它,这就是您不确定如何完成任务的意思)。所以我只需要将它添加到我的代码中以使其运行?我还需要使用plt.show()而不是fig.show()使我的代码运行!或者这只是一种普通的绘图方式?你原来的帖子模棱两可到了无法回答的程度。我抓住了一个机会,你可能需要帮助创建你的数字…看起来我浪费了我的时间。不,它有帮助!我会看一下,学习如何创造数字
import numpy as np
import pylab as pl

x = np.arange(10)
y = x * np.pi

# you can calculate a line of best fit (lobf) using numpy's polyfit function
lobf1 = np.polyfit(x, y, 1)  # first degree polynomial
lobf2 = np.polyfit(x, y, 2)  # second degree polynomial
lobf3 = np.polyfit(x, y, 3)  # third degree polynomial

# you can now use the lines of best fit to calculate the 
# value anywhere within the domain using numpy's polyval function
# FIRST, create a figure and a plotting axis within the fig
fig = pl.figure(figsize=(3.25, 2.5))
ax0 = fig.add_subplot(111)

# now use polyval to calculate your y-values at every x
x = np.arange(0, 20, 0.1)
ax0.plot(x, np.polyval(lobf1, x), 'k')
ax0.plot(x, np.polyval(lobf2, x), 'b')
ax0.plot(x, np.polyval(lobf3, x), 'r')

# add a legend for niceness
ax0.legend(('Degree 1', 'Degree 2', 'Degree 3'), fontsize=8, loc=2)

# you can label the axes whatever you like
ax0.set_ylabel('My y-label', fontsize=8)
ax0.set_xlabel('My x-label', fontsize=8)

# you can show the figure on your screen
fig.show()

# and you can save the figure to your computer in different formats
# specifying bbox_inches='tight' helps eliminate unnecessary whitespace around
# the axis when saving...it just looks better this way.
pl.savefig('figName.png', dpi=500, bbox_inches='tight')
pl.savefig('figName.pdf', bbox_inches='tight')

# don't forget to close the figure
pl.close('all')