简单的轴线

简单的轴线

  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.axisartist.axislines import SubplotZero
  3. fig = plt.figure(1)
  4. fig.subplots_adjust(right=0.85)
  5. ax = SubplotZero(fig, 1, 1, 1)
  6. fig.add_subplot(ax)
  7. # make right and top axis invisible
  8. ax.axis["right"].set_visible(False)
  9. ax.axis["top"].set_visible(False)
  10. # make xzero axis (horizontal axis line through y=0) visible.
  11. ax.axis["xzero"].set_visible(True)
  12. ax.axis["xzero"].label.set_text("Axis Zero")
  13. ax.set_ylim(-2, 4)
  14. ax.set_xlabel("Label X")
  15. ax.set_ylabel("Label Y")
  16. # or
  17. #ax.axis["bottom"].label.set_text("Label X")
  18. #ax.axis["left"].label.set_text("Label Y")
  19. # make new (right-side) yaxis, but with some offset
  20. offset = (20, 0)
  21. new_axisline = ax.get_grid_helper().new_fixed_axis
  22. ax.axis["right2"] = new_axisline(loc="right", offset=offset, axes=ax)
  23. ax.axis["right2"].label.set_text("Label Y2")
  24. ax.plot([-2, 3, 2])
  25. plt.show()

下载这个示例