python - Matplotlib:使用颜色图并对不同值使用不同标记

标签 python matplotlib scatter-plot colormap

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

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

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

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

最佳答案

分离数据集看起来是一个不错的选择。为了保持颜色之间的一致性,您可以使用 scatter 方法的 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()

关于python - Matplotlib:使用颜色图并对不同值使用不同标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44881378/

相关文章:

python - Python 中的密码提交

python - 绘制向量函数的最 Pythonic 方法

python - Pandas - 散点矩阵集标题

python - Tensorflow:类型错误:需要二进制或 unicode 字符串,得到 <tf.Tensor 'Placeholder:0' shape=<unknown> dtype=string>

python - 使用 pysftp 验证主机 key

python - 如何绘制并且只选择几个状态而不是全部 50 个状态

python - 如何在 Matplotlib 的子图中单独绘制相同的图形?

R -- 同一张图上的成对点图和箱线图 : is there a template in ggplot2 or another package?

python - 如何在 Python/Pylab/Seaborn/Plotly 中创建比较散点图/群图?

python - 如何使用两个 Django 身份验证后端并根据使用的后端提供不同的访问权限?