字体演示(面向对象的风格)

使用setter设置字体属性。

请参见字体演示(Kwargs),以使用kwargs实现相同的效果。

字体演示

  1. from matplotlib.font_manager import FontProperties
  2. import matplotlib.pyplot as plt
  3. plt.subplot(111, facecolor='w')
  4. font0 = FontProperties()
  5. alignment = {'horizontalalignment': 'center', 'verticalalignment': 'baseline'}
  6. # Show family options
  7. families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']
  8. font1 = font0.copy()
  9. font1.set_size('large')
  10. t = plt.text(-0.8, 0.9, 'family', fontproperties=font1,
  11. **alignment)
  12. yp = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
  13. for k, family in enumerate(families):
  14. font = font0.copy()
  15. font.set_family(family)
  16. t = plt.text(-0.8, yp[k], family, fontproperties=font,
  17. **alignment)
  18. # Show style options
  19. styles = ['normal', 'italic', 'oblique']
  20. t = plt.text(-0.4, 0.9, 'style', fontproperties=font1,
  21. **alignment)
  22. for k, style in enumerate(styles):
  23. font = font0.copy()
  24. font.set_family('sans-serif')
  25. font.set_style(style)
  26. t = plt.text(-0.4, yp[k], style, fontproperties=font,
  27. **alignment)
  28. # Show variant options
  29. variants = ['normal', 'small-caps']
  30. t = plt.text(0.0, 0.9, 'variant', fontproperties=font1,
  31. **alignment)
  32. for k, variant in enumerate(variants):
  33. font = font0.copy()
  34. font.set_family('serif')
  35. font.set_variant(variant)
  36. t = plt.text(0.0, yp[k], variant, fontproperties=font,
  37. **alignment)
  38. # Show weight options
  39. weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
  40. t = plt.text(0.4, 0.9, 'weight', fontproperties=font1,
  41. **alignment)
  42. for k, weight in enumerate(weights):
  43. font = font0.copy()
  44. font.set_weight(weight)
  45. t = plt.text(0.4, yp[k], weight, fontproperties=font,
  46. **alignment)
  47. # Show size options
  48. sizes = ['xx-small', 'x-small', 'small', 'medium', 'large',
  49. 'x-large', 'xx-large']
  50. t = plt.text(0.8, 0.9, 'size', fontproperties=font1,
  51. **alignment)
  52. for k, size in enumerate(sizes):
  53. font = font0.copy()
  54. font.set_size(size)
  55. t = plt.text(0.8, yp[k], size, fontproperties=font,
  56. **alignment)
  57. # Show bold italic
  58. font = font0.copy()
  59. font.set_style('italic')
  60. font.set_weight('bold')
  61. font.set_size('x-small')
  62. t = plt.text(-0.4, 0.1, 'bold italic', fontproperties=font,
  63. **alignment)
  64. font = font0.copy()
  65. font.set_style('italic')
  66. font.set_weight('bold')
  67. font.set_size('medium')
  68. t = plt.text(-0.4, 0.2, 'bold italic', fontproperties=font,
  69. **alignment)
  70. font = font0.copy()
  71. font.set_style('italic')
  72. font.set_weight('bold')
  73. font.set_size('x-large')
  74. t = plt.text(-0.4, 0.3, 'bold italic', fontproperties=font,
  75. **alignment)
  76. plt.axis([-1, 1, 0, 1])
  77. plt.show()

下载这个示例