Python Tutorial: Introduction to data visualization with Matplotlib

Learn Matplotlib's object-oriented interface to build charts from scratch. The tutorial uses weather data for Seattle and Austin to show how plt.subplots() creates a Figure and Axes, then how Axes.plot() adds data and plt.show() displays it. Visualizing data reveals trends that raw tables hide.
Chapters
Hello and welcome to this course on data visualization. With matplotlib, a picture is worth a thousand words. Data visualizations let you derive insights from data and let you communicate about the data with others. For example, this visualization shows an animated history of an outbreak of Ebola in West Africa. The amount of information in this complex visualization is simply staggering. This visualization was created using matplotlib, a Python library that is widely used to visualize data. There are many software libraries that visualize data. One of the main advantages of matplotlib is that it gives you complete control over the properties of your plot. This this allows you to customize and control the precise properties of your visualizations. At the end of this course, you will know not only how to control your visualizations, but also how to create programs that automatically create visualizations based on your data. There are many different ways to use matplotlib. In this course we will use the main object oriented interface. This interface is provided through the Pyplot submodule. Here we import this submodule and name it plt. While using the name PLT is not necessary for the program to work, this is a very strongly followed convention and we will follow it here as well. The PLT subplots command, when called without any inputs, creates two different objects, a figure object and an axis object. The figure object is a container that holds everything that you see on the page. Meanwhile, the axis is the part of the page that holds the data. It is the canvas on which we will draw with our data to visualize it. Here you can see a figure with empty axes. No data has been added yet. Let's add some data to our figure. Here's some data. This is a data frame that contains information about the weather in the city of Seattle in the different months of the year. The month column contains the three letter names of the months of the year. The monthly average normal temperature column contains the temperatures in these months in Fahrenheit degrees averaged over a 10 year period. To add the data to the axes we call a plotting command. The plotting commands are methods of the axes object. For example, here we call the method called Plot with the month column as the first argument and the temperature column as the second argument. Finally, we call the PLT show function to show the effect of the plotting command. This adds a line to the plot. The horizontal dimension of the plot represents the months according to their order and the height of the line at each month represents the average temperature. The trends in the data are now much clearer than they were just by reading off the temperatures from the table. If you want, you can add more data to the plot. For example, we also have a table that stores data about the average temperatures in the city of Austin, Texas. We add these data to the axes by calling the plot method. Again, here is what all of the code to create this figure would then look like. First we create the figure and the axes objects. We call the axes method plot to add first the Seattle temperatures and then the Austin temperatures to the axes. Finally, we ask matplotlib to show us the figure. Now it's your turn in the exercises. You will practice making a figure, an axes, and and adding data into them.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







