首页编程开发Matplotlib【数据可视化(Matplo...

【数据可视化(Matplotlib篇)】16.图表注释中的箭头样式

本系列文章配套代码获取有以下两种途径:

  • 通过百度网盘获取:
链接:https://pan.baidu.com/s/1jG-rGG4QMuZu0t0kEEl7SA?pwd=mnsj 提取码:mnsj
  • 前往GitHub获取
https://github.com/returu/Data_Visualization





在上一篇文章中,介绍了Matplotlibannotate()函数的使用,在实例中绘制的箭头为默认样式,本篇将介绍如何自定义注释中的箭头样式。
01

箭头样式


在上一篇文章中,arrowprops参数中没有设定arrowstyle,因此绘制的箭头为默认样式,此时可以使用下列关键词进行设置:
关键词参数
说明
width 箭头的宽度,单位是点
headwidth 箭头头部的宽度,单位是点
headlength 箭头头部的长度,单位是点
shrink 箭头两端收缩的百分比
? 任意matplotlib.patches.FancyArrowPatch属性
如果设定了arrowstyle,上表中的关键词就不可以使用,这时的arrowstyle可以参考下表:
类别
箭头样式
属性
Curve None
CurveA <- head_length=0.4, head_width=0.2
CurveB -> head_length=0.4, head_width=0.2
CurveAB <-> head_length=0.4, head_width=0.2
CurvefilledA
<|- head_length=0.4, head_width=0.2
CurvefilledB -|> head_length=0.4, head_width=0.2
CurvefilledAB <|-|> head_length=0.4, head_width=0.2
BrackedA ]- widthA=1.0, lengthA=0.2, angleA=0
BrackedB -] widthB=1.0, lengthB=0.2, angleB=0
BrackedAB ]-[

widthA=1.0, lengthA=0.2, angleA=0

widthB=1.0, lengthB=0.2, angleB=0

BarAB |-|

widthA=1.0, lengthA=0.2, widthB=0, angleB=0

BrackedCurve ]-> widthA=1.0, lengthA=0.2, angleA=None
CurveBarcked <-[ widthB=1.0, lengthB=0.2, angleB=None
Simple simple head_length=0.5, head_width=0.5, tail_width=0.2
Fancy fancy head_length=0.4, head_width=0.4, tail_width=0.4
Wedge wedge

tail_width=0.3, shrink_factor=0.5

通过以下代码列出完整的箭头样式:
ArrowStyle_list = ['-''<-''->''<->''<|-','-|>''<|-|>'']-''-['']-[''|-|'']->''<-[''simple''fancy''wedge']
ArrowStyle_name_list = ['Curve''CurveA''CurveB''CurveAB''CurvefilledA','CurvefilledB''CurvefilledAB''BrackedA''BrackedB''BrackedAB''BarAB''BrackedCurve''CurveBarcked''simple''fancy''wedge']

fig, axes = plt.subplots(44 , figsize=(1010))

for i in range(len(ArrowStyle_list)):
    axes[i//4,i%4].annotate(text=ArrowStyle_name_list[i], # 注释文字
                            fontweight
='bold'# 字体粗细
                            color='m'# 字体颜色
                            xy=(0.2,0.2),
                            xytext=(0.4,0.8),
                            arrowprops=dict(
                                facecolor='r'# 箭头填充色
                                edgecolor='k'# 箭头边框色
                                arrowstyle=ArrowStyle_list[i]) # 箭头样式
                           )
    axes[i//4,i%4].set_xticks([])
    axes[i//4,i%4].set_yticks([])
可视化效果如下图所示:


02

连接样式


另外,FancyArrowPatch中的connectionstyle(连接样式)包含样式参看下表angle3arc3中的3意味着所得到的路径是二次样条段,即三个控制点):

关键词
属性说明
angle

angleA=90, angleB=0, rad=0.0

angle3

angleA=90, angleB=0

arc angleA=90, angleB=0, armA=None, armB=None, rad=0.0
arc3 rad=0.0
bar armA=0.0, armB=0.0, fraction=0.3, angle=None

通过设置参数connectionstyle的具体值,绘制出以下连接样式:

connectionstyle_list = ["angle,angleA=-90,angleB=180,rad=0",
"angle,angleA=-90,angleB=180,rad=5",
"angle,angleA=-90,angleB=10,rad=5",
"angle3,angleA=90,angleB=0",
"angle3,angleA=0,angleB=90",
"arc,angleA=-90,angleB=0,armA=30,armB=30,rad=0",
"arc,angleA=-90,angleB=0,armA=30,armB=30,rad=5",
"arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0",
"arc3,rad=0.",
"arc3,rad=0.3",
"arc3,rad=-0.3",
"bar,fraction=0.3",
"bar,fraction=0.5,angle=60",
"bar,fraction=0.3,armA=30,armB=30",
"bar,fraction=-0.3",
"bar,angle=180,fraction=-0.2"]

connectionstyle_name = ["anglenangleA=-90nangleB=180nrad=0",
"anglenangleA=-90nangleB=180nrad=5",
"anglenangleA=-90nangleB=10nrad=5",
"angle3nangleA=90nangleB=0",
"angle3nangleA=0nangleB=90",
"arcnangleA=-90nangleB=0narmA=30narmB=30nrad=0",
"arcnangleA=-90nangleB=0narmA=30narmB=30nrad=5",
"arcnangleA=-90nangleB=0narmA=0narmB=40nrad=0",
"arc3nrad=0.",
"arc3nrad=0.3",
"arc3nrad=-0.3",
"barnfraction=0.3",
"barnfraction=0.5nangle=60",
"barnfraction=0.3narmA=30narmB=30",
"barnfraction=-0.3",
"barnangle=180nfraction=-0.2"]

fig, axes = plt.subplots(44 , figsize=(2020))

for i in range(len(connectionstyle_list)):
    axes[i//4,i%4].annotate(text=connectionstyle_name[i], # 注释文字
                            fontweight
='bold'# 字体粗细
                            fontsize=14# 字体大小
                            color='m'# 字体颜色
                            xy=(0.2,0.2),
                            xytext=(0.5,0.6),
                            arrowprops=dict(
                                facecolor='r'# 箭头填充色
                                edgecolor='k'# 箭头边框色
                                arrowstyle='->'# 箭头样式
                                connectionstyle=connectionstyle_list[i] # 连接样式
                           )
                           )

    axes[i//4,i%4].set_xticks([])
    axes[i//4,i%4].set_yticks([])
可视化效果如下图所示:


更多内容可以前往官网查看

https://matplotlib.org/stable/


本篇文章来源于微信公众号: 码农设计师

RELATED ARTICLES

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments