Introducing the basics of matplotlib

Visualization is a fundamental aspect of data science, allowing data scientists to better and more effectively communicate their findings to the organization they operate in, to both data experts and non-experts. Providing the nuts and bolts of the principles behind communicating information and crafting engaging beautiful visualizations is beyond the scope of our book, but we can recommend suitable resources if you want to improve your skills.

For basic visualization rules, you can visit https://lifehacker.com/5909501/how-to-choose-the-best-chart-for-your-data. We also recommend the books of Prof. Edward Tufte on analytic design and visualization.

We can instead provide a fast and to-the-point series of essential recipes that can get you started on visualization using Python, and  that you can refer to anytime you need to create a specific graphics chart. Consider all the snippets of code as your visualization building blocks; you can arrange them with different configurations and features just by using the large choice of parameters that we are going to present to you.

matplotlib is a Python package for plotting graphics. Created by John Hunter, it has been developed in order to address a lack of integration between Python and external software with graphical capabilities, such as MATLAB or gnuplot. Greatly influenced by MATLAB's way of operating and functions, matplotlib presents a quite similar syntax. In particular, the matplotlib.pyplot module, perfectly compatible with MATLAB, will be the core of our essential introduction to all the indispensable graphical tools to represent your data and analysis. MATLAB is indeed a standard for visualization in the data analysis and scientific community because of its recognized capabilities when it comes to exploratory analysis, mainly due to its smooth and easy to use plotting functions.

Each pyplot command makes a change on an initially instantiated figure. Once you set a figure, all additional commands will operate on it. Thus, it is easy to incrementally improve and enrich your graphic representation. In order for you to take advantage of the code and be able to personalize it to your needs, all the following examples are presented together with commented building blocks so that you can later draft your basic representation, and then look through this chapter for specific parameters among the examples in order to improve your chart as you planned it.

With the pyplot.figure() command, you can initialize a new visualization, though it suffices to call a plotting command to automatically start it. Instead, by using pyplot.show(), you close the figure that you were operating on, and you can open and operate on new figures.

Before starting with a few visualization examples, let's import the necessary packages in order to run all the examples:

In: import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

In this way, we can always refer to pyplot, the MATLAB-like module, as plt, and access the complete matplotlib functionality set with the help of mpl.

If you are using a Jupyter Notebook (or Jupyter Lab), you can use this line magic: %matplotlib inline. After writing the command in a cell of the notebook and running it, you can have your plots drawn directly on the notebook itself, instead of having the graphics presented in a separate window (by default, the GUI backend of matplotlib is the TkAgg backend). If you prefer a different backend such as Qt (www.qt.io), which is often distributed with Python scientific distributions, you just have to run this line magic instead: %matplotlib Qt.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset