Quick Success Data Science
An overview for Python beginners
If you’re learning 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 encoding 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 range of plots, from simple to sophisticated, can be created using a concise grammar.”
Scientists and engineers should find the declarative approach enticing, as it leads to more time doing their real jobs and less time coding. As I like to say, “Science first, coding second!”
In this Quick Success Data Science 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 users 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.