7 Examples to Master Line Plots With Python Seaborn | by Soner Yıldırım | Towards Data Science

Data visualization is an integral part of data science. It helps us explore a dataset and the relationships between variables to create proper data visualizations. They are also highly efficient tools to deliver results and findings.
There are several different kinds of data visualizations. One of the most commonly used ones is line plot which is used for analyzing the relationships between two continuous variables.
In this article, we will go over 7 examples to explain in detail how to create line plots with the Seaborn library of Python.
The main use case for line plots is time series analysis. It shows how the value of a variable changes over time. For instance, we can use line plots to visualize stock prices over a period of time.
Let’s start with acquiring some stock price data. We will be using the pandas datareader library to get the data. The first step is to import the libraries.
import pandas as pdfrom pandas_datareader import dataimport seaborn as snssns.set(style="darkgrid")
The following code snippet collects the stock prices of Apple, Microsoft, Google, and Moderna between the given start and end dates. The date, closing price, volume, and stock name are saved in the stocks data frame.
start = '2020-1-1'end = '2021-6-30'source = 'yahoo'stocks = pd.DataFrame(columns=["Date","Close","Volume","Stock"])stock_list = ["AAPL","IBM","MSFT","MRNA"]for stock in stock_list:    df = data.DataReader(stock, start=start ,end=end,                          data_source=source).reset_index()    df["Stock"] = stock    df = df[["Date","Close","Volume","Stock"]]    stocks = pd.concat([stocks, df], ignore_index=True)stocks.head()
In the first example, we create a line that shows the daily stock prices of Apple. We can either use the relplot or lineplot functions of Seaborn.
The relplot function is a figure-level interface for drawing relational plots including line plot and scatter plot. The lineplot function can be used for drawing line plots as well.
sns.relplot(    data=stocks[stocks.Stock == "AAPL"],     x="Date", y="Close",     kind="line")
The name of the data frame is passed to the data parameter. Since we only want to see Apple stock prices, we should filter the data points using the stock name. The x and y parameters are used for selecting what to plot on the x and y axis. Finally, the type of plot is determined with the kind parameter. Here is the plot returned by this code:
It will definitely look better if we make it wider because the dates cannot be read.
In this example, we will see how to adjust the size of the plots. The relplot function has two parameters that can be used for updating the default plot size.
The height parameter changes the height of the plot. The aspect parameter adjusts the width over height ratio.
sns.relplot(    data=stocks[stocks.Stock == "AAPL"],     x="Date", y="Close",    kind="line",    height=5, aspect=2 )
It looks much better now.
In this example, we will explore the hue parameter. The original data frame contains the stock prices of 4 different companies over the same time period. We may need to plot them in the same visualization to make a comparison. We can do that by passing the column name that contains the stock names to the hue parameter.
sns.relplot(    data=stocks,     x="Date", y="Close", hue="Stock",     height=5, aspect=2,     kind="line")
We observe the big jump on the Moderna stock prices due to the Covid-19 vaccination.
In this example, we will do some modifications on the appearance. For instance, we can increase the font size of the axis titles and legend. Another modification can be on the color palette of the lines.
sns.set(font_scale=1.5)sns.relplot(    data=stocks,     x="Date", y="Close", hue="Stock",     height=5, aspect=2.4,     kind="line",    palette="cool")
There are many options for the palette parameter. If you are not sure about the name of a palette or try out different ones, just type a wrong one. In the error message, you will see all the options :)
Let’s also add a title to our plot to make it look better.
sns.relplot(    data=stocks,     x="Date", y="Close", hue="Stock",    height=5, aspect=2.4,     kind="line").set(title="Stock Prices")
We can also change the axes labels the same way as the title. For instance, it might be more informative to change the y axis as “closing price”. It is crystal clear that the x axis represents the date so we do not actually need a label for it.
sns.relplot(    data=stocks,     x="Date", y="Close", hue="Stock",    height=5, aspect=2.4,     kind="line").set(    title="Stock Prices",     ylabel="Closing Price",    xlabel=None)
The relplot function also allows for creating subplots within a single visualization. For instance, we can create a line plot for each stock. The row and col parameters can be used to accomplish this task.
sns.relplot(    data=stocks, x="Date", y="Close",     row="Stock",    height=3, aspect=3.5,    kind="line")
The col parameter is quite similar. It creates a new column for each plot. Since the width is large compared to the height, the row is more suitable in our case.
The relplot function provides a simple and intuitive method for creating relational plots. It is also quite easy to modify the certain features of a plot such as font size, title, axes labels, and so on.
What we have covered in this article should be enough for most of your needs. However, if you need to do a more specific operation, you can always visit the official documentation.
Thank you for reading. Please let me know if you have any feedback.