matplotlib之text

text函数

matplotlib.pyplot.text(
    x, y, s, fontdict=None, 
    withdash=False, **kwargs)

参数

x, y : 文字位置坐标

s : 文字内容

fontdict : 字典, 可选, 文字属性

withdash : boolean, optional, default: False

返回值

Text

FontProperties类

class matplotlib.font_manager.FontProperties(
    family=None, style=None, variant=None,
     weight=None, stretch=None, size=None, 
     fname=None, _init=None)

family: 字体优先级列表‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, ‘monospace’

style: 字体风格 ‘normal’, ‘italic’ , ‘oblique’.

variant: ‘normal’ or ‘small-caps’.

stretch: 0-1000数字或者 ‘ultra-condensed’, ‘extra-condensed’, ‘condensed’, ‘semi-condensed’, ‘normal’, ‘semi-expanded’, ‘expanded’, ‘extra-expanded’, ‘ultra-expanded’中的一个

weight: 字体粗细,0-1000之间数字或者 ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’中的一个

size: 字体大小,‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’ 或者一个整数

实例

#-*-coding:utf-8-*-
from __future__ import print_function
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt

# text = "TEXT"
ax = plt.subplot(331)
ax.axis([-2,2,-2,2])
ax.text(0,0, 'TEXT')
ax.set_title("(0,0)")

ax = plt.subplot(332)
ax.axis([-2,2,-2,2])
ax.text(0,0, 'TEXT',withdash=True)
ax.set_title("withdash=True")

ax = plt.subplot(333)
ax.axis([-2,2,-2,2])
ax.text(0,0,'TEXT',ha="center",size=20,bbox=dict(boxstyle="circle", fc="b", ec="r"))
ax.set_title("bbox=dict(boxstyle=circle, fc=b, ec=r)")

ax = plt.subplot(334)
ax.axis([-2,2,-2,2])
# ax.text(0,0,'TEXT',size=20, ha='left', rotation=15, wrap=True,style='oblique',va="top")
ax.text(0,0,'TEXT',size=20, ha='left', rotation=15,style='oblique',va="top")
ax.set_title("size=20, ha=left, rotation=15,style=oblique,va=top")


ax = plt.subplot(335)
ax.axis([-2,2,-2,2])
ax.text(0,0,'TEXT',size=15, rotation=30.,ha="center", va="center",bbox=dict(boxstyle="round",ec=(1., 0.5, 0.5),fc=(1., 0.8, 0.8)))
ax.set_title("srotation=30")


ax = plt.subplot(336)
ax.axis([-2,2,-2,2])
ax.text(0,0,'TEXT',size=15, rotation=-30.,ha="right", va="top",bbox=dict(boxstyle="square",ec=(1., 0.5, 0.5),fc=(1., 0.8, 0.8)))
ax.set_title("rotation=-30")

ax = plt.subplot(337)
ax.axis([-2,2,-2,2])

fp = FontProperties()
fp.set_size('xx-large')
fp.set_family('serif')
fp.set_variant('small-caps')
fp.set_weight('bold')
fp.set_style('italic')

ax.text(0,0,'TEXT',fontproperties=fp)
ax.set_title("fontproperties=fp")

ax = plt.subplot(338)
ax.axis([-2,2,-2,2])
ax.text(0,0,'TEXT',size=15,zorder=2)
ax.text(0,0.2,'TEXT',size=15,color="red",zorder=1,alpha=0.3)
ax.set_title(u"zorder对比,和html中的z-index相似")

ax = plt.subplot(339)
ax.axis([-2,2,-2,2])
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 16,
        }
ax.text(0,0,'TEXT',size=15,fontdict=font)
ax.set_title("fontdict")

plt.gcf().set_size_inches(12,10)
plt.savefig("text.png")
plt.show()

大概的样子就像是下面的样子的:

text

参考

matplotlib.pyplot.text

Text

Fancybox Demo

matplotlib.patches.BoxStyle

Fonts参数对比

FancyBboxPatch

猜你喜欢

转载自my.oschina.net/u/2474629/blog/1794900