6

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:

  1. y and x cannot be separated.
  2. I am expecting a closed curve(actually a complicated curve)

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 c0 to c27 are known. How do I plot this curve?

Also could you please suggest me resources from where I can learn plotting and visualization in 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

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Ally
  • 73
  • 1
  • 1
  • 4
  • 1
    If you expect a curve, maybe you want your polynomial = 0, and view it as an implicit equation? If you just want to "plot your polynomial", since it's a function of two variable, the result is a surface, not a plane curve. –  Aug 06 '13 at 22:32
  • yes, edited my question. sorry for the confusion – Ally Aug 07 '13 at 05:52
  • 1
    You may have a look at [this](http://stackoverflow.com/questions/2484527/is-it-possible-to-plot-implicit-equations-using-matplotlib) SO question, and the *plot_implicit* function in [sympy](http://docs.sympy.org/dev/modules/plotting.html). –  Aug 07 '13 at 13:33

2 Answers2

6

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 documentation and pyvideos

Viktor Kerkez
  • 45,070
  • 12
  • 104
  • 85
  • Yes :), I'm using `ipython notebook --pylab=inline` so it shows it immediately. But if you are writing a script you need it. Fixed it, thank you :) – Viktor Kerkez Aug 06 '13 at 22:31
2

Your equation represents a 3D surface, which you can plot creating first a mesh grid of x and y values, easily achieved using numpy:

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

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

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

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:

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()
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234