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.


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')


I also like to use bokeh. Bokeh is a lower-level plotting API, which gives me a lot of control over things I want to do. I can easily adjust things and add features with Bokeh. There are multiple ways to do multiple line plotting in bokeh. First, we can loop through the column data source for each group and create each line for each group. Second, we can use the multi_line function in bokeh. Third, we can loop through CDSView.

  • Looping through column data source

We define column data source for the data we use for plotting. Column data source is especially useful if we want to create multiple plots or tables with the same data. In our example below, through a simple loop, I created three separate data sources and then three separate lines for each group. Then I added the tooltip with the HoverTool function.

from import show
from bokeh.models import ColumnDataSource
from bokeh.models import HoverTool
from bokeh.palettes import Category10
from bokeh.plotting…