文本自动换行

Matplotlib can wrap text automatically, but if it’s too long, the text will be displayed slightly outside of the boundaries of the axis anyways.

文本自动换行示例

  1. import matplotlib.pyplot as plt
  2. fig = plt.figure()
  3. plt.axis([0, 10, 0, 10])
  4. t = ("This is a really long string that I'd rather have wrapped so that it "
  5. "doesn't go outside of the figure, but if it's long enough it will go "
  6. "off the top or bottom!")
  7. plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
  8. plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
  9. plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)
  10. plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
  11. va='top', wrap=True)
  12. plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)
  13. plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)
  14. plt.show()

下载这个示例