Numpy array Seaborn Single Line Plot

In this article, we are going to learn how to Numpy array Seaborn Single Line Plot by using the Seaborn module and the data in the form of a Numpy array.We will learn this with the help of coding examples and the visual output in the form of 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.You can learn about all its arguments

seaborn.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, seed=None, sort=True, err_style='band',	err_kws=None, legend='auto', ax=None, **kwargs)

1.Numpy array Seaborn Single Line Plot


We are going to see how we can use the lineplot() function to plot single lines where the 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.

This step is extra when we have the data in the form of a ndarray or numpy array. We first convert this ndarray to a Panda Dataframe and then we use the lineplot() to draw the plot.

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.

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltsw


xData = np.array([1, 2, 3, 4, 5])
yData = np.array([1, 4, 9, 16, 25])
array = {'Number': xData, 'Square': yData}

DataFrame_lineplot = pd.DataFrame(array)

sns.lineplot(data=DataFrame_lineplot)
pltsw.show()

Output

Explanation:


  • We are importing the seaborn module to our program.
    • import seaborn as sns
  • We are importing the pandas module to our program.
    • import numpy as np
  • We are importing the pandas 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 Numpy array of number values.
    • xData = np.array([1, 2, 3, 4, 5])
  • We are taking another Numpy array of square of numbers from array1(xData) values.
    • yData = np.array([1, 4, 9, 16, 25])
  • Next we are assigning the arrays to array.
    • array = {‘Number’: xData, ‘Square’: yData}
  • We are next converting this data to a panda dataframe.
    • DataFrame_lineplot = 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_lineplot)
    • 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.
    • pltsw.show()

2. NumPy array 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.The 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 dont pass this argument then it is false by default.But if you pass this as true , then lineplot function handles the markers itself.Also you have option to pass the symbols in markers as argument if you want to handle it yourself.

We have another argument that is “ms” means marker size, you can pass this as a desired size of the markers.
We have “mfc” argument which is marker face color, that is used to set the color. You can use the first letter of a color to pass as argument.
“mec” is the marker edge color argument, you can use the first letter of a color to pass as 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 numpy as np
import pandas as pd
import matplotlib.pyplot as pltsw


xData = np.array([1, 2, 3, 4, 5])
yData = np.array([1, 4, 9, 16, 25])
array = {'Number': xData, 'Square': yData}

DataFrame_lineplot = pd.DataFrame(array)

sns.lineplot(x='Number', y='Square',data=DataFrame_lineplot,markers=['p','*'],palette='dark', legend=False, linewidth=2,ms=15, mec='r',mew=2,mfc='g')
pltsw.show()

Output