当前位置:网站首页>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
边栏推荐
- Can Jerry's AES 256bit [chapter]
- Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
- App. In wechat applet JS files, components, APIs
- 景联文科技—专业数据标注公司和智能数据标注平台
- Contact between domain name and IP address
- MapReduce compression
- MySQL how to merge the same data in the same table
- How can swagger2 custom parameter annotations not be displayed
- Notes on concurrent programming of vegetables (V) thread safety and lock solution
- 定义链表(链表)
猜你喜欢

How to quickly download vscode

Charles 功能介绍和使用教程

精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》

比深度学习更值得信赖的模型ART

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

Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
MapReduce core and foundation demo

Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file

ID number verification system based on visual structure - Raspberry implementation

Xshell+Xftp 下载安装步骤
随机推荐
解决方案架构师的小锦囊 - 架构图的 5 种类型
Detailed explanation of MapReduce calculation process
Restful、SOAP、RPC、SOA、微服务之间的区别
Yarn core parameter configuration
Solution architect's small bag - 5 types of architecture diagrams
Windows installs redis and sets the redis service to start automatically
Installing MySQL with CentOS / Linux
解决方案架构师的小锦囊 - 架构图的 5 种类型
Construction and traversal of binary tree
Precautions for latex formula
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
Charles 功能介绍和使用教程
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
MapReduce compression
202、快乐数
Deploy jar package
部署jar包
mysql同一个表中相同数据怎么合并
A diary of dishes | 238 Product of arrays other than itself
Reading integrity monitoring techniques for vision navigation systems - 3 background