 # [Matplotlib-users] scatter c=colors seems confusing and inconsistent

For 2d scatter, I can pass 2d arrays as X and Y. But it seems not c=.

Here is a demonstration:

···

import matplotlib.pyplot as plt
import numpy as np
A = np.array

import colorcet as cc
cmap = cc.m_rainbow

x = A([[0,.25], [0.5, 0.75]])
y = x**2
colors = np.empty (x.shape, dtype=object)
for i in range(2):
for j in range(2):
colors[i,j] = cmap(x[i,j])

## plt.scatter (x, y, c=colors.flatten())

Without colors.flatten(), mpl will give an error. It seems to be OK with X and Y being 2D, but not c.

In this example I have given c as a 2d array of objects, each object being an RGBA tuple.

Is there an easier way? Preferably one that doesn’t require me to specifically allocate an empty array of objects

and then populate it with an explicit loop?

Neal,

You can use the built-in color-mapping, like this:

import matplotlib.pyplot as plt
import numpy as np
A = np.array

import colorcet as cc
cmap = cc.m_rainbow

x = A([[0,.25], [0.5, 0.75]])
y = x**2

plt.scatter (x, y, c=x, cmap=cmap)

···

-------

Eric

On 2020/04/03 10:11 AM, Neal Becker wrote:

import matplotlib.pyplot as plt
import numpy as np
A = np.array

import colorcet as cc
cmap = cc.m_rainbow

x = A([[0,.25], [0.5, 0.75]])
y = x**2
colors = np.empty (x.shape, dtype=object)
for i in range(2):
for j in range(2):
colors[i,j] = cmap(x[i,j])

plt.scatter (x, y, c=colors.flatten())

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users