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
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')
If you have any questions, comments or recommendations, please email me at reneshbe@gmail.com
If you enhanced your knowledge and practical skills from this article, consider supporting me on
This work is licensed under a Creative Commons Attribution 4.0 International License