Interactive Heatmap in Python (using hvPlot and Bokeh)
In this article, you will learn how to create an interactive heatmap from pandas DataFrame using the hvPlot Python package.
You need to install the holoviews
, hvPlot
, and bioinfokit
Python package for creating heatmaps. Check
how to install Python packages
This article describes how you can create basic interactive heatmaps. If you are looking for advanced heatmaps with annotation and clustering features, read my article on creating heatmaps with pheatmap
Get dataset for heatmap
Load gene expression dataset for plotting heatmap,
Note: If you have your own dataset, you should import it as a pandas DataFrame. Learn how to import data using pandas
from bioinfokit import analys
import holoviews as hv
import hvplot.pandas # use hvplot directly with pandas
hv.extension('bokeh') # to generate interactive plots
# load example gene expression dataset as pandas dataframe
df = analys.get_data('hmap').data
df.head(2)
Gene A B C D E F
0 B-CHI 4.505700 3.260360 -1.249400 8.89807 8.05955 -0.842803
1 CTL2 3.508560 1.660790 -1.856680 -2.57336 -1.37370 1.196000
# set gene names as index
df = df.set_index(df.columns[0])
df.head(2)
A B C D E F
Gene
B-CHI 4.505700 3.260360 -1.249400 8.89807 8.05955 -0.842803
CTL2 3.508560 1.660790 -1.856680 -2.57336 -1.37370 1.196000
Generate interactive heatmap
Create a basic interactive heatmap using hvplot.heatmap()
function,
hm=df.hvplot.heatmap(cmap='seismic', width=300, height=500)
hv.save(hm, 'heatmap.html')
Get more in-built colormaps here
Change the position of X axis ticks label,
hm=df.hvplot.heatmap(cmap='RdYlGn', xaxis='top', width=300, height=400)
hv.save(hm, 'heatmap.html')
Add title to the heatmap,
hm=df.hvplot.heatmap(cmap='BrBG', xaxis='top', title='Gene expression heatmap',
width=300, height=400)
hv.save(hm, 'heatmap.html')
Remove colorbar from the heatmap,
hm=df.hvplot.heatmap(cmap='PRGn', xaxis='top', title='Gene expression heatmap',
colorbar=False, width=300, height=400)
hv.save(hm, 'heatmap.html')
Enhance your skills with courses on genomics and bioinformatics
- Genomic Data Science Specialization
- Biology Meets Programming: Bioinformatics for Beginners
- Python for Genomic Data Science
- Bioinformatics Specialization
- Command Line Tools for Genomic Data Science
- Introduction to Genomic Technologies
This work is licensed under a Creative Commons Attribution 4.0 International License
Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.