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课程推荐。
在这里插入图片描述

©️2020 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值