Callysto.ca Banner

Open in Callysto

Drawing in HTML and SVG

Sometimes we just need a quick drawing within a Jupyter notebook. A quick way to do this is in an HTML cell, using Scalable Vector Graphics elements (or SVG).

The idea is straightforward. We create an html cell, put an svg canvas in it, then draw some objects in it. Here are two simple examples.

Example 1

Here we set up an svg canvas of size 100x100, and draw a circle in it. Note how we set the position and radius of the circle.

%%html
<html>
<body>

<h1>My first SVG</h1>

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

</body>
</html>

My first SVG

Example 2

Here we make a bigger canvas, add two circles and a transparent square. Notice the interesting syntax that we can use to specify the drawing style for the circles and squares.

%%html
<html>
<body>

<h1>My second SVG example</h1>

<svg width="250" height="200">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  <circle cx="150" cy="150" r="30" stroke="red" stroke-width="4" fill="green" />
  <rect x="50" y="20" width="150" height="150"
  style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
</svg>

</body>
</html>

My second SVG example

Further information

To learn more about drawing in HTML and SVG, check out this tutorial: https://www.w3schools.com/graphics/svg_intro.asp

Callysto.ca License