Matplotlib 基础知识——色彩篇
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 color:https://color.adobe.com/zh/create/color-wheel
参考:立方数据学院、《科研论文配图绘制指南》