.. _convenience-api:
With Toyplot, a figure always consists of three parts:
canvas <toyplot.canvas.Canvas>
axes <toyplot.axes>
added to the canvas.marks <toyplot.mark>
added to the axes.Creating these entities separately gives you the maximum flexibility, allowing you to add multiple axes (even overlapping axes) to one canvas, splitting the marks among the different axes, etc. However for simple figures containing a single set of axes and a single mark, this way of working can be tedious. Toyplot's convenience API combines the three calls to create canvas, axes, and mark into a single function that can handle many of your plotting needs with a minimum of code.
Consider the following verbose example:
In [1]:
import numpy
y = numpy.linspace(0, 1, 20) ** 2
In [2]:
import toyplot
canvas = toyplot.Canvas(width=300)
axes = canvas.axes()
axes.plot(y);
Using the convenience API, it can be reduced to a single call to :py:func:toyplot.plot
:
In [3]:
canvas, axes, mark = toyplot.plot(y, width=300)
Of course, if you're using the convenience API there's a good chance you don't need the function's return value (a (canvas, axes, mark) tuple) at all, making it even more compact:
In [4]:
toyplot.plot(y, width=300);
If you check the reference documentation for :py:func:toyplot.plot
, you will see that its parameters include the union of the parameters for :py:class:toyplot.canvas.Canvas
, :py:meth:toyplot.canvas.Canvas.axes
, and :py:meth:toyplot.axes.Cartesian.plot
, except where parameter names might conflict.
Similar convenience API functions are provided for :py:func:bar <toyplot.bars>
, :py:func:fill <toyplot.fill>
, and :py:func:scatter <toyplot.scatterplot>
plots:
In [5]:
toyplot.bars(y, width=300);
In [6]:
toyplot.fill(numpy.column_stack((y, y*2)), width=300);
In [7]:
numpy.random.seed(1234)
toyplot.scatterplot(numpy.random.normal(size=50), width=300);
In [8]:
toyplot.matrix(numpy.random.normal(size=(10, 10)), width=300);
In [9]:
columns = ["Year", "MPG", "Model"]
canvas, table = toyplot.table(toyplot.data.read_csv("cars.csv").columns(columns)[:10], width=300)
table.column(2).width = 130
If you need greater control over the positioning of the axes within the canvas, need to add multiple axes to one canvas, or need to add multiple marks to one set of axes, you'll have to create the canvas and axes explicitly.