Python可视化|matplotlib03-一文掌握marker和linestyle使用
"pythonic生物人"的第21篇分享
摘要
详细介绍Matplotlib绘图时标记(marker)和线性(linestyle)使用方法、
以及自定义marker和linestyle的方法(这些marker和linestyle适合整个python
生态绘图用,不仅仅是matplotlib,seaborn等其它绘图库通用)。
目录
2 、线型(linestyle)
3、参考资料
正文开始啦
1、标记(marker)
matplotlib入门级marker
matplotlib一般marker位于matplotlib.lines import Line2D中,共计30+种,可以输出来康康有哪些:
from matplotlib.lines import Line2D
print([m for m, func in Line2D.markers.items()
if func != 'nothing' and m not in Line2D.filled_markers] + list(Line2D.filled_markers))
['.', ',', '1', '2', '3', '4', '+', 'x', '|', '_', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X']
这些marker都长什么样纸了,来康康:
37种marker,一般绘图可以完全满足了,如果您说不满足,想要更酷炫的,比如说下面这种,
往下看,都给你:
matplotlib高手级marker
可以显示的形状 marker名称
ϖ \varpi
ϱ \varrho
ς \varsigma
ϑ \vartheta
ξ \xi
ζ \zeta
Δ \Delta
Γ \Gamma
Λ \Lambda
Ω \Omega
Φ \Phi
Π \Pi
Ψ \Psi
Σ \Sigma
Θ \Theta
Υ \Upsilon
Ξ \Xi
℧ \mho
∇ \nabla
ℵ \aleph
ℶ \beth
ℸ \daleth
ℷ \gimel
/ /
[ [
⇓ \Downarrow
⇑ \Uparrow
‖ \Vert
↓ \downarrow
⟨ \langle
⌈ \lceil
⌊ \lfloor
⌞ \llcorner
⌟ \lrcorner
⟩ \rangle
⌉ \rceil
⌋ \rfloor
⌜ \ulcorner
↑ \uparrow
⌝ \urcorner
\vert
{ \{
\|
} \}
] ]
|
⋂ \bigcap
⋃ \bigcup
⨀ \bigodot
⨁ \bigoplus
⨂ \bigotimes
⨄ \biguplus
⋁ \bigvee
⋀ \bigwedge
∐ \coprod
∫ \int
∮ \oint
∏ \prod
∑ \sum
matplotlib高高手级marker
当然,最高境界是想咋滴就咋滴,自己造一个形状;
自定义marker,使用两个美元符号($)包围你想要显示的东东,目前试验了666不错,可以自行实验。
matplotlib中marker 怎么使用
非常简单,入门级marker使用时,marker=marker名称;
高手级和自定义级marker使用时,marker=$marker名称$;
举个栗子:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] # 用于显示中文
plt.rcParams['axes.unicode_minus'] = False # 用于显示中文
plt.figure(dpi=200)
#常规marker使用
plt.plot([1,2,3],[1,2,3],marker=4, markersize=15, color='lightblue',label='常规marker')
plt.plot([1.8,2.8,3.8],[1,2,3],marker='2', markersize=15, color='#ec2d7a',label='常规marker')
#非常规marker使用
#注意使用两个$符号包围名称
plt.plot([1,2,3],[4,5,6],marker='$\circledR$', markersize=15, color='r', alpha=0.5,label='非常规marker')
plt.plot([1.5,2.5,3.5],[1.25,2.1,6.5],marker='$\heartsuit$', markersize=15, color='#f19790', alpha=0.5,label='非常规marker')
plt.plot([1,2,3],[2.5,6.2,8],marker='$\clubsuit$', markersize=15, color='g', alpha=0.5,label='非常规marker')
#自定义marker
plt.plot([1.2,2.2,3.2],[1,2,3],marker='$666$', markersize=15, color='#2d0c13',label='自定义marker')
plt.legend(loc='upper left')
for i in ['top','right']:
plt.gca().spines[i].set_visible(False)
字符型linestyle
有四种,如下:
linestyle_str = [
('solid', 'solid'), # Same as (0, ()) or '-';solid’, (0, ()) , '-'三种都代表实线。
('dotted', 'dotted'), # Same as (0, (1, 1)) or '.'
('dashed', 'dashed'), # Same as '--'
('dashdot', 'dashdot')] # Same as '-.'
元组型linestyle
直接修改元组中的数字可以呈现不同的线型,所以有无数种该线型。
linestyle_tuple = [
('loosely dotted', (0, (1, 10))),
('dotted', (0, (1, 1))),
('densely dotted', (0, (1, 2))),
('loosely dashed', (0, (5, 10))),
('dashed', (0, (5, 5))),
('densely dashed', (0, (5, 1))),
('loosely dashdotted', (0, (3, 10, 1, 10))),
('dashdotted', (0, (3, 5, 1, 5))),
('densely dashdotted', (0, (3, 1, 1, 1))),
('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]
线型可视化效果
线型使用
import matplotlib.pyplot as plt
plt.figure(dpi=120)
#字符型linestyle使用方法
plt.plot([1,2,3],[1,2,13],linestyle='dotted', color='#1661ab', linewidth=5, label='字符型线性:dotted')
#元组型lintstyle使用方法
plt.plot([0.8,0.9,1.5],[0.8,0.9,21.5],linestyle=(0,(3, 1, 1, 1, 1, 1)), color='#ec2d7a', linewidth=5, label='元组型线性:(0,(3, 1, 1, 1, 1, 1)')
for i in ['top','right']:
plt.gca().spines[i].set_visible(False)
#自定义inestyle
plt.plot([1.5,2.5,3.5],[1,2,13],linestyle=(0,(1,2,3,4,2,2)), color='black', linewidth=5, label='自定义线性:(0,(1,2,3,4,2,2)))')
plt.plot([2.5,3.5,4.5],[1,2,13],linestyle=(2,(1,2,3,4,2,2)), color='g', linewidth=5, label='自定义线性:(1,(1,2,3,4,2,2)))')
plt.legend()
元组线型详解
第一个0的意义,比较黑色和绿色线性即可知道
1,2 第一小段线宽1磅,第一和第二段之间距离2磅
3,4 第二小段线宽3磅,第二和第三段之间距离4磅
2,2 第三小段线宽2磅,第三和第四段之间距离2磅
3、参考资料
https://matplotlib.org/gallery/lines_bars_and_markers/marker_reference.html#sphx-glr-gallery-lines-bars-and-markers-marker-reference-py
https://matplotlib.org/gallery/lines_bars_and_markers/marker_reference.html
https://matplotlib.org/tutorials/text/mathtext.html
https://matplotlib.org/gallery/lines_bars_and_markers/marker_fillstyle_reference.html
https://matplotlib.org/gallery/shapes_and_collections/marker_path.html
https://matplotlib.org/gallery/lines_bars_and_markers/linestyles.html
python可视化|matplotlib01-绘图方式|图形结构
python可视化|matplotlib02-matplotlib.pyplot坐标轴|刻度值|刻度|标题设置