2017-10-10 128 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

相關問題