当前位置:网站首页>Visualization Road (11) detailed explanation of Matplotlib color
Visualization Road (11) detailed explanation of Matplotlib color
2022-04-23 10:54:00 【The big pig of the little pig family】
The road to Visualization ( 11、 ... and )matplotlib Color details
One . Preface
matplotlib Library is the most popular data visualization library at present , He supports quite a number of color choices , Next, I will introduce in detail matplotlib Various color input supported by the library .
Two . Supported colors
-
2.1 Closed interval [0, 1] Floating point values in RGB or RGBA( red 、 green 、 blue 、alpha) Tuples .
give an example :(0.1, 0.2, 0.54),(0.1, 0.5, 0.2, 0.6)
-
Case insensitive hexadecimal RGB or RGBA character string .
give an example :’#0f0f0f’ perhaps ’#0f0f0f80’
-
Case insensitive RGB or RGBA A string is a hexadecimal shorthand equivalent to a repeating character .
give an example :’#abc’ As ‘#aabbcc’ and ’#fb1’ As ‘#ffbb11’
-
A string representation of a closed interval floating-point value of a grayscale value .
- 0 : As black
- 1: As white
- 0.8 : As light gray
-
Some basic colors of single character shorthand symbols .
'b'Like blue'g'As green'r'As red'c'As cyan'm'Magenta'y'As yellow'k'As black'w'As white
-
Case insensitive X11/CSS4 Color name ,( No spaces )
give an example :
'aquamarine'and'mediumseagreen' -
come from xkcd Case insensitive color names , with
'xkcd:'Prefix .give an example :
'xkcd:sky blue'and'xkcd:eggshell' -
come from “T10” The classification palette is case insensitive Tableau Color .
'tab:blue''tab:orange''tab:green''tab:red''tab:purple''tab:brown''tab:pink''tab:gray''tab:olive''tab:cyan'
-
“CN” Color specification .
'C0''C1'
matplotlib Formula for calculating transparency :
R G B n e w = R G B b e l o w ∗ ( 1 − α ) + R G B a r t i s t ∗ α RGB_{new} = RGB_{below} * (1 - \alpha) + RGB_{artist} * \alpha RGBnew=RGBbelow∗(1−α)+RGBartist∗α
3、 ... and . example
3.1X11/CSS4 And xkcd Color contrast
Complete code :
import matplotlib.colors as mcolors
import matplotlib.patches as mpatch
import matplotlib.pyplot as plt
overlap = {
name for name in mcolors.CSS4_COLORS
if f'xkcd:{
name}' in mcolors.XKCD_COLORS}
fig = plt.figure(figsize=[9, 5])
ax = fig.add_axes([0, 0, 1, 1])
n_groups = 3
n_rows = len(overlap) // n_groups + 1
for j, color_name in enumerate(sorted(overlap)):
css4 = mcolors.CSS4_COLORS[color_name]
xkcd = mcolors.XKCD_COLORS[f'xkcd:{
color_name}'].upper()
# Pick text colour based on perceived luminance.
rgba = mcolors.to_rgba_array([css4, xkcd])
luma = 0.299 * rgba[:, 0] + 0.587 * rgba[:, 1] + 0.114 * rgba[:, 2]
css4_text_color = 'k' if luma[0] > 0.5 else 'w'
xkcd_text_color = 'k' if luma[1] > 0.5 else 'w'
col_shift = (j // n_rows) * 3
y_pos = j % n_rows
text_args = dict(fontsize=10, weight='bold' if css4 == xkcd else None)
ax.add_patch(mpatch.Rectangle((0 + col_shift, y_pos), 1, 1, color=css4))
ax.add_patch(mpatch.Rectangle((1 + col_shift, y_pos), 1, 1, color=xkcd))
ax.text(0.5 + col_shift, y_pos + .7, css4,
color=css4_text_color, ha='center', **text_args)
ax.text(1.5 + col_shift, y_pos + .7, xkcd,
color=xkcd_text_color, ha='center', **text_args)
ax.text(2 + col_shift, y_pos + .7, f' {
color_name}', **text_args)
for g in range(n_groups):
ax.hlines(range(n_rows), 3*g, 3*g + 2.8, color='0.7', linewidth=1)
ax.text(0.5 + 3*g, -0.3, 'X11/CSS4', ha='center')
ax.text(1.5 + 3*g, -0.3, 'xkcd', ha='center')
ax.set_xlim(0, 3 * n_groups)
ax.set_ylim(n_rows, -1)
ax.axis('off')
plt.show()
The effect is as follows :

3.2CN Color choices
Matplotlib When drawing, the “CN” The color changes to RGBA. The complete code is as follows :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
th = np.linspace(0, 2*np.pi, 128)
def demo(sty):
mpl.style.use(sty)
fig, ax = plt.subplots(figsize=(3, 3))
ax.set_title('style: {!r}'.format(sty), color='C0')
ax.plot(th, np.cos(th), 'C1', label='C1')
ax.plot(th, np.sin(th), 'C2', label='C2')
ax.legend()
demo('default')
demo('seaborn')
The effect is as follows :


Four . Reference resources
版权声明
本文为[The big pig of the little pig family]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230617063367.html
边栏推荐
猜你喜欢

Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.

【leetcode】102.二叉树的层序遍历

Manjaro installation and configuration (vscode, wechat, beautification, input method)

net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。

Yarn core parameter configuration
Detailed explanation of MapReduce calculation process

/Can etc / shadow be cracked?

Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition

A diary of dishes | 238 Product of arrays other than itself

Yarn resource scheduler
随机推荐
Simple thoughts on the design of a microblog database
Reading integrity monitoring techniques for vision navigation systems - 5 Results
Pycharm
997. Square of ordered array (array)
Anaconda3 installation
Let the LAN group use the remote device
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
Is the pointer symbol of C language close to variable type or variable name?
Differences among restful, soap, RPC, SOA and microservices
SSH利用私钥无密钥连接服务器踩坑实录
Swagger2 接口如何导入Postman
HuggingFace
我的创作纪念日
【leetcode】102.二叉树的层序遍历
997、有序数组的平方(数组)
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
Windows installs redis and sets the redis service to start automatically
VIM usage
What if Jerry's function to locate the corresponding address is not accurate sometimes? [chapter]