Show Sidebar Hide Sidebar

Colors and Colormaps in matplotlib

Colors and Colorscale options in matplotlib. Examples of different colors and colormaps available in matplotlib.

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]:
'3.1.1'

Named Colors

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

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

color_names = ["r", "g", "b", "peachpuff", "fuchsia"] # Some of the colors

ax.set_title('Named Colors in Matplotlib')
for i in range(1,6):
   x = np.linspace(0,10,1000)
   y = np.sin(x*(np.pi/i))
   line, = ax.plot(x, y, lw=2, c=color_names[i-1],label='color:'+ color_names[i-1])

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plotly_fig.layout.showlegend = True
plotly_fig.layout.width = 550
plotly_fig.layout.height = 400
py.iplot(plotly_fig)
Out[2]:

Matplotlib Colormap

In [10]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

mpl_fig = plt.figure()
num = 1000
s = 121
x1 = np.linspace(-0.5,1,num) + (0.5 - np.random.rand(num))
y1 = np.linspace(-5,5,num) + (0.5 - np.random.rand(num))
x2 = np.linspace(-0.5,1,num) + (0.5 - np.random.rand(num))
y2 = np.linspace(5,-5,num) + (0.5 - np.random.rand(num))
x3 = np.linspace(-0.5,1,num) + (0.5 - np.random.rand(num))
y3 = (0.5 - np.random.rand(num))

ax1 = mpl_fig.add_subplot(221)
cb1 = ax1.scatter(x1, y1, c=x1, cmap=plt.cm.get_cmap('Blues'))
plt.colorbar(cb1, ax=ax1)
ax1.set_title('Blues')

ax2 = mpl_fig.add_subplot(222)
cb2 = ax2.scatter(x2, y2, c=x2, cmap=plt.cm.get_cmap('RdBu'))
plt.colorbar(cb2, ax=ax2)
ax2.set_title('RdBu')

ax3 = mpl_fig.add_subplot(223)
cb3 = ax3.scatter(x3, y3, c=x3, cmap=plt.cm.get_cmap('Dark2'))
plt.colorbar(cb3, ax=ax3)
ax3.set_xlabel('Dark2')

mpl_fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly(mpl_fig)
py.iplot(plotly_fig)
Out[10]:

Matplotlib Colormap Reversed

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

mpl_fig = plt.figure()
num = 1000
s = 121
x1 = np.linspace(-0.5,1,num) + (0.5 - np.random.rand(num))
y1 = np.linspace(-5,5,num) + (0.5 - np.random.rand(num))
x2 = np.linspace(-0.5,1,num) + (0.5 - np.random.rand(num))
y2 = np.linspace(5,-5,num) + (0.5 - np.random.rand(num))
x3 = np.linspace(-0.5,1,num) + (0.5 - np.random.rand(num))
y3 = (0.5 - np.random.rand(num))

ax1 = mpl_fig.add_subplot(221)
cb1 = ax1.scatter(x1, y1, c=x1, cmap=plt.cm.get_cmap('Blues_r'))
#plt.colorbar(cb1, ax=ax1)
ax1.set_title('Reversed Blues')

ax2 = mpl_fig.add_subplot(222)
cb2 = ax2.scatter(x2, y2, c=x2, cmap=plt.cm.get_cmap('RdBu_r'))
#plt.colorbar(cb2, ax=ax2)
ax2.set_title('Reversed RdBu')

ax3 = mpl_fig.add_subplot(223)
cb3 = ax3.scatter(x3, y3, c=x3, cmap=plt.cm.get_cmap('Dark2_r'))
#plt.colorbar(cb3, ax=ax3)
ax3.set_xlabel('Reversed Dark2')


mpl_fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly(mpl_fig)
py.iplot(plotly_fig)
Out[4]:

Setting Colormap Range

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

mpl_fig = plt.figure()
ax1 = mpl_fig.add_subplot(121)
x = np.linspace(1,10,100)
y = np.random.randint(1,10,100)
ax1.scatter(x,y, c=x, s=100, cmap=plt.cm.get_cmap('RdBu'))
ax1.set_title('Colormap range varying in X Direction')

ax2 = mpl_fig.add_subplot(122)
ax2.scatter(x,y, c=y, s=100, cmap=plt.cm.get_cmap('RdBu'))
ax2.set_title('Colormap range varying in Y Direction')

plotly_fig = tls.mpl_to_plotly(mpl_fig)
plotly_fig.layout.width = 500
plotly_fig.layout.height = 300
py.iplot(plotly_fig)
Out[5]:

Colorbar Custom Range

In [6]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z = np.sin(8*X) + np.cos(8*Y)

mpl_fig = plt.figure()
plt.title('Setting Colorbar range Manually')

plotly_fig = tls.mpl_to_plotly(mpl_fig)

plotly_fig.add_trace(dict(type='contour', 
                           x=x, 
                           y=y, 
                           z=Z, 
                           colorbar=dict(nticks=10, 
                                         tickmode='array',
                                         tickvals=[-2,-1,0,1,2]),
                           colorscale='Viridis'
                            )
                          )

plotly_fig.layout.width = 500
plotly_fig.layout.height = 400
py.iplot(plotly_fig)
Out[6]:

Colorbar Custom Size And Ticks

In [7]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z = np.sin(8*X) + np.cos(8*Y)

mpl_fig = plt.figure()
plt.title('Simple Example with Custom Colorbar')

plotly_fig = tls.mpl_to_plotly(mpl_fig)

custom_colorbar = dict(nticks=10, 
                       tickangle=20, 
                       #titlefont=dict(family=Arial, type=sans-serif),
                       title="Custom Colorbar Title",
                       thickness=50,
                       len=1,
                       outlinewidth=2.2)

plotly_fig.add_traces([dict(type='contour', x=x, y=y, z=Z, colorbar=custom_colorbar)])
plotly_fig.layout.width = 500
plotly_fig.layout.height = 300
py.iplot(plotly_fig)
Out[7]:

Matplotlib Colormap With Legend

In [8]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

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

for i in range(10):
    x = np.random.normal(loc=i, size=100)
    y = np.random.normal(loc=i, size=100)
    ax.scatter(x,y,c=y, cmap=plt.cm.get_cmap('RdBu'), label='Trace {}'.format(i))

plotly_fig = tls.mpl_to_plotly(mpl_fig)
plotly_fig.layout.showlegend = True
plotly_fig.layout.width = 500
plotly_fig.layout.height = 400
py.iplot(plotly_fig)
Out[8]:

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!

Still need help?
Contact Us

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