Seaborn Line Plot – Draw Multiple Line Plot | Python Seaborn Tutorial

If you have two numeric variable datasets and worry about what relationship between them. Then Python seaborn line plot function will help to find it. Seaborn library provides sns.lineplot() function to draw a line graph of two numeric variables like x and y.

Lest jump on practical.

Import Libraries

import seaborn as sns # for data visualization
import pandas as pd # for data analysis
import matplotlib.pyplot as plt # for data visualization

Python Seaborn line plot Function

Seaborn provide sns.lineplot() function to draw beautiful single and multiple line plots using its parameters.

Syntax: sns.lineplot(
                                        x=None,
                                        y=None,
                                        hue=None,
                                        size=None,
                                        style=None,
                                        data=None,
                                        palette=None,
                                        hue_order=None,
                                        hue_norm=None,
                                        sizes=None,
                                        size_order=None,
                                        size_norm=None,
                                        dashes=True,
                                        markers=None,
                                        style_order=None,
                                        units=None,
                                        estimator=’mean’,
                                        ci=95,
                                        n_boot=1000,
                                        sort=True,
                                        err_style=’band’,
                                        err_kws=None,
                                        legend=’brief’,
                                        ax=None,
                                        **kwargs,
                                       )

In python matplotlib tutorial, we learn how to draw line plot using matplotlib plt.plot() function. So, we use the same dataset which was used in the matplotlib line plot blog.

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]

#create dataframe using two list days and temperature
temp_df = pd.DataFrame({"days":days, "temperature":temperature})

# Draw line plot
sns.lineplot(x = "days", y = "temperature", data=temp_df,)
plt.show() # to show graph

Output >>>

Seaborn-line-plot

Load DataFrame from GitHub Seaborn Repository

Above temp_df dataset is insufficient to explain with sns.lineplot() function’s all parameters for that we are using another dataset. which load from GitHub seaborn Dataset repository. This repository contains lots of DataFrame ready to do operation using seaborn for visualization.

#load tips dataset from GitHub
tips_df = sns.load_dataset("tips")
print(tips_df) # call tips DataFrame

Output >>>

tips pandas DataFrame - seaborn Tutorial

The shape of tips DataFrame:

print(tips_df.shape) # get shape of dataseat tips

Output >>> (244, 7)

Draw line plot of total_bill and tip

# Draw line plot of total_bill and tip
sns.lineplot(x = "total_bill", y = "tip", data = tips_df)

Output >>>

sns line plot of total_bill and tip

Draw line plot of tip and total_bill

# Draw line plot of tip and total_bill
sns.lineplot(x = "tip", y = "total_bill", data = tips_df)

Output >>>

sns line plot of tip and total_bill

Draw line plot of tip and size

# Draw line plot of tip and size
sns.lineplot(x = "tip", y = "size", data = tips_df)

Output >>>

sns line plot of tip and size

Draw line plot of size and total_bill

# Draw line plot of size and total_bill
sns.lineplot(x = "size", y = "total_bill", data = tips_df)

Output >>>

sns line plot of size and total_bill

Seaborn Line Plot with Multiple Parameters

Till now, drawn multiple line plot using x, y and data parameters. Now, we are using multiple parameres and see the amazing output.

hue => Get separate line plots for the third categorical variable. In the above graph draw relationship between size (x-axis) and total-bill (y-axis). Now, plotting separate line plots for Female and Male category of variable sex.

style => Give style to line plot, like dashes. Different for each line plot.

palette => Give colormap for graph. You can choose anyone from bellow which is separated by a comma.

Colormap
Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, 
RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, 
binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, 
gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, 
nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, 
tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r

dashes => If line plot with dashes then use “False” value for no dashes otherwise “True“.

markers => Give the markers for point like (x1,y1). for markers follow matplotlib line plot blog.

legend => Give legend. The default value is “brief” but you can give “full” or “False“. False for no legend.

# Draw line plot of size and total_bill with parameters
sns.lineplot(x = "size", y = "total_bill", data = tips_df, hue = "sex",
            style = "sex", palette = "hot", dashes = False, 
            markers = ["o", "<"],  legend="brief",)

plt.title("Line Plot", fontsize = 20) # for title
plt.xlabel("Size", fontsize = 15) # label for x-axis
plt.ylabel("Total Bill", fontsize = 15) # label for y-axis
plt.show()

Note:

  • We use only important parameters but you can use multiple depends on requirements.
  • Seaborn line plot function support xlabel and ylabel but here we used separate functions to change its font size

Output >>>

sns.lineplot() with multiple parameters

Seaborn set style and figure size

Above, the line plot shows small and its background white but you cand change it using plt.figure() and sns.set() function.

plt.figure(figsize = (16,9)) # figure size with ratio 16:9
sns.set(style='darkgrid',) # background darkgrid style of graph 

# Draw line plot of size and total_bill with parameters
sns.lineplot(x = "size", y = "total_bill", data = tips_df, hue = "sex",
            style = "sex", palette = "hot", dashes = False, 
            markers = ["o", "<"],  legend="brief",)

plt.title("Line Plot", fontsize = 20)
plt.xlabel("Size", fontsize = 15)
plt.ylabel("Total Bill", fontsize = 15)
plt.show()

Output >>>

seaborn set style

Seaborn multiple line plots

Using sns.lineplot() hue parameter, we can draw multiple line plot. In the above graphs drawn two line plots in a single graph (Female and Male) same way here use day categorical variable. Which have total 4-day categories?

plt.figure(figsize = (16,9))
sns.set(style='darkgrid',)

# Draw line plot of size and total_bill with parameters and hue "day"
sns.lineplot(x = "size", y = "total_bill", data = tips_df, hue = "day",
            style = "day", palette = "hot", dashes = False, 
            markers = ["o", "<", ">", "^"],  legend="brief",)

plt.title("Line Plot", fontsize = 20)
plt.xlabel("Size", fontsize = 15)
plt.ylabel("Total Bill", fontsize = 15)
plt.show()

Output >>>

seaborn multiple line plot

Conclusion

In the seaborn line plot blog, we learn how to plot one and multiple line plots with a real-time example using sns.lineplot() method. Along with that used different method with different parameter. We Suggest you make your hand dirty with each and every parameter of the above methods. This is the best coding practice. Still, you didn’t complete the matplotlib tutorial jump on it.

Download practical code snippet in Jupyter Notebook file format

Leave a Reply

Top