Matplotlib In Jupyter Notebook

matplotlib jupyter

Matplotlib is a Python library that is used often with Jupyter Notebook. The module in matplotlib that is used is called pyplot. In this tutorial, we’ll learn a little bit about matplotlib and how to use it in Jupyter Notebook. Matplotlib.pyplot provides a MATLAB-like way of plotting. This means that pyplot has many functions to make changes to a figure. Matplotlib in combination with Jupyter Notebook is a popular way to visualize data using Python for all kinds of applications in science, technology, and education.


Install Matplotlib

Make sure you first have Jupyter notebook installed, then we can add Matplotlib to our virtual environment. To do so, navigate to the command prompt and type pip install matplotlib.

pip install matplotlib

Now launch your Jupyter notebook by simply typing jupyter notebook at the command prompt.

plt.plot(x,y)

The line graph is kind of the “hello world” of matplotlib. The following code shows how to start with a very simple line graph using the x and y-axis.

matplotlib pyplot plt

The code above first imports matplotlib using import matplotlib.pyplot as plt. This is a common convention to import and alias to plt. Now we can make use of the .plot() function. This function has a number of possible parameters, but the key thing to know is that you must pass it an x and a y value. These are sequences of data. In this example, we simply pass two Python lists. The first list is x and the second list is y. These sequences should always be of equal length. At this point, we are ready to display the plot and this is done using plt.show().


Legends, Titles, and Labels

Now we can talk about three more important aspects of matplotlib. These are Legends, Titles, and Labels. It’s a good idea to have a title for your graph, labels for the x and y axes, and a legend that explains what the data is. To begin, the following code adds both an X and Y label, as well as a title to the graph.

matplotlib label and title example

A legend is useful when there is more than one sequence of data being plotted. If you have more than one line on the graph, how do you know which line represents what? This is what you use a legend for. When adding a legend, it is also important to note that you need to add a third argument to the plot() function. This is a keyword argument of label, so that the legend displays properly. Here is an example.

matplotlib legend example

Bar Charts and Histograms

Displaying a bar chart using matplotlib is done with the .bar() function.

matplotlib barchart example

We can plot more than one set of data using the bar chart, just like we did with line graphs. In the following code, we add a second set of data using the x2 and y2 variables. Also notice that we used odd numbers now for the first x variable and even numbers for the x2 variable. We need to do this so that the bars do not overlap with each other. We want them to be side by side for comparison sake, and this step accomplishes that.

matplotlib multi bar chart

Also, note that you can specify the color of the bars within the .bar() function like so.

set bar color matplotlib

Histogram

The histogram can be used to display a distribution of data. To display a histogram we can use the matplotlib .hist() function. A histogram has a concept of bins. A bin is like a slot on the graph that holds a range of data. In our example, the bins are 50000, 60000, 70000, 80000, 90000, and 100000. Now we have a list of salaries. Suppose a survey was done to check what are some common salaries in information technology. Any salaries between 50000 and 59999 should go in the 50000 bin. Any that are between 60000 and 69999 should go in the 60000, and so on.

matplotlib histogram bins

Scatter Plots

Scatter plots can be used to plot data points on a horizontal and a vertical axis to display how much one variable is affected by another. Each row in the data table is represented by a dot whose position depends on its values in the columns set on the X and Y axes. In matplotlib the .scatter() function is used to render a scatter plot.

matplotlib scatter plot

The style of the plot points can be customized. By default, it is just a simple dot as we see. In addition, the size of the marker can be adjusted. You might want to make the plot points bigger for instance. Here is an example of using a diamond shape and a larger size for the scatter plot.

jupyter notebook scatter plot

Stack Plots

Stack plots are used for displaying two or more sets of data to be shown on the same set of axes, or you want to break down one data set by their components. Different colors typically are used to distinguish the components. In our example below, we are using a stackplot over the course of five days. Some refer to a stackplot as a pie chart over time. So we have 5 days, and each day we allow a certain amount of time to various tasks. We break these up into emails, code reviews, bug reports, and internet time. So each value in each list represents how many hours spent on that task for the given day. Once we have the days, we can use the .stackplot() function to render it to the page.

matplotlib stackplot example

Pie Charts

