基本颜色演示

Matplotlib为您提供了8种指定颜色的方法:

  1. 在[0, 1]中的浮点值的RGB或RGBA元组(例如 (0.1, 0.2, 0.5) 或 (0.1, 0.2, 0.5, 0.3))。RGBA是红色,绿色,蓝色,Alpha的缩写;
  2. 十六进制RGB或RGBA字符串 (例如: '#0F0F0F' 或者 '#0F0F0F0F')
  3. [0, 1]中浮点值的字符串表示,包括灰度级(例如,’0.5’);
  4. 单字母字符串,例如这些其中之一:{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'};
  5. 一个 X11/CSS4 (“html”) 颜色名称, 例如:”blue”;
  6. 来自xkcd的颜色调研的名称,前缀为 ‘xkcd:’ (例如:“xkcd:sky blue”);
  7. 一个 “Cn” 颜色规范,即 ‘C’ 后跟一个数字,这是默认属性循环的索引(matplotlib.rcParams['axes.prop_cycle']); 索引在艺术家对象创建时发生,如果循环不包括颜色,则默认为黑色。
  8. 其中一个 {'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'},它们是’tab10’分类调色板中的Tableau颜色(这是默认的颜色循环);

有关matplotlib中颜色的更多信息,请参阅:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. t = np.linspace(0.0, 2.0, 201)
  4. s = np.sin(2 * np.pi * t)
  5. # 1) RGB tuple:
  6. fig, ax = plt.subplots(facecolor=(.18, .31, .31))
  7. # 2) hex string:
  8. ax.set_facecolor('#eafff5')
  9. # 3) gray level string:
  10. ax.set_title('Voltage vs. time chart', color='0.7')
  11. # 4) single letter color string
  12. ax.set_xlabel('time (s)', color='c')
  13. # 5) a named color:
  14. ax.set_ylabel('voltage (mV)', color='peachpuff')
  15. # 6) a named xkcd color:
  16. ax.plot(t, s, 'xkcd:crimson')
  17. # 7) Cn notation:
  18. ax.plot(t, .7*s, color='C4', linestyle='--')
  19. # 8) tab notation:
  20. ax.tick_params(labelcolor='tab:orange')
  21. plt.show()

基本颜色演示

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.colors
  3. matplotlib.axes.Axes.plot
  4. matplotlib.axes.Axes.set_facecolor
  5. matplotlib.axes.Axes.set_title
  6. matplotlib.axes.Axes.set_xlabel
  7. matplotlib.axes.Axes.set_ylabel
  8. matplotlib.axes.Axes.tick_params

下载这个示例