python - 在python 3中使用matplotlib散点函数时如何更改点子集的绘图标记
问题描述
我有许多自定义的 2d 点对象,每个对象都有:
- 坐标 x 和 y
- 标签 1°(-1 和 +inf 之间的整数):这将以标记的颜色表示(cmap 的索引)
- 标签 2°([1 3] 范围内的整数):我希望这个在标记类型中表示
问题是许多点将共享一个标签 1° 值,但标签 2° 一可能不同,反之亦然。
我尝试提取关于标签 2° 值的点并分别绘制它们,这样:
pointsSubset1 = getPointsWithLabel2Value1()
pointsSubset2 = getPointsWithLabel2Value2()
pointsSubset3 = getPointsWithLabel2Value3()
# just assume x y and labels values are obtained correctly
plt.scatter(x1, y1, c=listOfLabels1ForSubset1, cmap="nipy_spectral", marker='s') # plotting pointsSubset1
plt.scatter(x2, y2, c=listOfLabels1ForSubset2, cmap="nipy_spectral", marker='.') # plotting pointsSubset2
plt.scatter(x3, y3, c=listOfLabels1ForSubset3, cmap="nipy_spectral", marker='<') # plotting pointsSubset3
我认为这会奏效,但事实并非如此。标记设置正确,但颜色设置不正确......
忽略 x 和 y 坐标的示例:
子集1 =
- 要点1:
- 标签1:-1
- 标签2:1
- 要点1:
子集2 =
- 要点2:
- 标签1:1
- 标签2:2
- 要点2:
在这种情况下,来自子集 1 的点 1 将与来自子集 2 的点 2 具有不同的标记,但两者将共享相同的颜色(黑色),因为当两者分别绘制时,尽管它们具有不同的 label1 值,但两者都将映射到第一个颜色频谱....
我希望 cmap 中的颜色索引在点子集之间匹配,并且我不认为传递自定义颜色数组是解决方案 bc 标签 1 的可能值在 [-1, +inf] 的范围内(我不'不知道如何管理 cmap 规范化)。
提前致谢。
解决方案
我想会到达你想要的地方
Npoints = 50
x,y = np.random.random(size=(2,Npoints))
label1 = np.random.choice([-1,1,2,3], size=(Npoints,))
label2 = np.random.choice([1,2,3],size=(Npoints,))
label1_min = min(label1)
label1_max = max(label1)
marker_dict = {1:'s',2:'o',3:'<'}
fig, ax = plt.subplots()
for i,m in marker_dict.items():
ax.scatter(x[label2==i], y[label2==i], marker=m, c=label1[label2==i], cmap='nipy_spectral', vmin=label1_min, vmax=label1_max)
推荐阅读
- hbase - 如何在hbase中放入多行?
- c# - 如何在 Xamarin.Forms 上使用 System.Data.SQLite Interop.dll?
- angular - 如何在Angular中重新加载可观察的http?
- java - 如何从 GCS 收听文件?是否可以在多节点应用程序中利用 GcsInboundFileSynchronizer 和 GcsStreamingMessageSource?
- android - 无法接收活动转换识别回调
- exchange-server - 从 Exchange 解析 X509Certificate
- ios - 属性包装器:“不能在属性初始化程序中使用实例成员;属性初始化程序在‘self’可用之前运行”
- javascript - Cognito自己的域登录页面为空白
- python - 无法在 linux Ubuntu 16.04 的 QThread 类中运行 pyshark.FileCapture(pcap)
- mysql - 两列(不同名称,不同表)为一行