Have you ever spent way too long digging through documentation, trying to get your plots to look the way you want? Next time it happens, consider ditching matplotlib or seaborn and using Jinja to create a custom plot.

Jinja is a Python templating language, based on Django templates, which you may be familiar with from other packages like Flask or Airflow. It has a ton of useful features but you only need some basics to get started.

You can use Jinja to make your visual as an SVG (with a workflow that’s pretty similar to D3’s) and display them, all while staying in Jupyter.

Some Basic SVG

Before we get into the big stuff, let me cover some basics. SVG stands for Scalable Vector Graphics, meaning all the image data is stored as points in vector space and therefore can be resized without getting pixelated (unlike raster images).

SVGs are XML-based and have many different tags, representing different paths and shapes. Some key tags are:

  • circle: Draws a circle using xy coordinates and a radius
  • rect: Draws a rectangle using xy coordinates for upper-left corner plus width and height
  • polyline: Draws a path of connected xy points
  • text: Draws text

Each SVG element has its own attributes that can be changed but most elements share common attributes to control the fill and stroke.

One other key thing to keep in mind when creating SVGs is that the xy coordinates start at (0,0) in the upper-left corner and increase as they move towards the bottom-right corner.

For more information on working with SVGs check out Mozilla’s reference material here.

#python #jinja #data-visualization #data-science #svg #programming

Take Full Control of Your Python Plots with Jinja
13.20 GEEK