cdb.graphics.plot
Plotting of functions
Functionality to make plots of functions. Currently supports two backends, matplotlib and plotly.
Select which one of these you want by using the set_plot_backend
function with argument
Backend.MATPLOTLIB
(default) or Backend.PLOTLY
.import numpy as np
import enum
import sys
class Backend(enum.Enum):
MATPLOTLIB = "matplotlib"
PLOTLY = "plotly"
_backend = Backend.MATPLOTLIB
def set_plot_backend(backend: Backend):
global _backend
_backend = backend
plot(ex: Ex, range: tuple, grid: bool, xlabel: str, ylabel: str, title: str, samples: int) -> plot
Plot a function of one variable given the range.
The function should be a Cadabra expression (so written using dollar symbols if it is constructed inline). The range is a tuple or list with three elements: the symbol which is the dependent variable as a Cadabra expression, the starting value and the end value. If the function to plot is an interpolating function, the range can be inferred automatically. Additional options like labels and grid can be specified.set_plot_backend(Backend.MATPLOTLIB)
f = plot($f(x)=\cos(x)*\exp(-x**2/5)$, ($x$, 0, 10),
grid=True, xlabel="x-axis", ylabel="function value", title="Sample plot", yrange=[-1,1], color="green");
plot($\sin(x)$, ($x$, 0, 10), fig=f, color="red", label="red");
plot($\exp(-x)$, ($x$, 0, 10), fig=f, label="red");
#set_plot_backend(Backend.PLOTLY)
#plot($\cos(x)*\exp(-x**2/5)$, ($x$, 0, 10), grid=True, xlabel="x-axis", ylabel="y-axis", title="Sample plot");
parametric_plot(fnx: Ex, fny: Ex, range: tuple, grid: bool, xlabel: str, ylabel: str, title: str, samples: int) -> plot
Plot pairs of functions against each other.
The functions fnx
and fny
should be single functions or lists of functions, determining the $x$ and $y$ values. The range is a tuple or list with three elements: the symbol which is the
dependent variable as a Cadabra expression, the starting value and the end value. You can specify
the number of samples with the optional parameter.#set_plot_backend(Backend.MATPLOTLIB)
fig = parametric_plot($x=\sin(x)$, $y=\cos(x)$, ($x$,0,6.28), aspect='equal');
plot3d(ex: Ex, range1: tuple, range2: tuple, samples: (int,int)) -> plot
Plot a function of two variables given their ranges.
The function should be a Cadabra expression (so written using dollar symbols if it is constructed
inline). The ranges is are tuples with three elements each: the symbol which is the
dependent variable as a Cadabra expression, the starting value and the end value. You can specify the
number of samples with the optional parameter.plot3d( $\sin(x)\cos(y)$, ($x$, 0, 12), ($y$, 0, 12));