Declarative vs Imperative Plotting with Python

Quick Science

An overview for Python beginners

Lee Vaughan
Towards Data Science
Two regal kings, representing declarative and imperative plotting, stand facing each other.
Declarative vs. Imperative imagined as kings by Leonardo absolute_reality_v16

If you’re Python, expect to make your first plots with Matplotlib. Besides being immensely popular, Matplotlib is an imperative plotting library. This means it generates graphics using a step-by-step approach, which is easy for beginners to grasp.

Python also supports declarative plotting libraries, such as seaborn, Altair, and HoloViews, that let you focus on what the plot should show, rather than how to draw it. To quote the Altair docs, “The key idea is that you are declaring links between data columns and visual channels, such as the x-axis, y-axis, and color. The rest of the plot details are handled automatically. Building on this declarative system, a surprising of plots, from simple to sophisticated, can be created using a concise grammar.”

Scientists and should find the declarative approach enticing, as it leads to more doing their real and less time coding. As I like to say, “Science first, coding second!”

In this Quick Success article, we’ll look at both imperative and declarative plotting. The main focus, however, will be on the declarative style, using examples from seaborn, Plotly Express, and hvplot.

Before we get into the details, here are some salient points and strengths and weaknesses of the two plotting approaches.

Imperative Plotting

Imperative plotting in Python involves a step-by-step approach where explicitly specify the details of the plot at a fairly low level. Users have direct control over each element of the plot, allowing for highly granular customization. As a result, imperative plotting requires more manual intervention compared to the declarative approach but can produce more complex visualizations.

Among the strong points of imperative plotting are:

  • Full control over the details of a plot.
  • The step-by-step methodology is easy to grasp.

Source link