_images/toyplot.png

Convenience API

With Toyplot, a figure always consists of three parts:

Creating these entities separately gives you the maximum flexibility, allowing you to add multiple (even overlapping) coordinate systems to one canvas, splitting the marks among the different coordinate systems, etc. However for simple figures containing a single coordinate system and a single mark, this way of working can be tedious. Toyplot’s convenience API combines the three calls to create canvas, coordinate system, and mark into a single function that can handle many of your plotting needs with a minimum of code.

Consider the following verbose example:

import numpy
y = numpy.linspace(0, 1, 20) ** 2
import toyplot
canvas = toyplot.Canvas(width=300)
axes = canvas.cartesian()
axes.plot(y);
051015200.00.51.0

Using the convenience API, it can be reduced to a single call to toyplot.plot():

canvas, axes, mark = toyplot.plot(y, width=300)
051015200.00.51.0

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:

toyplot.plot(y, width=300);
051015200.00.51.0

If you check the reference documentation for toyplot.plot(), you will see that its parameters include the union of the parameters for toyplot.canvas.Canvas, toyplot.canvas.Canvas.cartesian(), and toyplot.coordinates.Cartesian.plot(), except where parameter names might conflict.

Similar convenience API functions are provided for bar, fill, and scatter plots:

toyplot.bars(y, width=300);
010200.00.51.0
toyplot.fill(numpy.column_stack((y, y*2)), width=300);
051015200.00.51.01.52.0
numpy.random.seed(1234)
toyplot.scatterplot(numpy.random.normal(size=50), width=300);
01020304050-202
toyplot.matrix(numpy.random.normal(size=(10, 10)), width=300);
012345678900.841008794931391-1.4458100770443063-1.4019732815008439-0.10091819994891389-0.5482424491868549-0.144619508369384360.3540203321992379-0.03551302527814020.56573830606259511.54565880462555751-0.9742363337673154-0.070344877104102420.30796885521603423-0.208498763105879751.0338007325554992-2.40045363381229572.0306036208387996-1.14263128902276350.211883386777701050.70472062431710882-0.7854352117631970.46205973716204870.70422822546217430.5235079678938094-0.92625431353022592.00784295077800050.2269625418708953-1.15265910925095240.63197944580912950.039512686693366730.46439232505089606-3.56351666062473531.32110561547020590.152630552204534480.16452954293239852-0.43009569087648760.76736873575241150.98491984190989690.2708358488268041.391986193446407340.07984231300862901-0.3999645806965225-1.0278505586819058-0.58471821126078830.8165939265478418-0.08194705182666534-0.34476601425464430.5282881452973941-1.0689887834801322-0.511881309126815150.29120535974306350.56653369635357240.5035917591112030.28529568478185710.484288112749751.3634815124261457-0.781105283625392-0.46801766633748551.2245743551261743-1.281108275144042660.8754755042743244-1.710715324029529-0.45076510313627440.7491638059190651-0.20393286610125122-0.182175411665734170.6806560043814565-1.81849899039161420.0470716353257110840.39484420932720437-0.24843205438084665-0.6177066479970167-0.6828839964493340.4362576043409168-1.7030127741132380.3937105991386652-0.47932400357549726-0.29901629296608040.69410328767876430.678629673709856580.23955599500389690.151226629294449830.81612723336004091.89353446759620070.6396327631937027-0.9620288319051914-2.08526564212010971.9302467674655757-1.73534887447039271.210383704904514390.7974354194278735-0.3798107840473790.7025622240016004-0.8503462716551151.1768124501049289-0.5243361026324560.70090773091560470.9841880707224154-0.121728408666820222.365768628840039
data = toyplot.data.cars()
columns = ["Year", "MPG", "Model"]
canvas, table = toyplot.table(data[:10, columns], width=300)
table.cells.column[2].width = 130
YearMPGModel7018chevrolet chevelle malibu7015buick skylark 3207018plymouth satellite7016amc rebel sst7017ford torino7015ford galaxie 5007014chevrolet impala7014plymouth fury iii7014pontiac catalina7015amc ambassador dpl

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.