查看原文
其他

Python可视化|matplotlib03-一文掌握marker和linestyle使用

pythonic生物人 pythonic生物人 2022-09-11


"pythonic生物人"的第21篇分享



摘要

详细介绍Matplotlib绘图时标记(marker)和线性(linestyle)使用方法、以及自定义marker和linestyle的方法(这些marker和linestyle适合整个python生态绘图用,不仅仅是matplotlib,seaborn等其它绘图库通用)。

目录

1、标记(marker)
matplotlib入门级marker
matplotlib高手级marker
matplotlib高高手级marker
matplotlib中marker 怎么使用 

2 、线型(linestyle)

字符型linestyle
元组型linestyle
线型可视化效果
线型使用 
元组线型详解

3、参考资料



正文开始啦



1、标记(marker)

  • matplotlib入门级marker

matplotlib一般marker位于matplotlib.lines import Line2D中,共计30+种,可以输出来康康有哪些:

from matplotlib.lines import Line2Dprint([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
matplotlib官网上就有,请戳:https://matplotlib.org/tutorials/text/mathtext.html。一时冲动爬了下辣个网站,有400+种【感兴趣可以公众号私信我发给您】,长如下这样的:
可以显示的形状 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 pltplt.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')
#自定义markerplt.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)


2 、线型(linestyle)
线性 (linestyle)可分为字符串型的元组型的:
  • 字符型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 pltplt.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-pyhttps://matplotlib.org/gallery/lines_bars_and_markers/marker_reference.htmlhttps://matplotlib.org/tutorials/text/mathtext.htmlhttps://matplotlib.org/gallery/lines_bars_and_markers/marker_fillstyle_reference.htmlhttps://matplotlib.org/gallery/shapes_and_collections/marker_path.htmlhttps://matplotlib.org/gallery/lines_bars_and_markers/linestyles.html



同系列文章

python可视化|matplotlib01-绘图方式|图形结构

python可视化|matplotlib02-matplotlib.pyplot坐标轴|刻度值|刻度|标题设置


持续更新,欢迎您"关注"、"在看"、"分享"


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存