New Plotting Framework for SymPy

Structure of the Module

This module implements a new plotting framework for SymPy. The central class of the module is the Plot class that connects the data representations (subclasses of BaseSeries) with different plotting backends. It's not imported by default for backward compatibility with the old module.

Then there are the plot_*() functions for plotting different kinds of plots and is better suited for interactive work.

  • plot: Plots line plots in 2D.

  • plot_parametric: Plots parametric line plots in 2D.

  • plot_implicit : Plots implicit equations and region plots in 2D

  • plot3d : Plots functions of two variables in 3D

  • plot3d_parametric_line: Plots line parametric plots in 3D

  • plot3d_parametric_surface : Plots surface parametric plots of functions with two variables in 3D.

General examples


In [8]:
from sympy.plotting import plot, plot_parametric, plot3d, plot3d_parametric_line, plot3d_parametric_surface

In [9]:
p = plot(x)

In [10]:
p # the Plot object

In [11]:
p[0] # one of the data series objects

In [12]:
p[0].label # an option of the data series

In [13]:
p.legend # a global option of the plot

In [14]:
p.legend = True

p.show()

You can plot 2D different functions in the same plot.


In [15]:
p1 = plot_parametric(x*sin(x),x*cos(x), show=False)

p1.extend(p) # Plot objects are just like lists.

p1.show()

In [16]:
p1.legend = True

p1.show()

In [17]:
p1[0].line_color='r'

p1[1].line_color='b' # a constant color

p1.show()

In [18]:
p1[0].line_color = lambda a : a # color dependent on the parameter

p1.show()

In [19]:
p1.title = 'Big title'

p1.xlabel = 'the x axis'

p1[1].label = 'straight line'

p1.show()

In [20]:
p1.aspect_ratio

In [21]:
p1.aspect_ratio = (1,1)

p1.xlim = (-15,20)

p1.show()

Hm, xlim does not work in the notebook. Hopefully it works in IPython.


In [17]:
p1._backend.ax.get_xlim()

Yeah, the backend got the command, but the inline backend does not honour it.

Adding expressions to a plot


In [23]:
p = plot(x)

p

In [24]:
p.extend(plot(x+1, show=False))

p.show()

p

In [25]:
p.append(plot(x+3, x**2, show=False)[1])

p.show()

p

Different types of plots

plot

The plot by default uses an recursive adaptive algorithm to plot line plots. The default depth of recursion is 12, which means the function will be sampled at a maximum of $2^{12}$ points.


In [26]:
help(plot)

In [28]:
plot(sin(x**2)) # plots with adaptive sampling and default range of (-10, 10)

You can also specify the depth of the recursion. It is also possible to disable adaptive sampling and use uniform sampling with nb_of_points.


In [30]:
plot(sin(x**2), depth=7)  #specifying the depth of recursion.

In [31]:
plot(sin(x**2), adaptive=False, nb_of_points=500)

plot_parametric

plot_parametric uses an recursive adaptive sampling algorithm to plot.


In [32]:
help(plot_parametric)

In [33]:
plot_parametric(cos(x), sin(x))

Multiple plots.


In [34]:
plot_parametric((cos(x), sin(x)), (x, cos(x)))

We can combine parametric and line plots into a single plot.


In [35]:
p = plot(sin(x), show=False)



p.extend(plot_parametric(cos(x), sin(x), show=False))

p.show()

plot3d


In [36]:
help(plot3d)

In [37]:
plot3d(x*y)

In [38]:
plot3d(x*y, nb_of_points_x=100, nb_of_points_y=50)

plot3_parametric_line


In [39]:
help(plot3d_parametric_line)

In [40]:
plot3d_parametric_line(cos(x), sin(x), x)

plot3d_parametric_surface


In [41]:
help(plot3d_parametric_surface)

In [42]:
plot3d_parametric_surface(cos(x + y), sin(x - y), x - y)

Complex values

If complex values are encountered, they are discarded while plotting.


In [43]:
plot(sqrt(x), (x, -5, 5))

Textplot

There is also the textplot backend that permits plotting in the terminal.


In [44]:
pt = plot(sin(x),show=False)

In [45]:
pt.backend = plot_backends['text']

In [46]:
pt.show()