简体   繁体   English

使用 matplotlib 散点图 function 在 Z23EEEB4347BDD256BFCZB6333EEEB4347BDD256BFCZB6

[英]How to change plot marker of a subset of points when using matplotlib scatter function in python 3

I have many custom 2d points objects, each having:我有许多自定义的 2d 点对象,每个对象都有:

  • coordinates x and y坐标 x 和 y
  • label 1° (integer between -1 and +inf): this will be represented in the markers' color (index of cmap) label 1°(-1 和 +inf 之间的整数):这将以标记的颜色表示(cmap 的索引)
  • label 2° (integer in the range of [1 3]): I want this one to be represented in the marker type label 2°([1 3] 范围内的整数):我希望这个在标记类型中表示

The thing is that many points will share a label 1° value, but may differ in the label 2° one, and vice versa.问题是许多点将共享一个 label 1° 值,但在 label 2° 1 中可能会有所不同,反之亦然。

I tried extracting the points regarding the value of label 2° and plotting them separately, this way:我尝试提取有关 label 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

I thought this would work, but it doesn't.我认为这会奏效,但事实并非如此。 The markers are set correctly but not the colors...标记设置正确,但 colors 不正确...

Example ignoring x and y coordinates:忽略 x 和 y 坐标的示例:

  • subset1 =子集1 =

    • point1:要点1:
      • label1: -1标签1:-1
      • label2: 1标签2:1
  • subset2 =子集2 =

    • point2:要点2:
      • label1: 1标签1:1
      • label2: 2标签2:2

In this case, point1 from subset1 will have a different marker than point2 from subset2, but both will share the same color (black) because when both are plotted separately, although they have different label1 values, both will be mapped to the first color in the spectrum....在这种情况下,来自子集 1 的点 1 将与来自子集 2 的点 2 具有不同的标记,但两者将共享相同的颜色(黑色),因为当两者分别绘制时,尽管它们具有不同的 label1 值,但两者都将映射到第一个颜色光谱……

I want colors indexes in cmap to match between subsets of points, and I don't think passing a custom array of colors is the solution bc the label 1 possible values are in the range of [-1, +inf] (and I don't know how to manage cmap normalization).我希望 cmap 中的 colors 索引在点的子集之间匹配,并且我不认为传递 colors 的自定义数组是解决方案 bc ZD304BA20E96D87411588EEABAC850E31Z,+inf 1 可能的值范围'不知道如何管理 cmap 规范化)。

Thanks in advance.提前致谢。

I think would get to where you want我想会到达你想要的地方

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)

在此处输入图像描述

The easy way:简单的方法:

I will share what I found just in case anyone is struggling with the same issue.我将分享我的发现,以防万一有人遇到同样的问题。 It turns out that you can call plt.scatter() once and provide an array of custom sizes for the markers.事实证明,您可以调用 plt.scatter() 一次并为标记提供一组自定义大小。 This way, you can 'play' changing the marker size according to a given criteria (label 2 value in my case), being able to see the difference between subsets of points when plotting.这样,您可以根据给定的标准(在我的情况下为标签 2 值)“播放”更改标记大小,从而能够在绘图时看到点子集之间的差异。

It would be something like this:它会是这样的:

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)

Setting the marker size to a really small number, it's almost like having points, so you can use squares and 'points' while you only specify marker='s':)将标记大小设置为非常小的数字,几乎就像有点一样,因此您可以使用正方形和“点”,而只指定标记='s':)

Remember when building the different lists that indexes that match represent the same point (in x, y, clusters, and s)请记住,在构建不同的列表时,匹配的索引代表相同的点(在 x、y、簇和 s 中)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM