2 Plotting probabilities using Matplotlib

 

This section covers

  • Creating simple plots using Matplotlib
  • Labeling plotted data
  • What is a probability distribution?
  • Plotting and comparing multiple probability distributions

Data plots are among the most valuable tools in any data scientist’s arsenal. Without good visualizations, we are effectively crippled in our ability to glean insights from our data. Fortunately, we have at our disposal the external Python Matplotlib library, which is fully optimized for outputting high-caliber plots and data visualizations. In this section, we use Matplotlib to better comprehend the coin-flip probabilities that we computed in section 1.

2.1 Basic Matplotlib plots

Let’s begin by installing the Matplotlib library.

Note

Call pip install matplotlib from the command line terminal to install the Matplotlib library.

Once installation is complete, import matplotlib.pyplot, which is the library’s main plot-generation module. According to convention, the module is commonly imported using the shortened alias plt.

Listing 2.1 Importing Matplotlib
import matplotlib.pyplot as plt

We will now plot some data using plt.plot. That method takes as input two iterables: x and y. Calling plt.plot(x, y) prepares a 2D plot of x versus y; displaying the plot requires a subsequent call to plt.show(). Let’s assign our x to equal integers 0 through 10 and our y values to equal double the values of x. The following code visualizes that linear relationship (figure 2.1).

2.2 Plotting coin-flip probabilities

2.2.1 Comparing multiple coin-flip probability distributions

Summary