ap21
ap21

Reputation: 2614

Matplotlib: Use colormap AND use different markers for different values

I currently have a bunch of (x,y) points stored in the array xy, that I am colouring using a third array Kmap, using matplotlib's in built cmap option.

plt.scatter(xy[:, 0], xy[:, 1], s=70, c=Kmap, cmap='bwr')

This is fine. Now, I want to do something extra. While continuing to use the cmap, I want to use different markers according to whether the Kmap values are >0, < 0 or =0. How do I do this?

Note: I can imagine breaking up the points, and scatter-plotting them separately, with different markers, using an if statement. But then, I don't know how to apply a continuous cmap to these values.

Upvotes: 2

Views: 2231

Answers (1)

user2660966
user2660966

Reputation: 975

Separate the dataset looks to be a good option. To keep consistency between colors, you may use the vmin, vmax arguments of the scatter method

import matplotlib.pyplot as plt
import numpy as np


#generate random data
xy = np.random.randn(50, 2)
Kmax = np.random.randn(50)

#data range
vmin, vmax = min(Kmax), max(Kmax)

#split dataset
Ipos = Kmax >= 0. #positive data (boolean array)
Ineg = ~Ipos      #negative data (boolean array)


#plot the two dataset with different markers
plt.scatter(x = xy[Ipos, 0], y = xy[Ipos, 1], c = Kmax[Ipos], vmin = vmin, vmax = vmax, cmap = "bwr", marker = "s")
plt.scatter(x = xy[Ineg, 0], y = xy[Ineg, 1], c = Kmax[Ineg], vmin = vmin, vmax = vmax, cmap = "bwr", marker = "o")

plt.show()

Upvotes: 3

Related Questions