昆山市住房和建设局网站班级优化大师app下载学生版
详解matplotlib的color配置
Matplotlib可识别的color格式
格式 | 举例 |
---|---|
RGB或RGBA,由[0, 1]之间的浮点数组成的元组,分别代表红色、绿色、蓝色和透明度 | (0.1, 0.2, 0.5), (0.1, 0.2, 0.5, 0.3 |
不区分大小写的十六进制RGB或RGBA字符串。 | ‘#0f0f0f’, ‘#0f0f0f80’ |
不区分大小写的RGB或RGBA字符串等效的十六进制缩写重复字符。 | ‘#abc’ 等于 ‘#aabbcc’ |
对于灰度值,浮点值在闭合区间[0,1]中的字符串表示形式。 | ‘0’ 表示黑色,‘1’表示白色,‘0.8’表示浅灰色 |
一些基本颜色的单字符简写符号。 | ‘b’表示蓝色 |
不区分大小写的X11/CSS4颜色名称,不包含空格。 | ‘aquamarine’ |
使用前缀’xkcd:'进行xkcd颜色调查,颜色名称不区分大小写。 | ‘xkcd:sky blue’ |
来自T10 分类调色板的不区分大小写的Tableau颜色。 | ‘tab:blue’ |
CN"颜色规范,其中C 排在一个数字前面,作为默认属性cycle的索引 | ‘C0’ |
import matplotlib.pyplot as plt
import numpy as npt = 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()
通过辅助函数认识颜色
import mathfrom matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
import matplotlib.colors as mcolorsdef plot_colortable(colors, *, ncols=4, sort_colors=True):cell_width = 212cell_height = 22swatch_width = 48margin = 12# Sort colors by hue, saturation, value and name.if sort_colors is True:names = sorted(colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c))))else:names = list(colors)n = len(names)nrows = math.ceil(n / ncols)width = cell_width * 4 + 2 * marginheight = cell_height * nrows + 2 * margindpi = 72fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)fig.subplots_adjust(margin/width, margin/height,(width-margin)/width, (height-margin)/height)ax.set_xlim(0, cell_width * 4)ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)ax.yaxis.set_visible(False)ax.xaxis.set_visible(False)ax.set_axis_off()for i, name in enumerate(names):row = i % nrowscol = i // nrowsy = row * cell_heightswatch_start_x = cell_width * coltext_pos_x = cell_width * col + swatch_width + 7ax.text(text_pos_x, y, name, fontsize=14,horizontalalignment='left',verticalalignment='center')ax.add_patch(Rectangle(xy=(swatch_start_x, y-9), width=swatch_width,height=18, facecolor=colors[name], edgecolor='0.7'))return fig
- 基本色
plot_colortable(mcolors.BASE_COLORS, ncols=3, sort_colors=False)
- 表盘色
plot_colortable(mcolors.TABLEAU_COLORS, ncols=2, sort_colors=False)
- CSS颜色
plot_colortable(mcolors.CSS4_COLORS)
plt.show()
- XKCD颜色
xkcd_fig = plot_colortable(mcolors.XKCD_COLORS)
xkcd_fig.savefig("XKCD_Colors.png")
matplotlib.colors API 点击进入