PSDV_Assignment_4

Visualizing Data with Toyplot

Toyplot: A Kid-Sized Plotting Toolkit for Python with Grownup-Sized Goals

Introduction

Toyplot is a powerful Python library designed to create simple, interactive, and publication-quality visualizations. It enables users to generate a variety of plots such as line charts, bar charts, scatter plots, and more, with an easy-to-use API that allows for quick implementation and customization. Unlike other traditional plotting libraries like Matplotlib, Toyplot emphasizes interactivity, smooth animations, web embedding capabilities, and clean, concise code. This makes it a compelling choice for data scientists, researchers, and developers who need an intuitive plotting tool with advanced features that work well with modern web technologies.

Toyplot stands out because of its ability to export plots as interactive HTML, generate SVG-based high-quality visuals, and provide built-in features like tooltips and animations. This makes it ideal for web-based projects, real-time data monitoring, business dashboards, and scientific visualizations, which require dynamic, easy-to-understand visual feedback.

Installation & Setup

To install Toyplot, run:

pip install toyplot

After installation, import it in your Python script or Jupyter Notebook:

import toyplot
import numpy as np

Key Features

1. Simple and concise API

2. Interactive elements

3. Multiple chart types Toyplot supports a wide variety of visualizations, including:

4. Web-friendly embedding

5. Built-in data exporting

Code Examples

1. Lightweight and Minimalist

Toyplot requires minimal setup to generate clean, high-quality plots.

import toyplot

# Create a simple line plot
canvas = toyplot.Canvas(width=500, height=300)
axes = canvas.cartesian()
axes.plot([1, 2, 3, 4, 5], [10, 15, 5, 20, 10], color="blue")

canvas

123455101520

A clean, simple line plot.

2. Interactive & Web-Friendly (Tooltips Example)

import toyplot
import numpy as np

# Define five points
x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 3, 8, 6, 4])

# Create a canvas
canvas = toyplot.Canvas(width=400, height=300)

# Create an axis
axes = canvas.cartesian()

# Create a scatter plot with tooltips
scatterplot = axes.scatterplot(
    x, y, color="blue", size=10, title=[f"({xi}, {yi})" for xi, yi in zip(x, y)]
)

# Display the plot
canvas

(1, 5)(2, 3)(3, 8)(4, 6)(5, 4)12345345678

Hovering over a point shows its tooltip. (in this case coordinates of the point)

3. Declarative API (Multiple Series in One Plot)

import toyplot

# Create a canvas
canvas = toyplot.Canvas(width=600, height=400)
axes = canvas.cartesian()

# Define multiple series
series_1 = axes.plot([1, 2, 3, 4, 5], [10, 15, 5, 20, 10], color="blue")
series_2 = axes.plot([1, 2, 3, 4, 5], [5, 10, 15, 10, 5], color="green")

canvas

123455101520

Two line plots with automatic legend.

4. Multiple Coordinate Systems (Logarithmic Scale Example)

import toyplot

canvas = toyplot.Canvas(width=600, height=400)
axes = canvas.cartesian(xscale="log", yscale="log")

# Logarithmic plot
axes.plot([1, 10, 100, 1000], [1, 10, 100, 1000], color="purple")

canvas

10 010 110 210 310 010 110 210 3

Logarithmic plot on both axes, while the same plot requires more than 20 lines of code in matplotlib

5. High-Quality Vector Output (Exporting to PDF/SVG)

import toyplot.pdf

canvas = toyplot.Canvas(width=600, height=400)
axes = canvas.cartesian()
axes.plot([1, 2, 3, 4], [5, 10, 15, 20])

# Save as PDF
toyplot.pdf.render(canvas, "output.pdf")

A high-resolution PDF file is generated.

6. Supports Animation (Time-Series Example)

import numpy
import toyplot

canvas = toyplot.Canvas(width=600, height=400)
axes = canvas.cartesian()

# Animated sine wave
x = numpy.linspace(0, 2 * numpy.pi, 50)
y = numpy.sin(x)

axes.plot(x, y, color="blue")

canvas

0246-1.0-0.50.00.51.0

A dynamic sine wave plot.

7. Animated Line Plot ( Saved as HTML and Opened in Browser)

import toyplot
import numpy as np
import webbrowser

# Create a canvas
canvas = toyplot.Canvas(width=600, height=400)
axes = canvas.cartesian()

# Data points
x = np.array([1, 2, 3, 4, 5])
frames = 10

# Generate animated y-values (simulating changes over time)
y1_frames = np.column_stack([np.array([3, 7, 2, 8, 6]) + np.random.rand(5) * 2 for _ in range(frames)])
y2_frames = np.column_stack([np.array([4, 6, 3, 9, 7]) + np.random.rand(5) * 2 for _ in range(frames)])

# Plot the animated lines
axes.plot(x, y1_frames, color="blue", style={"stroke-width": "2px"})
axes.plot(x, y2_frames, color="red", style={"stroke-width": "2px"})

# Save as HTML
html_file = "animated_plot.html"
toyplot.html.render(canvas, html_file)

# Automatically open in the default browser
webbrowser.open(html_file)

print(f"Animated plot saved as {html_file} and opened in the browser.")

Animated plot saved as animated_plot.html and opened in the browser.

HTML file gets saved and is opened in the browser

Use Cases: Practical Applications

1. Interactive Data Dashboards

2. Scientific Data Visualization

3. Business Intelligence & Reports

4. IoT & Real-Time Monitoring

Best for: Scientists, data analysts, web developers, and educators looking for interactive, publication-ready plots.

Conclusion

Toyplot is a modern, interactive, and lightweight alternative to Matplotlib, making it a powerful choice for web-based, scientific, and business visualization needs. With features like built-in interactivity, HTML export, and seamless Jupyter integration, it outshines traditional tools in areas that demand responsiveness and ease of use.

References & Further Reading

Official Toyplot Documentation: https://toyplot.readthedocs.io/en/stable/

GitHub Repository: https://github.com/sandialabs/toyplot