In this article, we are going to learn how to Seaborn Single Line Plot Pandas Dataframe by using the Seaborn module. We will learn this with the help of coding examples and the visual output in the form of a plot by using some sample data.
single-line plot using Seaborn’s lineplot() function
Here we are going to see all our examples using Seaborn’s lineplot() function. Let us learn about this function first.
seaborn.lineplot(*, x=None, y=None, hue=None, size=None, style=None, data=None, palette=None, hue_order=None, ue_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, seed=None, sort=True, err_style='band', err_kws=None, legend='auto', ax=None, **kwargs)
You can learn about all its arguments
1.Seaborn Single Line Plot Pandas Dataframe
We are going to see how we can use the lineplot() function to plot single lines where each line represents data. To plot single lines we must have the data of each line. if this data is unformatted data, we must convert it into a panda dataframe where we align each line of data to each column of this data frame. Once we have this formatted data in the form of a panda dataframe, we can plot this panda dataframe on the plot by using the lineplot() function. So let us see this with the help of a code example.
- We are importing the seaborn module to our program.
- import seaborn as sns
- We are importing the panda’s module to our program.
- import pandas as pd
- We are importing the matplotlib module’s pyplot to our program to display the plot.
- import matplotlib.pyplot as pltsw
- We are taking a sample array of random values. array = [1,2,1,4,1]
- We are next converting this linear array to a panda dataframe.
- DataFrame_multiplot = pd.DataFrame(array)
- Finally, we are calling the lineplot() function on this dataframe, by just passing this dataframe to lineplot() function.
- sns.lineplot(data=DataFrame_multiplot) The lineplot() function will act on the values that are in the columns of the data frame as the y-axis values. It will use the index of the values as the x-axis values to plot the lines.
- The last and important step is to call the show function to show the plot on screen.
import seaborn as sns import pandas as pd import matplotlib.pyplot as pltsw array = [1,2,1,4,1] DataFrame_multiplot = pd.DataFrame(array) sns.lineplot(data=DataFrame_multiplot) pltsw.show()
2. Seaborn single line plot with markers
We are going to see how we can use the lineplot() function to plot single lines with markers. Also, we will see how we can set the palette, line width, and other properties. lineplot function takes several other arguments that are used to enhance the visibility of the plot like markers, palette, legend, linewidth, etc.
The markers argument of the lineplot() function is used to plot markers inside the lines. If you don’t pass this argument then it is False by default. But if you pass this as True, then lineplot() function handles the markers themselves.
Also, you have the option to pass the symbols in markers as an argument if you want to handle it yourself. We have another argument that “ms” means marker size, you can pass this as the desired size of the markers. We have the “mfc” argument which is a marker face color, that is used to set the color. You can use the first letter of color to pass as an argument.”mec” is the marker edge color argument, you can use the first letter of color to pass as an argument. linewidth is another argument to set the width of each line that we are plotting on this plot. so let us see this with the help of a code example.
import seaborn as sns import pandas as pd import matplotlib.pyplot as pltsw array = [1,2,1,4,1] DataFrame_multiplot = pd.DataFrame(array) sns.lineplot(data=DataFrame_multiplot,markers=['*'],palette='dark', legend=False, linewidth=2,ms=15, mec='r',mew=2,mfc='g') pltsw.show()