2017-10-10 127 views
-2

enter image description here如何使一个散点图,通过颜色matplotlib.pyplot分离

我想要的情节一样,...

有3个condistions ..

  1. 如果x > 700 - > COL = '蓝色'
  2. 如果x < = 700且y> -2 - > COL = '黑'
  3. 如果x < = 700和y < = -2 - > col ='red'and marker ='*'

我该如何做一个这样的情节?

enter image description here

+2

尝试写一些代码,如果它不工作分享... – Julien

+0

图(X,Y, 山坳= ifelse(Y < Coff & x >也不是, '绿色',ifelse(Y < COFF, '红',ifelse(X>也不是, '蓝', “黑色”))), PCH = ifelse(Y

+0

上面的代码为R代码..我可以,使用R陌散点图.. 但我想用蟒! –

回答

0

我没有你的确切数据,所以我只是要创建自己的数据集。最简单的方法是将三种不同的颜色类型存储在三组独立的数组中。检查下面的代码。

import matplotlib.pyplot as plt 
import random 

x_val = [random.randint(600, 800) for i in range(500)] 
y_val = [random.randint(-4, 0) for i in range(500)] 
blue_plot = [(x, y) for (x, y) in zip(x_val, y_val) if x > 700] 
black_plot = [(x, y) for (x, y) in zip(x_val, y_val) if x <= 700 and y >= -2] 
red_plot = [(x, y) for (x, y) in zip(x_val, y_val) if x <= 700 and y <= -2] 

def separate_x_y(val): 
    x = [x for (x, _) in val] 
    y = [y for (_, y) in val] 
    return x, y 

blue_x, blue_y = separate_x_y(blue_plot) 
black_x, black_y = separate_x_y(black_plot) 
red_x, red_y = separate_x_y(red_plot) 

# Main part of code 
plt.scatter(blue_x, blue_y, color = 'blue') 
plt.scatter(black_x, black_y, color = 'black') 
plt.scatter(red_x, red_y, color = 'red', marker = '*') 
plt.xlim(550, 850) 
plt.ylim(-6, 2) 
plt.show() 

你会得到这样的情节。

Plot

+1

我得到了你的提示!谢谢! –

+0

欢迎您:) – user1190882

相关问题