0.98.4版本新的炫酷特性

创建精美的盒子和箭头样式。

  1. import matplotlib.patches as mpatch
  2. import matplotlib.pyplot as plt
  3. figheight = 8
  4. fig = plt.figure(1, figsize=(9, figheight), dpi=80)
  5. fontsize = 0.4 * fig.dpi
  6. def make_boxstyles(ax):
  7. styles = mpatch.BoxStyle.get_styles()
  8. for i, (stylename, styleclass) in enumerate(sorted(styles.items())):
  9. ax.text(0.5, (float(len(styles)) - 0.5 - i)/len(styles), stylename,
  10. ha="center",
  11. size=fontsize,
  12. transform=ax.transAxes,
  13. bbox=dict(boxstyle=stylename, fc="w", ec="k"))
  14. def make_arrowstyles(ax):
  15. styles = mpatch.ArrowStyle.get_styles()
  16. ax.set_xlim(0, 4)
  17. ax.set_ylim(0, figheight)
  18. for i, (stylename, styleclass) in enumerate(sorted(styles.items())):
  19. y = (float(len(styles)) -0.25 - i) # /figheight
  20. p = mpatch.Circle((3.2, y), 0.2, fc="w")
  21. ax.add_patch(p)
  22. ax.annotate(stylename, (3.2, y),
  23. (2., y),
  24. #xycoords="figure fraction", textcoords="figure fraction",
  25. ha="right", va="center",
  26. size=fontsize,
  27. arrowprops=dict(arrowstyle=stylename,
  28. patchB=p,
  29. shrinkA=5,
  30. shrinkB=5,
  31. fc="w", ec="k",
  32. connectionstyle="arc3,rad=-0.05",
  33. ),
  34. bbox=dict(boxstyle="square", fc="w"))
  35. ax.xaxis.set_visible(False)
  36. ax.yaxis.set_visible(False)
  37. ax1 = fig.add_subplot(121, frameon=False, xticks=[], yticks=[])
  38. make_boxstyles(ax1)
  39. ax2 = fig.add_subplot(122, frameon=False, xticks=[], yticks=[])
  40. make_arrowstyles(ax2)
  41. plt.show()

新特性示例

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.patches
  3. matplotlib.patches.BoxStyle
  4. matplotlib.patches.BoxStyle.get_styles
  5. matplotlib.patches.ArrowStyle
  6. matplotlib.patches.ArrowStyle.get_styles
  7. matplotlib.axes.Axes.text
  8. matplotlib.axes.Axes.annotate

下载这个示例