WebGL Text and Annotations in Python/v3

How to add webGL based text labels and annotations to plots in python


Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.

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!!

Heatmap with Annotations

In [1]:
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.figure_factory import create_annotated_heatmap

n=250

y = [[i]*n for i in range(12)]
y = [item for sublist in y for item in sublist]

trace = dict(type='heatmap', z=np.random.randint(1, 10,(12, n)), colorscale = 'Viridis')
data=[trace]

# Here's the key part - Scattergl text! 

data.append({'type': 'scattergl',
                    'mode': 'text',
                    'x': list(range(n))*12,
                    'y': y,
                    'text': np.random.choice(list('ATGC'), 12*250),
                    'textfont': {
                        'size': 20
                    }})

steps = [{'args': ['xaxis', {'range': [-0.5 + e, 30.5 + e]}], 'method': 'relayout'} for e in range(n-30)]

sliders = [dict(
    active = 0,
    steps = steps
)]

layout = dict(sliders=sliders)
layout['xaxis'] = {'range': [-0.5, 30.5]}

fig = dict(data=data, layout=layout)

py.iplot(fig, validate=False)
Out[1]:

Reference

See https://plotly.com/python/reference/#scattergl for more information and chart attribute options!