|

# Matplotlib In Jupyter Notebook

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.

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.

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.

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.

## Bar Charts and Histograms

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

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.

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

### 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.

## 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.

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.

## 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.

## 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.

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.

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.

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.

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.

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 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

bmh

Solarize_Light2

dark_background

fivethirtyeight

### 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.

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