首页 > 解决方案 > python - 在主 x 轴旁边制作一个附加(寄生轴)

问题描述

我正在使用下面的示例,该示例显示了 y 轴旁边的附加轴的构造。 https://matplotlib.org/2.0.2/examples/axes_grid/demo_parasite_axes2.html

在此处输入图像描述

我想知道的是如何修改代码以在主 x 轴旁边的底部获得另一个寄生轴,使其直接与主 y 轴相关联。

标签: pythonmatplotlib

解决方案


我已经编辑了官方参考中的示例以实现您的目标。这是通过调整偏移位置和 y 轴范围来实现的。这是否符合您的问题的意图?

from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits import axisartist
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=axisartist.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twiny()
par2 = host.twiny()

par2.axis["bottom"] = par2.new_fixed_axis(loc="bottom", offset=(0, 25))

par1.axis["top"].toggle(all=True)
par2.axis["bottom"].toggle(all=True)

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 3, 2], [0, 1, 2], label="Temperature")
p3, = par2.plot([50, 30, 15], [0, 1, 2], label="Velocity")

host.set_ylim(-0.25, 2)
host.set_xlim(0, 2)
par1.set_xlim(0, 4)
par2.set_xlim(1, 65)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_xlabel("Temperature")
par2.set_xlabel("Velocity")

host.legend()

host.axis["bottom"].label.set_color(p1.get_color())
par1.axis["top"].label.set_color(p2.get_color())
par2.axis["bottom"].label.set_color(p3.get_color())

plt.show()

在此处输入图像描述


推荐阅读