Matplotlib 基础知识——色彩篇

本文详细介绍了Matplotlib中的颜色与透明度设置,包括颜色参数、线型样式、线宽及线条标记,以及如何添加颜色条(colorbar)和选择配色方案。还推荐了几个常用的配色网站。

1.颜色与透明度

1.颜色的参数使用color或者c
2.透明度的参数使用alpha
3.在matplotlib中,我们可以使用四种定义颜色的方式,包括:
(1) 标准名,如red代表红色,yellow代表黄色;
(2) 别名,如r代表红色 (red) ,k代表黑色 (black) b代表蓝色 (blue) ;
(3) HTML十六进制字符串,如#000000代表黑色;
(4) 归一化到[0,1]的RGB元组。

x = np.array([1,2,3,4,5])
y2 = x * 2
y3 = x * 3
y4 = x * 4
y5 = x * 5

plt.plot(x,y2,color = 'r',alpha = 0.8)
plt.plot(x,y3,c = 'pink')
plt.plot(x,y4,color = '#00bc12')
plt.plot(x,y5,color = (0,0,0)) # RGB十六进制  255,255,255

2.线型:样式、线宽、线条标记

1.样式的参数名是linestyle或ls
在这里插入图片描述

x = np.array([1,2,3,4,5])

y2 = x * 2
y3 = x * 3
y4 = x * 4
y5 = x * 5

plt.plot(x,y2,linestyle = "-.")
plt.plot(x,y3,linestyle = "dashed")
plt.plot(x,y4,ls = "solid")
plt.plot(x,y5,ls= ":")

2.线宽的参数名是linewidth或lw;

x = np.array([1,2,3,4,5])

y2 = x * 2
y3 = x * 3
y4 = x * 4
y5 = x * 5

plt.plot(x,y2,ls='--',linewidth=1)
plt.plot(x,y3,ls='-',lw=3)
plt.plot(x,y4,ls=':',lw=5)
plt.plot(x,y5,ls='--',lw=7)

3.标记参数名是marker。外,我们也可以设置标记的边线颜色 (markeredgecolor or mec) 与填充色 (markerfacecolor or mfc) 等。

plt.plot(x,y2,ls='-',marker='*',markersize = 13)
plt.plot(x,y3,ls='-',marker='o',markeredgecolor='b',markerfacecolor='red')
plt.plot(x,y4,ls='-',marker='1')
plt.plot(x,y5,ls='-',marker='+',markersize = 12)

在这里插入图片描述

Marker reference: https://matplotlib.org/stable/gallery/lines_bars_and_markers/marker_reference.html
在这里插入图片描述
在这里插入图片描述

3.颜色条(colorbar)

1.在matplotlib中,颜色条 (colorbar) 是一个独立的坐标轴,可以指明图形中颜色的含义
2.可以在绘图函数(或方法)中,通过cmap参数为图形设置colorbar的配色方案(Colormap reference 至支持的颜色)。
3.创建colorbar可以通过(colorbar)函数或者Figure对象的colorbar()方法,参数基本-致: mappable是需要映射的色彩带,这个参数在colorbar方法中是必须的,但在colorbar小函数中是可选的,这是因为类MATLAB接口已经指定了Axes对象。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

常见的配色网站

1.Color Scheme Designer:https://www.chinavid.com/color.html
在这里插入图片描述

2.Colorbrewer:https://colorbrewer2.org/#type=qualitative&scheme=Set2&n=3
在这里插入图片描述

3.Adobe colorhttps://color.adobe.com/zh/create/color-wheel
在这里插入图片描述

参考:立方数据学院、《科研论文配图绘制指南》

### Matplotlib 配色方案设置 在 Matplotlib 中,可以通过多种方式来定义和应用配色方案。最常用的方法之一是通过 `colormap` 来实现色彩映射。 #### 使用预设的 colormap 设置颜色 Matplotlib 提供了一系列内置的 colormaps,可以方便地应用于图像显示和其他可视化操作中[^1]: ```python import numpy as np import matplotlib.pyplot as plt data = np.random.rand(10,10) plt.imshow(data, cmap='viridis') plt.colorbar() plt.show() ``` 这段代码展示了如何利用名为 'viridis' 的预置 colormap 对随机数据进行着色处理并展示出来。 #### 自定义离散型 color map 除了使用现成的 colormap 外,还可以创建自定义的颜色列表作为 colormap: ```python from matplotlib.colors import ListedColormap custom_colors = ['#FFAAAA', '#AAFFAA','#AAF0DD'] cmap_custom = ListedColormap(custom_colors) plt.scatter(np.random.rand(50), np.random.rand(50), c=np.random.randint(3,size=50), cmap=cmap_custom) plt.colorbar(ticks=[0,1,2], label='Custom Colors') plt.show() ``` 此示例说明了怎样构建一个由特定 RGB 值组成的 colormap 并将其用于绘制散点图中的不同类别标记上[^2]。 #### 应用连续渐变效果 对于需要平滑过渡的效果,则可以选择线性分段函数来自动生成具有渐变特性的 colormap: ```python from matplotlib.colors import LinearSegmentedColormap cdict = {'red': ((0.0, 0.25, .4), (0.3, 1.0, 1.0), (0.5, 1.0, 1.0), (1.0, 0.0, 0.0)), 'green': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.5), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0)), 'blue': ((0.0, 0.0, 0.0), (0.5, 0.5, 0.5), (1.0, 0.0, 0.0)) } my_cmap = LinearSegmentedColormap('my_colormap', cdict) plt.contourf([[i*j for j in range(10)]for i in range(10)], levels=range(-1,10), cmap=my_cmap) plt.colorbar(label="My Custom Colormap") plt.show() ``` 上述例子解释了如何基于给定的颜色变化规律设计出独特的 colormap,并应用于等高线填充图表之中.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值