Visualizing data with Python
Contents
import numpy as np ## It's good practice to put your imports right at the top!
8. Visualizing data with Python#
Follow along!
Remember that to best make use of this tutorial, it is highly recommended that you make your own notebook and type every piece of code yourself!
At this point in the tutorial, you should now be familiar with Python variables and data types, lists and Numpy arrays, basic Python structures like loops and conditional expressions, and how to combine all these pieces into functions that you can use yourself! In this section, we’re going to learn how to make use of these functionalities and some new packages in order to generate visualizations of data.
Specifically, I want to introduce you to the Matplotlib and Seaborn packages for generating pleasant visualizations in Python. It’s worth noting that there are other plotting packages, notably Plotly and Bokeh, but after 10+ years of Python coding, I have found that the combination of Matplotlib’s low-level control and Seaborn’s high-level styling makes perfectly decent visualizations. I use these methods to generate figures both for my own quick analyses and for publication.
Situating this section in the larger tutorial, we can see this as an extension of our practice of constantly using print
to look at our variables. As we start working with data, which usually consist of more than a few pieces of information, it is going to be increasingly important to use plotting in order to see the information at hand. Furthermore, if you continue working with data, you will need to practice communicating your results and convincing people of their validity, which can most effectively be done by creating clear and precise visualizations.
8.1. Matplotlib#
The first package that we’ll learn about for visualizing data is the Matplotlib package. This is a low-level plotting package and is probably the most widely-used data visualization package in Python. In this tutorial, we’ll demonstrate the basic usage of this package and some common tips and tricks, but the entire package is vast, so we’ll leave more comprehensive instructions to others. Thankfully, because of its wide-usage, there are often examples on stackoverflow.com or you can peruse Matplotlib’s own examples.
To get started, we’ll import the main plotting module, pyplot
, and we can add the following to our module loading cell at the top of the notebook going forward:
import matplotlib.pyplot as plt ## It's become customary to use the plt abbreviation for Matplotlib
%matplotlib inline
As a reminder, the import
statement is where we tell Python that it should add these functions to the current working environment, but the %
syntax might be new to you. The %matplotlib inline
is an example of a Jupyter “magic” command, which is a