Skip to content
Home » matplotlib Part 22 – Colormap Plots

matplotlib Part 22 – Colormap Plots

Spread the love

Colormap Plots

In this article we’ll be talking about colormap plots. These are used to represent three-dimensional data in 2D. The x and y axes are represented by position in a 2D coordinate system whereas the value of z is represented by a color.

There are several methods we can use to create colormaps. One of them is pcolor. It requires the data to be formatted in a two-dimensional array. We can use the numpy meshgrid method to prepare the data.

In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

# Let's create the x and y values.
x = y = np.linspace(0, 10, 100)

# Let's format the data.
X, Y = np.meshgrid(x, y)

# Let's define the values of Z.
Z = np.sin(X) ** 5 + np.cos(Y) * np.cos(X)

# Let's create the color map
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z)
Out[1]:
<matplotlib.collections.PolyCollection at 0x26c5cc59448>

You can add edge colors:

In [2]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, edgecolors='r')
Out[2]:
<matplotlib.collections.PolyCollection at 0x26c60466c48>

You can also use predefined mathplotlib color maps. They are available in mpl.cm. To use one, set the cmap accordingly:

In [3]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.bwr)
Out[3]:
<matplotlib.collections.PolyCollection at 0x26c60ad5208>

Here are some examples of other built-in color maps.

In [4]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.plasma)
Out[4]:
<matplotlib.collections.PolyCollection at 0x26c620b67c8>
In [5]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.inferno)
Out[5]:
<matplotlib.collections.PolyCollection at 0x26c626f3a88>
In [6]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.Oranges)
Out[6]:
<matplotlib.collections.PolyCollection at 0x26c62d1a208>
In [7]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.winter)
Out[7]:
<matplotlib.collections.PolyCollection at 0x26c6334db48>
In [8]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.twilight)
Out[8]:
<matplotlib.collections.PolyCollection at 0x26c63983f48>
In [9]:
fig, ax = plt.subplots()
ax.pcolor(X, Y, Z, cmap=mpl.cm.jet)
Out[9]:
<matplotlib.collections.PolyCollection at 0x26c64f8eb88>

And there are many more. You can use this link:

https://matplotlib.org/stable/gallery/color/colormap_reference.html

to check them out.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply