Markers¶

In Toyplot, markers are used to show the individual datums in scatterplots:

import numpy
import toyplot

y = numpy.linspace(0, 1, 20) ** 2
toyplot.scatterplot(y, width=300);


Markers can also be added to regular plots to highlight the datums (they are turned-off by default):

canvas = toyplot.Canvas(600, 300)
canvas.cartesian(grid=(1, 2, 0)).plot(y)
canvas.cartesian(grid=(1, 2, 1)).plot(y, marker="o");


You can use the size argument to control the size of the markers:

canvas = toyplot.Canvas(600, 300)
canvas.cartesian(grid=(1, 2, 0)).plot(y, marker="o", size=6)
canvas.cartesian(grid=(1, 2, 1)).plot(y, marker="o", size=10);


You can use the area argument instead of size as an approximation of the area of the markers (this is recommended if you’re using per-datum marker sizes to display data).

By default, the markers are small circles, but there are many alternatives:

canvas = toyplot.Canvas(600, 300)
canvas.cartesian(grid=(1, 2, 0)).scatterplot(y, marker="x", size=10)
canvas.cartesian(grid=(1, 2, 1)).scatterplot(y, marker="^", size=10, mstyle={"stroke":toyplot.color.near_black});


Note the use of the mstyle argument to override the appearance of the marker in the second example. For line plots, this allows you to style the lines and the markers separately:

canvas = toyplot.Canvas(600, 300)
canvas.cartesian(grid=(1, 2, 0)).plot(y, marker="o", size=8, style={"stroke":"darkgreen"})
canvas.cartesian(grid=(1, 2, 1)).plot(y, marker="o", size=8, mstyle={"stroke":"darkgreen"});


So far, we’ve been using string codes to specify different marker shapes. Here is every builtin marker shape in Toyplot, with their string codes:

markers = [None, "|","/","-","\\","+","x","*","^",">","v","<","s","d","o","oo","o|","o-","o+","ox","o*"]
mstyle = {"stroke":toyplot.color.near_black, "fill":"#feb"}
labels = [str(marker) for marker in markers]

# This is necessary because "<" and ">" are interpreted as tags when rendering text.
from xml.sax.saxutils import escape
labels = [escape(label) for label in labels]

style = {"fill":toyplot.color.near_black, "font-size":"16px", "font-family":"monospace"}

canvas = toyplot.Canvas(700, 200)
axes = canvas.cartesian(label="Marker Codes and Shapes", ymin=-0.5, ymax=1.5)
axes.x.show = False
axes.y.show = False
axes.scatterplot(numpy.repeat(0, len(markers)), marker=markers, mstyle=mstyle, size=15)
axes.text(numpy.arange(len(markers)), numpy.repeat(1, len(markers)), text=labels, style=style);


There are several items worth noting - first, you can pass a sequence of marker codes to the marker argument, to specify markers on a per-series or per-datum basis. Second, you can pass None or an empty string to produce an invisible marker, if you need to hide a datum or declutter the display. Third, observe that several of the marker shapes contain internal details that require a contrasting stroke and fill to be visible.

So far, we’ve been using the marker shape code to specify our markers, but this is actually a shortcut. A full marker specification is a Python dictionary that contains a set of specific attributes:

• “shape” - the shape code for the marker.
• “label” - optional text label superimposed on the marker.
• “mstyle” - optional dictionary of CSS styles to apply to the marker.
• “lstyle” - optional dictionary of CSS styles to apply to the marker label.
• “angle” - optional angle (in degrees) to rotate the marker.
canvas = toyplot.Canvas(600, 300)
canvas.cartesian(grid=(1, 2, 0)).scatterplot(y, marker={"shape":"|", "angle":45}, size=10)
canvas.cartesian(grid=(1, 2, 1)).scatterplot(y, marker={"shape":"o", "label":"A", "lstyle":{"fill":"white"}}, size=15);


Using the full marker specification allows you to control the style and appearance of individual markers, if needed.