Of all the chart types, the Pie Chart might be the most common. A pie chart looks kind of like an actual Pie, hence the name. Each slice of the pie is a data point. Pie charts are often used to display data based on percentages. Pie charts are great when there are a relatively limited number of data points to examine. If you have too many, the pie chart gets sliced so many times that the visualization gives no real benefit. Below is a redo of the stackplot example in pie chart form. Notice that when we use the .pie() function, we are passing in the sum of all hours for each activity.

matplotlib pie chart

Pretty cool! Just by looking at the pie chart, we can see how our time got divided up during the week. It looks like we had a little too much time on the Internet!

Adjusting The Start Angle and Percentages

When you render the pie chart, matplotlib simply chooses how it will orient the chart on the page. This can be specified by setting the startangle and autopct parameters.

matplotlib percentage pie chart

The pie chart now begins at 90 degrees, which is vertical. In addition, it is easy to see what percentage of time was given to each task. Emails used up 22.2% of the time, code reviews 25%, bug reports 8.3% and internet 44.4%.

Exploding a slice

You may have seen pie charts where a particular slice is being slightly removed from the pie as an accent. This gives a visual tip to key in on this data. We can do this in matplotlib using the explode parameter. Let’s explode out the bug reports from the pie.

matplotlib pie chart explode slice

Loading Data From Files

Many times matplotlib is used to display information in graph form from files on disk, or maybe even data from the internet. Let’s first look at reading data from a file, to use in matplotlib. Consider this example file on disk named fileondisk.txt.

python file on disk

What we have are 9 rows of data with 2 pieces of data separated by a comma on each row. The goal is to use Python to read the file and then plot that data in matplotlib.

matplotlib plot data from file

Looks pretty cool! The only difference in this example is that we broke apart the data on the comma from the file. The values to the left of the comma got assigned to the x list, and the values to the right of the comma got assigned to the y list. From there, we know how to use the .plot() function to simply display that data on our cool chart.

Using Numpy

Instead of using the CSV reader, we could accomplish the same thing using the popular Python library numpy. Instead of having to manually open the file and manually set up a loop to append to each list, we can use numpy to unpack the list right into each variable in one line. The resulting chart is the same.

Plotting Internet Data in matplotlib

A popular source of data to plot via the internet is stock quotes. We can download some data from Yahoo Finance and plot the result using matplotlib. The code below makes use of the urllib library to read data from the Yahoo Finance API, and then numpy is used to unpack the data into the variables we need. To plot the result, we use the .plot_date() function.

matplotlib yahoo finance api

Matplotlib Styles

One other thing to look at is customizing the styles of plots in matplotlib. Matplotlib ships with many built in styles you can use. They include the styles of Solarize_Light2, _classic_test_patch, bmh, classic, dark_background, fast, fivethirtyeight, ggplot, grayscale, seaborn, seaborn-bright, seaborn-colorblind, seaborn-dark, seaborn-dark-palette, seaborn-darkgrid, seaborn-deep, seaborn-muted, seaborn-notebook, seaborn-paper, seaborn-pastel, seaborn-poster, seaborn-talk, seaborn-ticks, seaborn-white, seaborn-whitegrid, and tableau-colorblind10. Here are a few examples of how those styles look.

seaborn-dark
matplotlib seaborn-dark

bmh
matplotlib bmh style

Solarize_Light2
matplotlib solarize_light2

dark_background
dark_background matplotlib

fivethirtyeight
fivethirtyeight matplotlib

Matplotlib XKCD Mode

Another cool feature in matplotlib is the XKCD drawing mode. This works a little differently than just applying styles as we did above, but it’s a really neat way to make your graphs have that XKCD sketch style. Here is how to do it.

matplotlib xkcd

In this example, we combine the XKCD mode with a custom style for a neat effect.

matplotlib xkcd and custom style

Additional Matplotlib Resources

Below are some additional articles and tutorials to help learn more about Matplotlib in Python.

Matplotlib In Jupyter Notebook Summary

As we have seen Matplotlib is a powerful Python library that allows us to view data in all kinds of interesting ways. By leveraging Jupyter Notebook in addition to installing Matplotlib, we set up a user-friendly way to test all of what Matplotlib has to offer. Then we learned about the various functions to use with matplotlib like .plot(), .show(), .legend(), .bar(), .hist(), .scatter(), .stackplot(), .pie(), .plot_date(), and more. In addition to this tutorial, we listed additional resources that may be helpful just above. Matplotlib also has some official tutorials in Introductory, Intermediate, and Advanced levels.