两个下X轴matplotlib

时间:2014-10-02 16:16:58

标签: matplotlib axis

我想知道是否有可能在matplotlib中有两个不同的X轴,但不是因为它们位于图形的两侧。相反,它们可以将它们放在一起吗?

1 个答案:

答案 0 :(得分:2)

这是你要找的吗?

Two x-axes on the bottom.

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


host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(bottom=0.2)

par2 = host.twiny()

offset = -40
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["bottom"] = new_fixed_axis(loc="bottom",
                                     axes=par2,
                                     offset=(0, offset))

par2.axis["top"].toggle(all=False)

host.set_xlim(0, 2)
host.set_ylim(0, 2)

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

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

par2.set_xlim(1, 65)

host.legend()

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