Show Sidebar Hide Sidebar

WebGL Heatmaps in Python

How to make webGL based heatmaps in Python with Plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'2.0.6'

Imports

In [2]:
import plotly.plotly as py

import PIL.Image
import urllib, cStringIO
import numpy as np

Create a HeatmapGL from an Image

Process the image for generating heatmap:

In [3]:
image_url = 'https://images.plot.ly/plotly-documentation/images/heatmap-galaxy.jpg'
f = cStringIO.StringIO(urllib.urlopen(image_url).read())
img = PIL.Image.open(f)
img
Out[3]:
In [11]:
arr = np.array(img)
z_data = []

for i in range(500):
    k = []
    for j in range(500):
        k.append(sum(arr[i][j]))
    z_data.append(k)

Create the WebGL Heatmap

In [20]:
trace = dict(type='heatmapgl', z=z_data, colorscale='Picnic')
data = [trace]

layout = dict(width=700, height=700)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='basic heatmapgl')
Out[20]:
Still need help?
Contact Us

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