Cadabra
a field-theory motivated approach to computer algebra

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 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. You can specify the number of samples with the optional parameter.
set_plot_backend(Backend.MATPLOTLIB) plot($\cos(x)*\exp(-x**2/5)$, ($x$, 0, 10), grid=True, xlabel="x-axis", ylabel="function value", title="Sample plot", yrange=[-1,1]);
plot([$\cos(x)*\exp(-x**2/5)$, $\sin(x)$], ($x$, 0, 10), grid=True);
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($\sin(x)$, $\cos(x)$, ($x$,0,6.28));

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));
Copyright © 2001-2024 Kasper Peeters
Questions? info@cadabra.science