当前位置:网站首页>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
边栏推荐
- Jerry's users how to handle events in the simplest way [chapter]
- MapReduce compression
- [provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)
- 精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
- Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
- 定义链表(链表)
- Understand the key points of complement
- MapReduce core and foundation demo
- Anaconda3 installation
- Comparison and practice of prototype design of knowledge service app
猜你喜欢
Download and installation steps of xshell + xftp
Reading integrity monitoring techniques for vision navigation systems - 5 Results
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
部署jar包
景联文科技—专业数据标注公司和智能数据标注平台
Ueditor -- limitation of 4m size of image upload component
A diary of dishes | 238 Product of arrays other than itself
Yarn core parameter configuration
Charles 功能介绍和使用教程
随机推荐
Image processing - Noise notes
Read integrity monitoring techniques for vision navigation systems
206. Reverse linked list (linked list)
142. Circular linked list||
高价买来的课程,公开了!phper资料分享
部署jar包
Construction and traversal of binary tree
【leetcode】102. Sequence traversal of binary tree
MySql常用语句
209、长度最小的子数组(数组)
二叉树的构建和遍历
Yarn resource scheduler
SSH uses private key to connect to server without key
景联文科技—专业数据标注公司和智能数据标注平台
Source insight 4.0 FAQs
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
Swagger2 接口如何导入Postman
Resolution and size of mainstream mobile phones
Anaconda3 installation
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis