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.
from sympy.plotting import plot, plot_parametric, plot3d, plot3d_parametric_line, plot3d_parametric_surface
p = plot(x)
p # the Plot object
p[0] # one of the data series objects
p[0].label # an option of the data series
p.legend # a global option of the plot
p.legend = True
p.show()
You can plot 2D different functions in the same plot.
p1 = plot_parametric(x*sin(x),x*cos(x), show=False)
p1.extend(p) # Plot objects are just like lists.
p1.show()
p1.legend = True
p1.show()
p1[0].line_color='r'
p1[1].line_color='b' # a constant color
p1.show()
p1[0].line_color = lambda a : a # color dependent on the parameter
p1.show()
p1.title = 'Big title'
p1.xlabel = 'the x axis'
p1[1].label = 'straight line'
p1.show()
p1.aspect_ratio
p1.aspect_ratio = (1,1)
p1.xlim = (-15,20)
p1.show()
Hm, xlim does not work in the notebook. Hopefully it works in IPython.
p1._backend.ax.get_xlim()
Yeah, the backend got the command, but the inline backend does not honour it.
p = plot(x)
p
p.extend(plot(x+1, show=False))
p.show()
p
p.append(plot(x+3, x**2, show=False)[1])
p.show()
p
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.
help(plot)
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.
plot(sin(x**2), depth=7) #specifying the depth of recursion.
plot(sin(x**2), adaptive=False, nb_of_points=500)
plot_parametric¶plot_parametric uses an recursive adaptive sampling algorithm to plot.
help(plot_parametric)
plot_parametric(cos(x), sin(x))
Multiple plots.
plot_parametric((cos(x), sin(x)), (x, cos(x)))
We can combine parametric and line plots into a single plot.
p = plot(sin(x), show=False)
p.extend(plot_parametric(cos(x), sin(x), show=False))
p.show()
plot3d¶help(plot3d)
plot3d(x*y)
plot3d(x*y, nb_of_points_x=100, nb_of_points_y=50)
plot3_parametric_line¶help(plot3d_parametric_line)
plot3d_parametric_line(cos(x), sin(x), x)
plot3d_parametric_surface¶help(plot3d_parametric_surface)
plot3d_parametric_surface(cos(x + y), sin(x - y), x - y)
If complex values are encountered, they are discarded while plotting.
plot(sqrt(x), (x, -5, 5))
There is also the textplot backend that permits plotting in the terminal.
pt = plot(sin(x),show=False)
pt.backend = plot_backends['text']
pt.show()