Today we’ll learn to draw a bit more sophisticated lineplots that display multiple lines. We’ll provide examples leveraging the two popular Python Data Visualization libraries: Seaborn and Matplotlib.
Plot multiple lines with Matplotlib and Seaborn
To create a line plot showing multiple lines with Matplotlib or Seaborn proceed as following:
- Gather the data to plot into lists, Numpy arrays, a dictionary or a pandas DataFrame.
- Import matplotlib or Seaborn.
- Create a matplotlib figure and an axes sub-plot.
- Use the plot() function to render several lines, as shown below:
x = ['North', 'South', ' East', 'West', 'North-East']
y = [10,20,50,100,110]
z= [111,80,35,40,50]
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x,y)
ax.plot(x,z)
Importing Data Visualization libraries
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.style.use('ggplot')
Create example data
We’ll use the Numpy library to quickly generate simple x,y coordinate data.
x = np.linspace (1,10,25)
y = x * 2
z = np.exp(x)
Seaborn multiple lines chart
We will start by using Seaborn and specifically the lineplot chart. First, we will go ahead and create a DataFrame that we later feed into a couple of lineplot calls, each drawing one plot.
Note: We can obviously construct our DataFrame by reading excel, text, json or csv files as well as connecting to databases or data APIs.
# seaborn plot multiple lines
import pandas as pd
import seaborn as sns
my_dict = dict(x=x,y=y,z=z)
data = pd.DataFrame (my_dict)
fig, ax = plt.subplots()
ax= sns.lineplot(x='x', y='y', data=data)
ax1 = sns.lineplot(x='x', y='z', data=data)
Multiple line charts with Pandas
You can use the plot() DataFrame method, that is integral to the Pandas library to draw a simple multi-line chart off data in multiple DataFrame columns.
You can reuse the data DataFrame that you have created in the previous section of this tutorial.
line_plot = data.plot(kind='line');
line_plot.legend(loc='upper left');
Matplotlib multiple line graph
You might as well use the Matplotlib to generate a simple multi line graph.
# plotting multiple lines from array
plt.plot(x,y)
plt.plot(x,z);
Adding a legend to the chart
We can easily add a legend to the chart using the plt.legend() method as shown below. In this example we also customize the marker type and line color. We also use the label parameter to define the appropriate label legend.
# multiple lines with legend
plt.plot(x,y,marker='.', color='r', label= 'accelerated growth')
plt.plot(x,z, marker = '+', color = 'g',label = 'exponential growth')
plt.legend();
Multiple line plots in one figure
When dealing with more complex multi variable data, we use subplot grids to render multiple graphs. Here’s a a simple example.
# multiple graphs one figure
fig, ax = plt.subplots(2,1, sharex=True)
ax[0].plot(x,y)
ax[1].plot(x,z);
Add an horizontal line to a line plot
One follow up question we got is on how to add a reference horizontal line to a line plot. For theis example we’ll use the Pandas library:
import pandas as pd
x = np.linspace(0,50)
'Horizontal line for y=10
h=10
'define a DataFrame to plot
my_dict = dict(x=x, h=h)
data = pd.DataFrame (my_dict)
line_plot = data.plot(kind='line', title = 'Chart with horizontal line');
line_plot.legend(loc='upper left');
And here’s our chart:
Follow-up learning
How to add labels to data points in Seaborn and Matplotlib plots?