7

I have the following Pandas Dataframe, where column a represents a dummy variable:

Panda Dataframe

What I would like to do is to give my markers a cmap='jet' color following the value of column b, except when the value in column a is equal to 1 - in this case I want it to be the color grey.

Any idea how I can do this?

Charlie Crown
  • 1,071
  • 2
  • 11
  • 28
MattnDo
  • 444
  • 1
  • 5
  • 17

1 Answers1

9

You have to mark your values which are equal to one and plot:

import matplotlib.pyplot as plt
import numpy as np

# test data
t = np.linspace(0, 2 * np.pi, 30)
x = np.sin(t)
x[3] = 1
y = np.cos(t)

# indices for 'bad' values
indices = x == 1
# calc colors from jet cmap
cmap = plt.get_cmap('jet')
colors = cmap((y - y.min()) / y.ptp())

# normal values
plt.scatter(t[~indices], x[~indices], c = colors[~indices], cmap = cmap)
# bad values
plt.scatter(t[indices], x[indices], c = 'grey')
plt.show()

enter image description here

Arrays t, x, y represent pandas series.

Serenity
  • 35,289
  • 20
  • 120
  • 115