Python Visualization — Multiple Line Plotting

Sophia Yang, Ph.D.
3 min readDec 2, 2018

Multiple line plotting is easy to do in Python. There are many ways people can do this with various Python visualization tools, e.g., matplotlib, seaborn, bokeh, holoviews, and hvplot. Here I am demonstrating how I plot multiple lines in bokeh and hvplot. For your reference, the package versions I used for this article are: Python 3.8.2, hvplot 0.6.0, and bokeh 2.1.0.

To get started, let’s create a very simple dataset with three variables — x, y, and group.

import numpy as np 
import pandas as pd
group = np.repeat(['Group 1', 'Group 2','Group 3'], 10)
x = list(range(1,11))*3
y = np.random.randint(10, size=30)
df = pd.DataFrame({'group': group,'x': x,'y': y})

Our goal is to create a multiple line plot by group like the figure showing below. And I would like to see the tooltips of the mouse pointer.

hvplot

The easiest approach is hvplot. hvplot is a high-level plotting tool built on Holoviews. It’s super easy to use and the output figure is formatted perfectly. With two lines of code below, we can get the desired results.

import hvplot.pandasp1= df.hvplot('x','y',by='group')
p1

bokeh

--

--