# The Toyplot Tutorial¶

## Getting Started¶

Welcome! This tutorial will introduce you to the basics of working with Toyplot.

Note: this tutorial was created in a Jupyter notebook and assumes that you’re following-along in a notebook of your own. If you aren’t using a notebook, you should read the user guide section on Rendering for some important information on how to display your figures.

To begin, we’re going to import numpy (so we can create data to use for our figures), and the main toyplot module:

For our first figure, let’s create a simple set of X and Y coordinates that we can plot:

Now, let’s put Toyplot to work …

Short as it is, this example demonstrates several key features of Toyplot:

• Toyplot figures are beautiful, scalable, embeddable, and interactive. For example:
• Click or tap anywhere in the figure, and note the coordinates that update along each axis.
• Carefully place the cursor on top of the plotted line, and open a context menu - in the popup that appears, you can choose to save the figure data to a CSV file from your browser.
• Note that Toyplot produces a clean, aesthetically pleasing figure that is crisp at any scale and free of chartjunk “out-of-the-box”.
• Every Toyplot figure begins with a canvas - a drawing area upon which the caller adds marks. Note that the width and height of the canvas have been specified in CSS pixels, which are always equal to 1/96th of an inch, and converted to device units when the canvas is rendered.
• A Toyplot figure typically contains one-or-more coordinate systems which map a data domain to a range of canvas pixels.
• Marks are added to coordinate systems using the factory functions they provide. In this example, the plot function adds a plot mark using the supplied coordinates. Note that the cartesian axes been automatically sized to the data’s domain.

## Styles¶

Let’s say that you wanted to alter the above figure to make the plotted line blue and dashed. To do so, simply override the default style information when creating the plot:

In this case, you can see that the style information is a dictionary of key-value properties that alter how a mark is rendered. To avoid reinventing the wheel, Toyplot uses Cascading Style Sheets (CSS) to specify styles. If you’re familiar with web development, you already know CSS. If not, this tutorial will cover many of most useful CSS properties for Toyplot as we go, and there are many learning resources for CSS online.

Every mark you add to a figure will have at least one (and possibly more than one) set of styles that control its appearance.

## Plotting¶

Let’s continue with the previous example. As a shortcut, you can omit the X coordinates when using the plot command, and a set of coordinates in the range $$[0, M)$$ will be provided (compare the following X axis with the previous two plots to see the difference):

If you add multiple plots to a set of axes, each automatically receives a different color:

As we’ve already seen, we can use the “stroke” style to override the default color of each plot; in addition, the “stroke-width” and “stroke-opacity” styles are useful properties for (de)emphasizing individual plots:

## Palettes¶

Before proceeding, let’s take a moment to look at how the default color for a mark is assigned. When we add multiple marks to a set of axes, each mark gets a different color. These default colors are all drawn from a palette - an ordered collection of RGBA colors. For example, here’s Toyplot’s default palette:

Note: Like canvases, palettes are automatically rendered in Jupyter notebooks, in this case as a collection of color swatches.

You should observe that the order of colors in the palette match the order of the colors that were assigned to our plots as they were added to their axes. You could create a custom palette by passing a sequence of colors to the toyplot.color.Palette constructor, but Toyplot already comes with a builtin collection of high-quality palettes from Color Brewer, which we will use in the examples that follow.

For more detail on colors in Toyplot, see the Colors section of the user guide.

## Filled Regions¶

You can use fill to display a region bounded by two sets of Y coordinates. This can be a handy way to visualize data distributions:

Use the “fill” style (not to be confused with the fill command) to control the color of the shaded region. You might also want to change the fill-opacity or add a stroke using styles:

If you omit one of the boundaries it will default to $$y = 0$$:

As with plots, if you omit the X coordinates, they will default to the range $$[0, M)$$:

Toyplot also makes it easy to define multiple sets of boundaries, by passing an $$M \times N$$ matrix as input, where $$M$$ is the number of observations, and $$N$$ is the number of boundaries:

This introduces an important new concept: you can think of fill (and other types of) marks as containers for collections of series, where in this case, $$N$$ boundaries define $$N-1$$ series.

This distinction is important because we can control the styles of individual series, not just the mark as a whole. So, if we want to override the default colors for the fill regions, we can do it using the mark’s global “fill” style (with a contrasting stroke to display the boundaries between series):

… or we can do it using the “color” argument:

The advantage of the latter is that the “color” argument can specify a single color value as we’ve seen, or a sequence of color values, one-per-series. And, you can combine those per-series color values with global styles in intuitive ways:

The “opacity” and “title” arguments can also be specified on a per-series basis (hover the mouse over the fill regions in the following figure to see the title as a popup):

In the preceding examples you defined the fill regions by explicitly specifying their boundaries … as an alternative, you can generate fills by specifying the magnitudes (the heights) of each region (note that in this case $$N$$ heights define $$N$$ series):

If you pass a sequence of scalar values instead of colors to the “color” argument, the values will be mapped to colors using a linear mapping and a default diverging colormap:

… note that the baseline parameter is what signals that the inputs are magnitudes instead of boundaries. You can also change the baseline parameter to create various types of streamgraph:

## Barplots¶

Of course, you can’t have a plotting library without barplots

By default the bars are centered on integer X coordinates in the range $$[0, M)$$ - but we can specify our own X coordinates to suit:

As a convenience, you can pass the output from numpy.histogram() directly to toyplot.coordinates.Cartesian.bars():

As with fill marks, Toyplot allows you to stack multiple sets of bars by passing an $$M \times N$$ matrix as input, where $$M$$ is the number of observations, and $$N$$ is the number of series:

As before, we can style the bars globally and use the “color”, “opacity”, and “title” arguments to specify constant or per-series behavior:

However, with bars we can take these concepts even further to specify per-datum quantities. That is, the color, opacity, and title arguments can accept data that will apply to every individual bar in the plot. For the following example, we generate a per-datum set of random values to map to the color, and also use them as the bar titles (hover over the bars to see the titles):

## Scatterplots¶

Next on our whirlwind tour of marks is the scatterplot:

As you might expect, you can omit the X coordinates for a scatterplot, and they will fall in to the range $$[0, M)$$:

And as we’ve seen before, you can pass multiple series in a single call: