matplotlib 散點圖,為不同區域的點添加不同顏色


import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

x2 = list(map(int, np.random.random_sample(100) * 100))
y2 = list(map(int, np.random.random_sample(100) * 100))

split_x = split_y = 80
colors = []

for x, y in zip(x2, y2):
    if x > split_x:
        if y > split_y:  # 第一象限
            colors.append('red')
        else:  # 第四象限
            colors.append('blue')
    else:
        if y > split_y:  # 第二象限
            colors.append('yellow')
        else:  # 第三象限
            colors.append('green')
            
plt.plot([split_y]*100, 'c--') # 添加橫向分隔線
plt.plot([split_x]*100, list(range(0, 100)), 'c--')  # 添加縱向分隔線
plt.scatter(x2, y2, c=colors)
plt.xlim([0, 100])
plt.ylim([0, 100])
plt.show()

效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM