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

Posted 我是管小亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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课程推荐。

以上是关于plt.text()函数解析(最清晰的解释)的主要内容,如果未能解决你的问题,请参考以下文章

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

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

sklearn.linear_model.LogisticRegression()函数解析(最清晰的解释)

pd.DataFrame()函数解析(最清晰的解释)

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

js中this最简单清晰的解释