Getting Started With matplotlib

Getting Started With matplotlib#

For our purposes, a plot is defined as “a graphic representation (such as a chart)” (Merriam Webster). These graphic representations of data are often called charts, graphs, figures, etc. In the context of programming, computer science, and data science, we refer to these as plots. We can generate plots for data stored in pandas using the matplotlib package.

matplotlib was developed in 2002 as a MATLAB-like plotting interface for Python.

  • “Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python…Matplotlib produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits” (Matplotlib documentation, Github)

As described by the original developer John Hunter, “Matplotlib is a library for making 2D plots of arrays in Python. Although it has its origins in emulating the MATLAB graphics commands, it is independent of MATLAB, and can be used in a Pythonic, object oriented way. Although Matplotlib is written primarily in pure Python, it makes heavy use of NumPy and other extension code to provide good performance even for large arrays. Matplotlib is designed with the philosophy that you should be able to create simple plots with just a few commands, or just one! If you want to see a histogram of your data, you shouldn’t need to instantiate objects, call methods, set properties, and so on; it should just work.”

  • For more on matplotlib’s development and history: John Hunter, “History” Matplotlib (2008)

To be able to call the matplotlib API (application programming interface) within Python, we need to make sure the package is installed and loaded.

  • To install at the command line: pip install matplotlib

  • To load in a .py script: import matplotlib.pyplot as plot

  • To work with matplotlib from a Jupyter notebook: %matplotlib notebook

NOTE: If your kernel dies when you try to run the code below, run conda install freetype=2.10.4 in a terminal and restart the Jupyter Notebook kernel. You will need to launch the terminal as an administrator to be able to run the command successfully.