演示寄生轴2

寄生轴的演示。

以下代码是寄生虫轴的示例。 它旨在展示如何在一个单独的图上绘制多个不同的值。 请注意,在此示例中,par1和par2都调用twinx,这意味着两者都直接绑定到x轴。 从那里,这两个轴中的每一个可以彼此分开地表现,这意味着它们可以从它们自身以及x轴上获取单独的值。

请注意,此方法使用 parasite_axesHostAxesParasiteAxes。 在 Demo Parasite Axes2 示例中可以找到使用Matplotlib axes_grid1 Toolkit 和 Matplotlib axisartist Toolkit的替代方法。 使用通常的matplotlib子图的替代方法显示在 Multiple Yaxis With Spines 示例中。

演示寄生轴2

  1. from mpl_toolkits.axes_grid1 import host_subplot
  2. import mpl_toolkits.axisartist as AA
  3. import matplotlib.pyplot as plt
  4. host = host_subplot(111, axes_class=AA.Axes)
  5. plt.subplots_adjust(right=0.75)
  6. par1 = host.twinx()
  7. par2 = host.twinx()
  8. offset = 60
  9. new_fixed_axis = par2.get_grid_helper().new_fixed_axis
  10. par2.axis["right"] = new_fixed_axis(loc="right",
  11. axes=par2,
  12. offset=(offset, 0))
  13. par1.axis["right"].toggle(all=True)
  14. par2.axis["right"].toggle(all=True)
  15. host.set_xlim(0, 2)
  16. host.set_ylim(0, 2)
  17. host.set_xlabel("Distance")
  18. host.set_ylabel("Density")
  19. par1.set_ylabel("Temperature")
  20. par2.set_ylabel("Velocity")
  21. p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
  22. p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
  23. p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
  24. par1.set_ylim(0, 4)
  25. par2.set_ylim(1, 65)
  26. host.legend()
  27. host.axis["left"].label.set_color(p1.get_color())
  28. par1.axis["right"].label.set_color(p2.get_color())
  29. par2.axis["right"].label.set_color(p3.get_color())
  30. plt.show()

下载这个示例