Python Matplotlib散点图:根据条件指定色点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40803570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:00:56  来源:igfitidea点击:

Python Matplotlib scatter plot: Specify color points depending on conditions

pythonmatplotlib

提问by Argumanez

I have two numpy arrays, x and y, with 7000 elements each. I want to make a scatter plot of them giving each point a different color depending on these conditions:

我有两个 numpy 数组 x 和 y,每个数组有 7000 个元素。我想制作它们的散点图,根据这些条件为每个点赋予不同的颜色:

-BLACK if x[i]<10.

-RED if x[i]>=10 and y[i]<=-0.5

-BLUE if x[i]>=10 and y[i]>-0.5 

I tried creating a list of the same length as the data with the color I want to assign to each point and then plot the data with a loop, but it takes me a long time to run it. Here's my code:

我尝试创建一个与数据长度相同的列表,并使用我想要分配给每个点的颜色,然后用循环绘制数据,但运行它需要很长时间。这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

#color list with same length as the data
col=[]
for i in range(0,len(x)):
    if x[i]<10:
        col.append('k') 
    elif x[i]>=10 and y[i]<=-0.5:
        col.append('r') 
    else:
        col.append('b') 

#scatter plot
for i in range(len(x)):
    plt.scatter(x[i],y[i],c=col[i],s=5, linewidth=0)

#add horizontal line and invert y-axis
plt.gca().invert_yaxis()
plt.axhline(y=-0.5,linewidth=2,c='k')

Before that, I tried creating the same color list in the same way, but plotting the data without the loop:

在此之前,我尝试以相同的方式创建相同的颜色列表,但在没有循环的情况下绘制数据:

#scatter plot
plt.scatter(x,y,c=col,s=5, linewidth=0)

Even though this plots the data much, much faster than using the for loop, some of the scattered points appear with a wrong color. Why not using a loop to plot the data leads to incorrect color of some points?

尽管这比使用 for 循环绘制数据快得多,但一些分散的点出现了错误的颜色。为什么不使用循环来绘制数据会导致某些点的颜色不正确?

I also tried defining three sets of data, one for each color, and adding them to the plot separately. But this is not what I am looking for.

我还尝试定义三组数据,每种颜色一组,并将它们分别添加到绘图中。但这不是我要找的。

Is there a way to specify in the scatter plots arguments the list of colors I want to use for each point in order not to use the for loop?

有没有办法在散点图参数中指定我想用于每个点的颜色列表,以便不使用 for 循环?

PS: This is the plot I get when I don't use the for loop (wrong one):

PS:这是我不使用 for 循环时得到的图(错误的):

enter image description here

在此处输入图片说明

And this one when I use the for loop (correct):

当我使用 for 循环时(正确):

enter image description here

在此处输入图片说明

回答by DavidG

This can be done using numpy.where. Since I do not your exact x and y values I will have to use some fake data:

这可以使用numpy.where. 由于我没有您确切的 x 和 y 值,因此我将不得不使用一些假数据:

import numpy as np
import matplotlib.pyplot as plt

#generate some fake data
x = np.random.random(10000)*10
y = np.random.random(10000)*10

col = np.where(x<1,'k',np.where(y<5,'b','r'))

plt.scatter(x, y, c=col, s=5, linewidth=0)
plt.show()

This produces the plot below:

这产生了下面的情节:

enter image description here

在此处输入图片说明

The line col = np.where(x<1,'k',np.where(y<5,'b','r'))is the important one. This produces a list, the same size as x and y. It fills this list with 'k','b'or 'r'depending on the condition that is written before it. So if x is less than 1, 'k'will be appended to list, else if y is less than 5 'b'will be appended and if neither of those conditions are met, 'r'will be appended to the list. This way, you do not have to use a loop to plot your graph.

线col = np.where(x<1,'k',np.where(y<5,'b','r'))是最重要的。这将生成一个列表,其大小与 x 和 y 相同。它使用'k','b''r'取决于写入它之前的条件填充此列表。因此,如果 x 小于 1,'k'将被附加到列表中,否则如果 y 小于 5,'b'则将被附加,如果这两个条件都不满足,'r'则将被附加到列表中。这样,您不必使用循环来绘制图形。

For your specific data you will have to change the values in the conditions of np.where.

对于您的特定数据,您必须更改 条件中的值np.where