总结:用ListedColormap作plt.scatter()

本文总结了如何使用ListedColormap和plt.scatter()结合,通过自定义颜色库来生成散点图。介绍了 ListedColormap 的用法,包括使用十六进制颜色码定义颜色库,以及plt.scatter()的关键参数如x、y、s、c、cmap等。通过实例展示了如何根据数据值映射到自定义颜色库,生成个性化散点图。
部署运行你感兴趣的模型镜像

看了几篇关于scatter()和ListedColormap的用法文章,这里做下总结,如何用自己想要的colormap做出想要的散点图。

1.ListedColormap允许用户使用十六进制颜色码来定义自己所需的颜色库,并作为plt.scatter()中的cmap参数出现:

例子

# 定义了一个顺序为(255,0,0),(0,255,0),(0,0,255)的颜色库
colormap = ListedColormap(['#FF0000','#00FF00','#0000FF'])

2.plt.scatter()用法:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, *, data=None, **kwargs)

其中

x,y表示所要绘制的散点数据

s代表散点大小

c表示的可以是

  • A single color format string.(比如‘red’)
  • A sequence of color specifications of length n.(比如['blue','yellow','green','red']),sequence中的元素与散点一一对应
  • A sequence of n numbers to be mapped to colors using cmap and norm.其中number可以是任意数字,根据number大小,一一对应cmap中的十六进制颜色编码

cmap表示使用的颜色库,当且仅当c以number且格式匹配数据表示时会被访问到

3.实例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

plt.subplot(311)
colormap = ListedColormap(['#00FF00','#FF0000','#0000FF'])
np.random.seed(1)
colors = np.random.randint(3,6,size=5)
x,y = np.random.rand(5),np.random.rand(5)

plt.scatter(x, y, s=100, c=colors,cmap=colormap,edgecolors='black')

plt.subplot(312)
plt.scatter(x, y, s=100, c='r',edgecolors='black')

plt.subplot(313)
colors = ['r','r','g','g','b']
plt.scatter(x, y, s=100, c=colors,edgecolors='black')

结果:

您可能感兴趣的与本文相关的镜像

Anything-LLM

Anything-LLM

AI应用

AnythingLLM是一个全栈应用程序,可以使用商用或开源的LLM/嵌入器/语义向量数据库模型,帮助用户在本地或云端搭建个性化的聊天机器人系统,且无需复杂设置

from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score from matplotlib.colors import ListedColormap import matplotlib.pyplot as plt import numpy as np #提取特征值,划分数据集; iris = load_iris() x,y = iris.data[:,2:4],iris().target x_train,y_train,x_test,y_test = train_test_split(x,y,random_state= 42,test_size= 50)#划分训练集 #定义与训练逻辑回归模型 model = LogisticRegression(max_iter = 200) model.fit(x_train,y_train) #模型评估 ac = accuracy_score(x_test,y_test) print(f"模型准确率:{ac}") #绘制分类界面 N,M = 500,500 t1 = np.linspace(0,8,N) t2 = np.linspace(0,3,M) x1,x2=np.meshgrid(t1,t2) #生成网格采样点 x_new=np.stack((x1.flat,x2.flat),axis=1) #将采样点为测试点 y_predict=model.predict(x_new) #预测测试点的值 y_hat=y_predict.reshape(x1.shape) #与x1设置相同的形状 iris_cmap=ListedColormap(["#ACC6C0","#FF8080","#A0A0FF"]) #设置分类界面的颜色 plt.pcolormesh(x1,x2,y_hat,cmap=iris_cmap) #绘制分类界面 #绘制3种类别鸢尾花的样本点 plt.scatter(x[y==0,0],x[y==0,1],s=30,c='g',marker='^') #绘制标签为0的样本点 plt.scatter(x[y==1,0],x[y==1,1],s=30,c='r',marker='o') #绘制标签为1的样本点 plt.scatter(x[y==2,0],x[y==2,1],s=30,c='b',marker='s') #绘制标签为2的样本点 #设置坐标轴的名称并显示图形 plt.rcParams['font.sans-serif']='Simhei' plt.xlabel('花瓣长度') plt.ylabel('花瓣宽度') plt.show()Traceback (most recent call last): File "D:/pythonproject/pytest1/项目三.py", line 11, in <module> x,y = iris.data[:,2:4],iris().target TypeError: 'Bunch' object is not callable
10-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值