Matplotlib Colorscales in Python/v3

How to make Matplotlib Colorscales in Python with Plotly.

Note: this page is part of the documentation for version 3 of, 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!

Formatting the Colormap

Parula Colormap can be downloaded from here

In [1]:
import parula as par
import matplotlib
from matplotlib import cm
import numpy as np

magma_cmap ='magma')
viridis_cmap ='viridis')
parula_cmap = par.parula_map

viridis_rgb = []
magma_rgb = []
parula_rgb = []
norm = matplotlib.colors.Normalize(vmin=0, vmax=255)

for i in range(0, 255):
       k = matplotlib.colors.colorConverter.to_rgb(magma_cmap(norm(i)))

for i in range(0, 255):
       k = matplotlib.colors.colorConverter.to_rgb(viridis_cmap(norm(i)))

for i in range(0, 255):
       k = matplotlib.colors.colorConverter.to_rgb(parula_cmap(norm(i)))

def matplotlib_to_plotly(cmap, pl_entries):
    h = 1.0/(pl_entries-1)
    pl_colorscale = []

    for k in range(pl_entries):
        C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
        pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])

    return pl_colorscale

magma = matplotlib_to_plotly(magma_cmap, 255)
viridis = matplotlib_to_plotly(viridis_cmap, 255)
parula = matplotlib_to_plotly(parula_cmap, 255)

Colorscales for Heatmaps

In [2]:
import plotly.plotly as py
import numpy as np
import os
import plotly.graph_objs as go
from plotly import tools

def heatmap_plot(colorscale, title):
    example_dir = os.path.join(os.path.dirname('__file__'), "examples")

    hist2d = np.loadtxt(os.path.join(example_dir, "hist2d.txt"))
    trace1 = go.Heatmap(z=hist2d, colorscale=colorscale, showscale=False)

    st_helens = np.loadtxt(os.path.join(example_dir,
    trace2 = go.Heatmap(z=st_helens, colorscale=colorscale, y0=-5, x0=-5)

    dx = dy = 0.05
    y, x = np.mgrid[-5 : 5 + dy : dy, -5 : 10 + dx : dx]
    z = np.sin(x)**10 + np.cos(10 + y*x) + np.cos(x) + 0.2*y + 0.1*x
    trace3 = go.Heatmap(z=z, colorscale=colorscale, showscale=False)

    fig = tools.make_subplots(rows=1, cols=3, print_grid=False)
    fig.append_trace(trace1, 1, 1)
    fig.append_trace(trace2, 1, 2)
    fig.append_trace(trace3, 1, 3)
    fig['layout']['xaxis2'].update(range=[0, 450])
    fig['layout']['yaxis2'].update(range=[0, 270])

    return fig
In [3]:
py.iplot(heatmap_plot(colorscale=magma, title='MAGMA'))
In [4]:
py.iplot(heatmap_plot(colorscale=viridis, title='VIRIDIS'))
In [5]:
py.iplot(heatmap_plot(colorscale=parula, title='PARULA'))

Colorscales for Trisurf Plots

In [6]:
import plotly.plotly as py
from import FigureFactory as FF
import plotly.graph_objs as go
import numpy as np
from scipy.spatial import Delaunay

u = np.linspace(0, 2*np.pi, 24)
v = np.linspace(-1, 1, 8)
u,v = np.meshgrid(u, v)
u = u.flatten()
v = v.flatten()

tp = 1 + 0.5*v*np.cos(u/2.)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2.)

points2D = np.vstack([u, v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

trace1 = FF.create_trisurf(x=x, y=y, z=z,
                           simplices=simplices, colormap=magma_rgb, plot_edges=False,
                           title='Magma Colorscale for Trisurf Plot')
C:\Python27\lib\site-packages\plotly\ UserWarning: is deprecated. Use plotly.figure_factory.create_trisurf

In [7]:
trace2 = FF.create_trisurf(x=x, y=y, z=z,
                           simplices=simplices, colormap=viridis_rgb, plot_edges=False,
                           title='Viridis Colorscale for Trisurf Plot')
In [8]:
trace3 = FF.create_trisurf(x=x, y=y, z=z,
                          simplices=simplices, colormap=parula_rgb, plot_edges=False,
                          title='Parula Colorscale for Trisurf Plot')


Special thanks to Stéfan van der Walt and Nathaniel Smith for the statistics of colormaps.