Matplotlib:使用颜色图并为不同的值使用不同的标记

Matplotlib: Use colormap AND use different markers for different values

我目前有一堆 (x,y) 点存储在数组 xy 中,我正在使用第三个数组 Kmap 着色,使用内置的 matplotlib cmap选项。

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

这很好。现在,我想做一些额外的事情。在继续使用 cmap 的同时,我想根据 Kmap 值是 >0、<0 还是 =0 使用不同的标记。我该怎么做呢?

注意:我可以想象分解这些点,并使用 if 语句分别用不同的标记对它们进行散点图绘制。但是,我不知道如何对这些值应用连续 cmap

分离数据集看起来是个不错的选择。为了保持颜色之间的一致性,您可以使用分散方法的 vmin、vmax 参数

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()