使用Axesgrid为Ylabel腾出空间

使用Axesgrid为Ylabel腾出空间

使用Axesgrid为Ylabel腾出空间示例1

使用Axesgrid为Ylabel腾出空间示例2

  1. from mpl_toolkits.axes_grid1 import make_axes_locatable
  2. from mpl_toolkits.axes_grid1.axes_divider import make_axes_area_auto_adjustable
  3. if __name__ == "__main__":
  4. import matplotlib.pyplot as plt
  5. def ex1():
  6. plt.figure(1)
  7. ax = plt.axes([0, 0, 1, 1])
  8. #ax = plt.subplot(111)
  9. ax.set_yticks([0.5])
  10. ax.set_yticklabels(["very long label"])
  11. make_axes_area_auto_adjustable(ax)
  12. def ex2():
  13. plt.figure(2)
  14. ax1 = plt.axes([0, 0, 1, 0.5])
  15. ax2 = plt.axes([0, 0.5, 1, 0.5])
  16. ax1.set_yticks([0.5])
  17. ax1.set_yticklabels(["very long label"])
  18. ax1.set_ylabel("Y label")
  19. ax2.set_title("Title")
  20. make_axes_area_auto_adjustable(ax1, pad=0.1, use_axes=[ax1, ax2])
  21. make_axes_area_auto_adjustable(ax2, pad=0.1, use_axes=[ax1, ax2])
  22. def ex3():
  23. fig = plt.figure(3)
  24. ax1 = plt.axes([0, 0, 1, 1])
  25. divider = make_axes_locatable(ax1)
  26. ax2 = divider.new_horizontal("100%", pad=0.3, sharey=ax1)
  27. ax2.tick_params(labelleft=False)
  28. fig.add_axes(ax2)
  29. divider.add_auto_adjustable_area(use_axes=[ax1], pad=0.1,
  30. adjust_dirs=["left"])
  31. divider.add_auto_adjustable_area(use_axes=[ax2], pad=0.1,
  32. adjust_dirs=["right"])
  33. divider.add_auto_adjustable_area(use_axes=[ax1, ax2], pad=0.1,
  34. adjust_dirs=["top", "bottom"])
  35. ax1.set_yticks([0.5])
  36. ax1.set_yticklabels(["very long label"])
  37. ax2.set_title("Title")
  38. ax2.set_xlabel("X - Label")
  39. ex1()
  40. ex2()
  41. ex3()
  42. plt.show()

下载这个示例