处理文本 - 文本属性及布局

优质
小牛编辑
139浏览
2023-12-01

原文:Text properties and layout

matplotlib.text.Text实例有各种属性,可以通过关键字参数配置文本命令(例如,title()xlabel()text())。

属性值类型
alpha浮点
backgroundcolor任何 matplotlib 颜色
bboxrectangle prop dict plus key ‘pad’ which is a pad in points
clip_boxmatplotlib.transform.Bbox 实例
clip_on[True / False]
clip_pathPathTransformPatch 实例
color任何 matplotlib 颜色
family[ 'serif' / 'sans-serif' / 'cursive' / 'fantasy' / 'monospace' ]
fontpropertiesmatplotlib.font_manager.FontProperties 实例
horizontalalignment or ha[ 'center' / 'right' / 'left' ]
label任何字符串
linespacing浮点
multialignment['left' / 'right' / 'center' ]
name or fontname字符串,例如 ['Sans' / 'Courier' / 'Helvetica' ...]
picker[None / 浮点 / 布尔值 / 可调用对象]`
position(x,y)
rotation[ 角度制的角度 / ‘vertical’ / ‘horizontal’
size or fontsize[ 点的尺寸相对尺寸,例如 ['smaller', 'x-large' ]
style or fontstyle[ 'normal' / 'italic' / 'oblique']
text字符串或任何可使用'%s'打印的东西
transformmatplotlib.transform 实例
variant[ 'normal' / 'small-caps' ]
verticalalignment or va[ 'center' / 'top' / 'bottom' / 'baseline' ]
visible[True / False]
weight or fontweight[ 'normal' / 'bold' / 'heavy' / 'light' / 'ultrabold' / 'ultralight']
x浮点
y浮点
zorder任意数值

你可以使用对齐参数horizontalalignmentverticalalignmentmultialignment来布置文本。 horizontalalignment控制文本的x位置参数表示文本边界框的左边,中间或右边。 verticalalignment控制文本的y位置参数表示文本边界框的底部,中心或顶部。 multialignment,仅对于换行符分隔的字符串,控制不同的行是左,中还是右对齐。 这里是一个使用text()命令显示各种对齐方式的例子。 在整个代码中使用transform = ax.transAxes,表示坐标相对于轴边界框给出,其中0,0是轴的左下角,1,1是右上角。

  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as patches
  3. # build a rectangle in axes coords
  4. left, width = .25, .5
  5. bottom, height = .25, .5
  6. right = left + width
  7. top = bottom + height
  8. fig = plt.figure()
  9. ax = fig.add_axes([0,0,1,1])
  10. # axes coordinates are 0,0 is bottom left and 1,1 is upper right
  11. p = patches.Rectangle(
  12. (left, bottom), width, height,
  13. fill=False, transform=ax.transAxes, clip_on=False
  14. )
  15. ax.add_patch(p)
  16. ax.text(left, bottom, 'left top',
  17. horizontalalignment='left',
  18. verticalalignment='top',
  19. transform=ax.transAxes)
  20. ax.text(left, bottom, 'left bottom',
  21. horizontalalignment='left',
  22. verticalalignment='bottom',
  23. transform=ax.transAxes)
  24. ax.text(right, top, 'right bottom',
  25. horizontalalignment='right',
  26. verticalalignment='bottom',
  27. transform=ax.transAxes)
  28. ax.text(right, top, 'right top',
  29. horizontalalignment='right',
  30. verticalalignment='top',
  31. transform=ax.transAxes)
  32. ax.text(right, bottom, 'center top',
  33. horizontalalignment='center',
  34. verticalalignment='top',
  35. transform=ax.transAxes)
  36. ax.text(left, 0.5*(bottom+top), 'right center',
  37. horizontalalignment='right',
  38. verticalalignment='center',
  39. rotation='vertical',
  40. transform=ax.transAxes)
  41. ax.text(left, 0.5*(bottom+top), 'left center',
  42. horizontalalignment='left',
  43. verticalalignment='center',
  44. rotation='vertical',
  45. transform=ax.transAxes)
  46. ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
  47. horizontalalignment='center',
  48. verticalalignment='center',
  49. fontsize=20, color='red',
  50. transform=ax.transAxes)
  51. ax.text(right, 0.5*(bottom+top), 'centered',
  52. horizontalalignment='center',
  53. verticalalignment='center',
  54. rotation='vertical',
  55. transform=ax.transAxes)
  56. ax.text(left, top, 'rotated\nwith newlines',
  57. horizontalalignment='center',
  58. verticalalignment='center',
  59. rotation=45,
  60. transform=ax.transAxes)
  61. ax.set_axis_off()
  62. plt.show()

文本属性及布局 - 图1