Beautiful seahorse cartoon vector illustration | Free Vector

Hi Again,

In this tutorial, you will learn how to create high quality line charts with seaborn in Python.

Set up the notebook

You can set up the notebook as explained in previous article, in case you missed it, here is the link

Select a dataset

the data set for this tutorial will be the data from the Los Angeles Data Portal that tracks monthly visitors to each museum. you can filter out on the museums you are interested to work on.

Load the data

As you learned in the previous tutorial, we load the dataset using the pd.read_csv command.In [1]:

# Path of the file to read
museum_filepath = "../input/museum_visitors.csv"

# read the file into a variable museum_data
museum_data = pd.read_csv(museum_filepath, index_col="Date", parse_dates=True)

The end result of running both lines of code above is that we can now access the dataset by using museum_data.

Examine the data

We can print the first five rows of the dataset by using the head command that you learned about in the previous tutorial.In [2]:

# Print the last 5 rows of the data
museum_data.tail()

Out[2]:

Empty entries will appear as NaN, which is short for “Not a Number”.

We can also take a look at the first five rows of the data by making only one small change (where .tail() becomes .head()):In [2]:

Plot the data

Now that the dataset is loaded into the notebook, we need only a few lines of code to make a line chart!In [3]:

# Line chart showing daily global streams of each song 
# Set the width and height of the figure 
plt.figure(figsize=(12,6)) 

# Line chart showing the number of visitors to each museum over time 
sns.lineplot(data=museum_data) 

# Add title 
plt.title("Monthly Visitors to Los Angeles City Museums")

As you can see above, the line of code is relatively short and has three main components:

  • plt.figure tells the notebook to setup the figure size of the plot being generated. The first line of code sets the size of the figure to 12 inches (in width) by 6 inches (in height)
  • sns.lineplot tells the notebook that we want to create a line chart.
    • Note: Every command that you learn here will start with sns, which indicates that the command comes from the seaborn package. For instance, we use sns.lineplot to make line charts. Soon, you’ll learn that we use sns.barplot and sns.heatmap to make bar charts and heatmaps, respectively.
  • data=museum_data selects the data that will be used to create the chart.
  • plt.title tells the notebook to sets the title of the plot to whatever is written inside the brackets.

Note that you will always use this same format when you create a line chart, and the only thing that changes with a new dataset is the name of the dataset. So, if you were working with a different dataset named financial_data, for instance, the line of code would appear as follows:

sns.lineplot(data=financial_data)

Sometimes there are additional details we’d like to modify, like the size of the figure and the title of the chart. Each of these options can easily be set with a single line of code.In [3]:

Plotting a subset of the data

So far, you’ve learned how to plot a line for every column in the dataset. What if, you need to plot only a subset of the data.

In the next code cell, we plot the lines corresponding to the first two columns in the dataset.

# Set the width and height of the figure
plt.figure(figsize=(12,6)) 
# Add title 
plt.title("Monthly Visitors to Avila Adobe") 
# Line chart showing the number of visitors to Avila Adobe over time 
sns.lineplot(data=museum_data['Avila Adobe']) 
# Add label for horizontal axis 
plt.xlabel("Date")

This line looks really similar to the code we used when we plotted every line in the dataset, but it has a few key differences:

  • Instead of setting data=museum_data, we set data=museum_data['Avila Adobe']. In general, to plot only a single column, we use this format with putting the name of the column in single quotes and enclosing it in square brackets. (To make sure that you correctly specify the name of the column, you can print the list of all column names using the command you learned above.)
  • We also add label="Avila Adobe" to make the line appear in the legend and set its corresponding label.

The final line of code modifies the label for the horizontal axis (or x-axis), where the desired label is placed in quotation marks ("...").

Stay tuned as we dive deeper into the ocean to bring out more plotting capabilities.