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