Show Sidebar Hide Sidebar

Colors and Colormaps in matplotlib

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

import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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_url = py.plot(plotly_fig)
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
num=1000
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 = 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 = 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 = 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')
plt.tight_layout(pad=0.8, w_pad=0.8, h_pad=1.0)

fig = plt.gcf()
py.plot_mpl(fig, filename="mpl-colormaps-simple")
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
num=1000
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 = 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 = 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 = 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')
plt.tight_layout(pad=0.8, w_pad=0.8, h_pad=1.0)

fig = plt.gcf()
py.plot_mpl(fig, filename="mpl-colormaps-reversed")
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = 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 = 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(fig)
plotly_url = py.plot(plotly_fig)
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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)

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

plotly_fig = tls.mpl_to_plotly(fig)

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

plotly_url = py.plot(plotl_fig, filename='mpl-colorbar-range')
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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)

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

plotly_fig = tls.mpl_to_plotly(fig)

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

plotly_fig['data'] = [dict(type='contour', x=x, y=y, z=Z, colorbar=custom_colorbar)]

plotly_url = py.plot(plotl_fig, filename='mpl-custom-colorbar')
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = 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(fig)
plotly_fig['layout']['showlegend'] = True
py.iplot(plotly_fig)
Still need help?
Contact Us

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