Show Sidebar Hide Sidebar

Matplotlib Colorscales in Python

How to make Matplotlib Colorscales 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!

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 = matplotlib.cm.get_cmap('magma')
viridis_cmap = matplotlib.cm.get_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)))
       magma_rgb.append(k)

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

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

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,
                                        "st-helens_before-modified.txt.gz")).T
    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'].update(title=title)
    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'))
Out[3]:
In [4]:
py.iplot(heatmap_plot(colorscale=viridis, title='VIRIDIS'))
Out[4]:
In [5]:
py.iplot(heatmap_plot(colorscale=parula, title='PARULA'))
Out[5]:

Colorscales for Trisurf Plots

In [6]:
import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import graph_objs
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')
py.iplot(trace1)
Out[6]:
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')
py.iplot(trace2)
Out[7]:
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')
py.iplot(trace3)
Out[8]:

Acknowledgment

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

Still need help?
Contact Us

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