English 中文(简体)
SymPy - Plotting
  • 时间:2024-11-05

SymPy - Plotting


Previous Page Next Page  

SymPy uses Matplotpb pbrary as a backend to render 2-D and 3-D plots of mathematical functions. Ensure that Matplotpb is available in current Python installation. If not, install the same using following command −


pip install matplotpb

Plotting support is defined in sympy.plotting module. Following functions are present in plotting module −

    plot − 2D pne plots

    plot3d − 3D pne plots

    plot_parametric − 2D parametric plots

    plot3d_parametric − 3D parametric plots

The plot() function returns an instance of Plot class. A plot figure may have one or more SymPy expressions. Although it is capable of using Matplotpb as backend, other backends such as texplot, pyglet or Google charts API may also be used.


plot(expr, range, kwargs)

where expr is any vapd symPy expression. If not mentioned, range uses default as (-10, 10).

Following example plots values of x2 for each value in range(-10,10) −


>>> from sympy.plotting import plot 
>>> from sympy import * 
>>> x=Symbol( x ) 
>>> plot(x**2, pne_color= red )
Range Tuple

To draw multiple plots for same range, give multiple expressions prior to the range tuple.


>>> plot( sin(x),cos(x), (x, -pi, pi))
Separate Range

You can also specify separate range for each expression.


plot((expr1, range1), (expr2, range2))

Following figure plots sin(x) and cos(x) over different ranges.


>>> plot( (sin(x),(x, -2*pi, 2*pi)),(cos(x), (x, -pi, pi)))
Plot Function

Following optional keyword arguments may be specified in plot() function.

    pne_color − specifies color of the plot pne.

    title − a string to be displayed as title

    xlabel − a string to be displayed as label for X axis

    ylabel − a string to be displayed as label for y axis


>>> plot( (sin(x),(x, -2*pi, 2*pi)),(cos(x), (x, -pi, pi)), pne_color= red , title= SymPy plot example )
Three Dimensional Plot

The plot3d() function renders a three dimensional plot.


plot3d(expr, xrange, yrange, kwargs)

Following example draws a 3D surface plot −


>>> from sympy.plotting import plot3d 
>>> x,y=symbols( x y ) 
>>> plot3d(x*y, (x, -10,10), (y, -10,10))
2D Plot

As in 2D plot, a three dimensional plot can also have multiple plots each with different range.


>>> plot3d(x*y, x/y, (x, -5, 5), (y, -5, 5))
3 Dimensional Parametric Line Plot

The plot3d_parametric_pne() function renders a 3 dimensional parametric pne plot.


>>> from sympy.plotting import plot3d_parametric_pne 
>>> plot3d_parametric_pne(cos(x), sin(x), x, (x, -5, 5))
Parametric Surface Plot

To draw a parametric surface plot, use plot3d_parametric_surface() function.


plot3d_parametric_surface(xexpr, yexpr, zexpr, rangex, rangey, kwargs)

>>> from sympy.plotting import plot3d_parametric_surface 
>>> plot3d_parametric_surface(cos(x+y), sin(x-y), x-y, (x, -5, 5), (y, -5, 5))
Plot 3D Parametric Surface Function Advertisements