在 python 3 中使用 matplotlib 散点函数时如何更改点子集的绘图标记
How to change plot marker of a subset of points when using matplotlib scatter function in python 3
我有很多自定义的二维点对象,每个都有:
- 坐标 x 和 y
- label 1°(-1 和 +inf 之间的整数):这将以标记的颜色(cmap 的索引)表示
- label 2°([1 3]范围内的整数):我希望这个在marker类型中表示
问题是许多点将共享一个标签 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
子集 2 =
- 要点2:
- 标签 1: 1
- 标签 2: 2
在这种情况下,子集 1 中的点 1 将与子集 2 中的点 2 具有不同的标记,但两者将共享相同的颜色(黑色),因为当两者分别绘制时,尽管它们具有不同的标签 1 值,但两者都将被映射到光谱中的第一种颜色....
我希望 cmap 中的颜色索引在点子集之间匹配,我不认为传递自定义颜色数组是解决方案,因为标签 1 可能的值在 [-1, +inf 范围内](而且我不知道如何管理 cmap 规范化)。
提前致谢。
简单的方法:
我将分享我的发现,以防有人遇到同样的问题。事实证明,您可以调用一次 plt.scatter() 并为标记提供一组自定义大小。这样,您可以 'play' 根据给定标准(在我的例子中为标签 2 值)更改标记大小,在绘图时能够看到点子集之间的差异。
会是这样的:
s = getMarkerCustomSizeForEachPoint()
# x is a list of every x coordinate
# y is a list of every y coordinate
# clusters is a list of every point label (label 1 value in my case)
# marker='s' -> squares
plt.scatter(x, y, c=clusters, cmap="nipy_spectral", marker='s', alpha=0.8, s=s)
将标记大小设置为非常小的数字,几乎就像有点一样,因此您可以使用正方形和 'points' 而您只指定标记='s' :)
请记住,在构建不同的列表时,匹配的索引代表相同的点(在 x、y、簇和 s 中)
我想会到达你想要的地方
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)
我有很多自定义的二维点对象,每个都有:
- 坐标 x 和 y
- label 1°(-1 和 +inf 之间的整数):这将以标记的颜色(cmap 的索引)表示
- label 2°([1 3]范围内的整数):我希望这个在marker类型中表示
问题是许多点将共享一个标签 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 具有不同的标记,但两者将共享相同的颜色(黑色),因为当两者分别绘制时,尽管它们具有不同的标签 1 值,但两者都将被映射到光谱中的第一种颜色....
我希望 cmap 中的颜色索引在点子集之间匹配,我不认为传递自定义颜色数组是解决方案,因为标签 1 可能的值在 [-1, +inf 范围内](而且我不知道如何管理 cmap 规范化)。
提前致谢。
简单的方法:
我将分享我的发现,以防有人遇到同样的问题。事实证明,您可以调用一次 plt.scatter() 并为标记提供一组自定义大小。这样,您可以 'play' 根据给定标准(在我的例子中为标签 2 值)更改标记大小,在绘图时能够看到点子集之间的差异。
会是这样的:
s = getMarkerCustomSizeForEachPoint()
# x is a list of every x coordinate
# y is a list of every y coordinate
# clusters is a list of every point label (label 1 value in my case)
# marker='s' -> squares
plt.scatter(x, y, c=clusters, cmap="nipy_spectral", marker='s', alpha=0.8, s=s)
将标记大小设置为非常小的数字,几乎就像有点一样,因此您可以使用正方形和 'points' 而您只指定标记='s' :)
请记住,在构建不同的列表时,匹配的索引代表相同的点(在 x、y、簇和 s 中)
我想会到达你想要的地方
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)