趕項目停了好久啊,這個事情還是要堅持下去的。
matplotlib可以用以下幾種方式指定顏色:
- RGB或RGBA(red, green, blue, alpha,alpha代表透明度)的元組,其中的元素是[0,1]區(qū)間內(nèi)的數(shù)值,例如(0.1, 0.2, 0.5) or (0.1, 0.2, 0.5, 0.3);
- 16進制的RGB或RGBA字符串,例如 '#0f0f0f' or '#0f0f0f80',不區(qū)分大小寫;
- 字符型的[0,1]區(qū)間內(nèi)的數(shù)值,例如,'0.5';
- 顏色的首字母簡寫形式,從{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}中取值;
- X11/CSS4 的顏色名稱,不區(qū)分大小寫;
-
xkcd color survey中的顏色名稱, 需要加上前綴
'xkcd:'
,例如'xkcd:sky blue'
; - 默認的 'T10' 調(diào)色板中的一種顏色:{'tab:blue', 'tab:orange', 'tab:green', 'tab:red','tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'},不區(qū)分大小寫;
- 'CN'方式指定,其中的 'N' 表示數(shù)字,代表默認的屬性循環(huán)的index(matplotlib.rcParams['axes.prop_cycle']);
更多matplotlib顏色的信息:
- the Specifying Colors tutorial;
- the
matplotlib.colors
API; - the List of named colors example.
顏色當中 "alpha" 的表現(xiàn)取決于圖形的zorder
值,同一個坐標系中的圖形是分層渲染的,高 zorder 值的圖形被置于低 zorder 值的圖形之上,"alpha" 值就決定了下層的圖形是否會被上層圖形覆蓋。假設某個像素點原先的 RGB 為 RGBold ,加在其上的Alpha
值為 alpha 的新圖形的 RGB 為 RGBnew ,那么這個像素點當前的 RGB 則更新為:RGB = RGBOld * (1 - Alpha) + RGBnew * Alpha。Alpha 為1表示原來的顏色被完全覆蓋,alpha 為0則表示新圖形的像素是透明的。
下面來看例子:
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0.0, 2.0, 201)
s = np.sin(2 * np.pi * t)
# 1) RGB tuple:
fig, ax = plt.subplots(facecolor=(.18, .31, .31))
# 2) hex string:
ax.set_facecolor('#eafff5')
# 3) gray level string:
ax.set_title('Voltage vs. time chart', color='0.7')
# 4) single letter color string
ax.set_xlabel('time (s)', color='c')
# 5) a named color:
ax.set_ylabel('voltage (mV)', color='peachpuff')
# 6) a named xkcd color:
ax.plot(t, s, 'xkcd:crimson')
# 7) Cn notation:
ax.plot(t, .7*s, color='C4', linestyle='--')
# 8) tab notation:
ax.tick_params(labelcolor='tab:orange')
plt.show()