Show Sidebar Hide Sidebar

Heatmaps in matplotlib

How to make a heatmap in matplotlib with a matrix. Examples of colored and labeled heatmaps with custom colorscales.

import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

fig = plt.figure()
ax = fig.add_subplot(111)

ax.set_title('Simple Heatmap with matplotlib and plotly')

plotly_fig = tls.mpl_to_plotly( fig )

trace = dict(z=[[1,20,30],[20,1,60],[30,60,1]], type="heatmap", zmin=1, zmax=60)

plotly_fig['data'] = [trace]

plotly_fig['layout']['xaxis'].update({'autorange':True})
plotly_fig['layout']['yaxis'].update({'autorange':True})

plot_url = py.plot(plotly_fig, filename='mpl-basic-heatmap')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

fig = plt.figure()
ax = fig.add_subplot(111)

ax.set_title('Heatmap with Custom Colorscale (Viridis) ')

plotly_fig = tls.mpl_to_plotly( fig )

print plotly_fig
trace = dict(z=[[1,20,30],[20,1,60],[30,60,1]], type="heatmap", zmin=1, zmax=60, colorscale='Viridis')
plotly_fig['data'] = [trace]
plotly_fig['layout']['xaxis'].update({'autorange':True})
plotly_fig['layout']['yaxis'].update({'autorange':True})

plot_url = py.plot(plotly_fig, filename='mpl-heatmap-custom-colorscale')
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.