The text provides an overview of imperative and declarative plotting in Python for beginners. It discusses the use of libraries such as Matplotlib, seaborn, Plotly Express, and hvplot for creating visualizations. The text details the characteristics, strengths, weaknesses, and examples of both imperative and declarative plotting styles. Different methods and techniques for creating various plots are also presented, along with the benefits and trade-offs involved in each approach. It concludes by emphasizing the importance of customizing declarative methods to produce complete figures for publication purposes.
“`html
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. Scientists and engineers should find the declarative approach enticing, as it leads to more time doing their real jobs and less time coding.
Declarative versus Imperative: The Big Picture
Imperative Plotting
- Full control over the details of a plot.
- Step-by-step methodology is easy to grasp.
- Can require many lines of code, making programs complex and hard to read and maintain.
Declarative Plotting
- A concise, expressive, and intuitive syntax that facilitates ease of use.
- Consistency and reproducibility across plot types.
- Excellent suitability for swift exploratory data analysis plots that don’t require extensive refinement for publication.
Code Examples
To evaluate the various libraries consistently, we’ll use the same dataset and make a scatterplot. We’ll then fit a regression line to the data and add a title and legend.
Installing Libraries
We’ll use the following open-source libraries: pandas, NumPy, Matplotlib, seaborn, Plotly, hvplot, and statsmodels. You can find installation instructions in each of the previous hyperlinks.
Loading the Data
For data, we’ll use the tips dataset that comes with seaborn. This dataset records restaurant data such as the total bill, the tip amount, the day of the week, the size of the party, and so on.
Matplotlib – Imperative Example
Let’s start with the imperative library, Matplotlib. Note how we need to build a figure (fig) and axes (ax) object, create the scatterplot, build and add the regression line, and manually set the labels, title, and legend.
Seaborn — Declarative Example
As mentioned previously, seaborn is a declarative library designed to make easier, more attractive plots than native Matplotlib.
Plotly Express — Declarative Example
Plotly Express is a built-in part of the Plotly graphing library. As a simpler, higher-level version of Plotly, it’s the recommended starting point for creating common figures.
hvplot — Declarative Example
The hvplot library is a high-level declarative interface for the HoloViews plotting library. It’s designed to simplify the process of creating complex visualizations with minimal code.
Customizing Declarative Methods
By leveraging the seaborn regplot() method, you can generate your very own declarative function for making plots.
Summary
Imperative plotting in Python involves a step-by-step approach where users explicitly specify the details of the plot at a fairly low level. Declarative plotting in Python revolves around high-level methods that allow users to create basic visualizations with only a few lines of code. Customization is still permitted to various degrees.
If you want to evolve your company with AI, stay competitive, use for your advantage Declarative vs Imperative Plotting with Python.
Discover how AI can redefine your way of work. Identify Automation Opportunities: Locate key customer interaction points that can benefit from AI.
For AI KPI management advice, connect with us at hello@itinai.com. And for continuous insights into leveraging AI, stay tuned on our Telegram or Twitter.
Spotlight on a Practical AI Solution:
Consider the AI Sales Bot from itinai.com/aisalesbot designed to automate customer engagement 24/7 and manage interactions across all customer journey stages.
Discover how AI can redefine your sales processes and customer engagement. Explore solutions at itinai.com.
“`