• plt.text()函数解析(最清晰的解释)


    欢迎关注WX公众号:【程序员管小亮】

    plt.text()函数用于设置文字说明。

    plt.text(x,
    	y,
    	string,
    	fontsize=15,
    	verticalalignment="top",
    	horizontalalignment="right"
    )
    

    参数:

    • x,y:表示坐标值上的值

    • string:表示说明文字

    • fontsize:表示字体大小

    • verticalalignment:垂直对齐方式 ,参数:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]

    • horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ]

    • xycoords选择指定的坐标轴系统:

      • figure points:图左下角的点
      • figure pixels:图左下角的像素
      • figure fraction:图的左下部分
      • axes points:坐标轴左下角的点
      • axes pixels:坐标轴左下角的像素
      • axes fraction:左下轴的分数
      • data:使用被注释对象的坐标系统(默认)
      • polar(theta,r):if not native ‘data’ coordinates t
    • arrowprops #箭头参数,参数类型为字典dict

      • width:箭头的宽度(以点为单位)
      • headwidth:箭头底部以点为单位的宽度
      • headlength:箭头的长度(以点为单位)
      • shrink:总长度的一部分,从两端“收缩”
      • facecolor:箭头颜色
    • bbox给标题增加外框 ,常用参数如下:

      • boxstyle:方框外形
      • facecolor:(简写fc)背景颜色
      • edgecolor:(简写ec)边框线条颜色
      • edgewidth:边框线条大小

    例子1:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    plt.axis([0, 10, 0, 10])
    t = "This is a really long string that I'd rather have wrapped so that it"
        " doesn't go outside of the figure, but if it's long enough it will go"
        " off the top or bottom!"
    plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
    plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
    plt.text(6, 5, t, ha='left', rotation=15, wrap=False)
    plt.show()
    

    在这里插入图片描述
    (x,y)参数是句子头的坐标。

    例子2:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    plt.axis([0, 10, 0, 10])
    t = "This is a really long string that I'd rather have wrapped so that it"
        " doesn't go outside of the figure, but if it's long enough it will go"
        " off the top or bottom!"
    plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
    plt.text(6, 5, t, ha='left', rotation=-15, wrap=True)
    plt.text(6, 5, t, ha='left', rotation=-50, wrap=True)
    plt.show()
    

    在这里插入图片描述

    1. ha为’left’参数, rotation>0的时候,句子头这一侧更低。
    2. ha为’left’参数, rotation<0的时候,做上面的1的关于水平坐标轴的镜像。
    3. rotation参数是和水平坐标轴的夹角。

    例子3:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    plt.axis([0, 10, 0, 10])
    t = "This is a really long string that I'd rather have wrapped so that it"
        " doesn't go outside of the figure, but if it's long enough it will go"
        " off the top or bottom!"
    plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
    plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',va='top',wrap=True)
    plt.show()
    

    在这里插入图片描述
    fontsize,style,ha,va参数分别是字号,字体,垂直对齐方式,水平对齐方式。

    例子4:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    plt.axis([0, 10, 0, 10])
    t = "This is a really long string that I'd rather have wrapped so that it"
        " doesn't go outside of the figure, but if it's long enough it will go"
        " off the top or bottom!"
    plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
    plt.text(4, 4, t, family='serif', style='italic', ha='right', wrap=True)
    plt.text(4, 6, t, style='italic', ha='right', wrap=True)
    plt.show()
    

    在这里插入图片描述
    family参数应该是一个字体参数。

    例子4:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    plt.axis([0, 10, 0, 10])
    t = "This is a really long string that I'd rather have wrapped so that it"
        " doesn't go outside of the figure, but if it's long enough it will go"
        " off the top or bottom!"
    plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
    plt.text(2, 5, t, ha='left', rotation=15, wrap=True,  bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5))
    plt.show()
    

    在这里插入图片描述
    这个参数和plt.annotate()一样。

    python课程推荐。
    在这里插入图片描述

  • 相关阅读:
    使用dozermapper,处理不了LocalDateTime的映射问题:java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
    mybatis-plus使用Wrapper自定义sql时出现错误:Invalid bound statement (not found)
    com.baomidou.mybatisplus.core.mapper 不存在
    python爬虫
    DRF源码系列分析
    python学习目录
    脚本加载django环境
    celery定时任务
    用脚本创建django-orm数据库表数据
    关于python很吊的一项技术!!!!!
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302835.html
Copyright © 2020-2023  润新知