Get startedGet started for free

Creating line plots

1. Creating line plots

In this video, we'll learn how to create line plots using another Python module: matplotlib. You've seen matplotlib before in Chapter 1, when you learned about the function plt-dot-plot. Recall that plt is the alias that we use when importing matplotlib.

2. The plot thickens

We're close to solving the mystery of Bayes, the kidnapped golden retriever. Remember that a ransom note was found at the scene of the crime. In the previous chapter, we narrowed the list of suspects to Freddie Frequentist and Gertrude Cox. We've asked Freddie and Gertrude to give us writing samples to compare against the ransom note. We'll compare the letter frequencies in Freddie and Gertrude's samples to the note found at the scene of the crime. We'll do this by creating line plots.

3. From DataFrame to Visualization

We've stored the letter frequencies in a Pandas DataFrame. We'll need to transform that DataFrame into a two-dimensional line plot. A line plot uses a coordinate grid to plot a series of points and then connects each point using a line. In this case, the x-axis represents the letters of the alphabet and the y-axis represents the frequency of each letter.

4. Introducing Matplotlib

In order to create a line plot, we'll need to import the module matplotlib. Generally, Data Scientists only load in the submodule pyplot and use the alias plt. We do this by typing "from matplotlib import pyplot as plt". Once we've loaded our module, we can use the functions plt-dot-plot and plt-dot-show to create and display our line plot.

5. Line Plot

Let's examine the line plot function. We begin with the name of the function: plt-dot-plot Like all functions, we'll be putting the arguments inside of parentheses. The first argument is the x-values we want to plot. In this case, we use the letter column from the DataFrame ransom. Recall that we select a column by typing the name of the DataFrame, followed by a dot, followed by the column name. The second argument is the y-values we want to plot. In this case, we use the frequency column from the DataFrame ransom. Again, we type DataFrame name, then dot, then column name. Finally, we close the parentheses.

6. Displaying the Results

If we only type plt-dot-plot, nothing will show up. That's because Python wants to give us the opportunity to enter other commands. For example, we might want to add another line or a title for the plot. When we want to display everything that we've made, we use a second function: plt-dot-show. This function takes no arguments. You just type it, and then your plot will pop open in a new window.

7. Multiple Lines

If we want to plot multiple lines on the same axis, we can just add a second plt-dot-plot command before adding plt-dot-show. In this example, we plotted two different datasets: data1, which represents a straight line and data2, which represents a parabola. Matplotlib will automatically give the two lines different colors.

8. Let's practice!

Let's practice making line plots!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.