在 Python 中绘制多项式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18088261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:53:51  来源:igfitidea点击:

Plotting a polynomial in Python

pythonnumpymatplotlibcurve

提问by Ally

I am new to Python plotting apart from some basic knowledge of matplotlib.pyplot. My question is how to plot some higher degree polynomials? One method I saw was expressing y in terms of x and then plotting the values. But I have 2 difficulties:

除了一些基本的matplotlib.pyplot. 我的问题是如何绘制一些更高次的多项式?我看到的一种方法是用 x 表示 y,然后绘制这些值。但我有两个困难:

  1. y and x cannot be separated.
  2. I am expecting a closed curve(actually a complicated curve)
  1. y 和 x 不能分开。
  2. 我期待一条闭合曲线(实际上是一条复杂的曲线)

The polynomial I am trying to plot is:

我试图绘制的多项式是:

c0 + c1*x + c2*y +c3*x*x + c4*x*y + c5*y*y + c6*x**3 + c7*x**2*y + ..... c26*x*y**5 + c27*y**6

All coefficients c0to c27are known. How do I plot this curve?

所有系数c0toc27都是已知的。我如何绘制这条曲线?

Also could you please suggest me resources from where I can learn plotting and visualization in Python?

另外,您能否向我推荐可以在 Python 中学习绘图和可视化的资源?

Clarification:Sorry everyone for not making it clear enough. It is not an equation of a surface (which involves 3 variables: x, y and z). I should have put a zero at the end: c0 + c1*x + c2*y +c3*x*x + c4*x*y + c5*y*y + c6*x**3 + c7*x**2*y + ..... c26*x*y**5 + c27*y**6 =0

澄清:对不起,大家没有说得足够清楚。它不是一个表面方程(它涉及 3 个变量:x、y 和 z)。我应该在最后放一个零:c0 + c1*x + c2*y +c3*x*x + c4*x*y + c5*y*y + c6*x**3 + c7*x** 2*y + ..... c26*x*y**5 + c27*y**6 =0

回答by Viktor Kerkez

I'm not sure I fully understood your question, but I think you want a surface plot

我不确定我是否完全理解你的问题,但我认为你想要一个表面图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(x, y)
F = 3 + 2*X + 4*X*Y + 5*X*X

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, F)
plt.show()

And for the resources: official documentationand pyvideos

对于资源:官方文档pyvideos

回答by Saullo G. P. Castro

Your equation represents a 3D surface, which you can plot creating first a mesh grid of xand yvalues, easily achieved using numpy:

你的方程代表一个 3D 表面,你可以绘制首先创建一个网格网格xy值,使用 numpy 很容易实现:

X,Y = np.meshgrid( np.linspace( xmin, xmax, 100), np.linspace( ymin, ymax, 200) )

Xand Yare both 2D arrays containing the X and Y coordinates, respectively.

XY都是分别包含 X 和 Y 坐标的二维数组。

Then you can calculate zvalues for each point in this mesh, using the known coefficients:

然后,您可以z使用已知系数计算此网格中每个点的值:

Z = c0 + c1*X + c2*Y +c3*X*X + c4*X*Y + c5*Y*Y + c6*X**3 + c7*X**2*Y + ..... c26*X*Y**5 + c27*Y**6

After that you can plot it using matplotlib:

之后,您可以使用matplotlib以下方法绘制它:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
ax = plt.subplot(111, projection='3d')
ax.plot_surface( X, Y, Z )
plt.show()