Matplotlib可以用寄生轴生成子图吗?

2024-04-25 04:46:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个包含两个子图的图表,每个子图都有一个寄生轴,如文档here所示。不过,用一个小例子来说,我不能复制很多。Matplotlib能做到这一点吗?在

这是我的代码:

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

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

plt.subplot(2,1,1)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))

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

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

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

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

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()

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

#####2#####
plt.subplot(2,1,2)
par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))

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

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

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

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

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()

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


plt.draw()
plt.show()

当我运行时,我只得到一组空白的子图:

Output

对不起,如果我在做傻事!在

非常感谢, 亚历克斯


Tags: righthostnewgetpltlabeloffsetcolor
1条回答
网友
1楼 · 发布于 2024-04-25 04:46:33

你有一些小错误。首先是覆盖plt.subplot()命令的host_subplot()命令(请参阅note in the matplotlib.pyplot.subplot() documentation:“创建子批次将删除与之重叠的任何预先存在的子批次,而不是共享边界”)。此外,还必须分别跟踪这两个图的实例。我这样解决它,我为第一个Axespar11,和{},为第一个Axespar21,为第二个Axes创建{}。现在整个代码如下所示:

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

fig = plt.figure()

host1 = host_subplot(211, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par11 = host1.twinx()
par12 = host1.twinx()

offset = 60
new_fixed_axis = par12.get_grid_helper().new_fixed_axis
par12.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par12,
                                    offset=(offset, 0))

par11.axis["right"].toggle(all=True)
par12.axis["right"].toggle(all=True)

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

host1.set_xlabel("Distance")
host1.set_ylabel("Density")
par11.set_ylabel("Temperature")
par12.set_ylabel("Velocity")

p1, = host1.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par11.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par12.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par11.set_ylim(0, 4)
par12.set_ylim(1, 65)

host1.legend()

host1.axis["left"].label.set_color(p1.get_color())
par11.axis["right"].label.set_color(p2.get_color())
par12.axis["right"].label.set_color(p3.get_color())

#####2#####
host2 = host_subplot(212, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)
par21 = host2.twinx()
par22 = host2.twinx()

offset = 60
new_fixed_axis = par22.get_grid_helper().new_fixed_axis
par22.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par22,
                                    offset=(offset, 0))

par21.axis["right"].toggle(all=True)
par22.axis["right"].toggle(all=True)

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

host2.set_xlabel("Distance")
host2.set_ylabel("Density")
par21.set_ylabel("Temperature")
par22.set_ylabel("Velocity")

p1, = host2.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par21.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par22.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par21.set_ylim(0, 4)
par22.set_ylim(1, 65)

host2.legend()

host2.axis["left"].label.set_color(p1.get_color())
par21.axis["right"].label.set_color(p2.get_color())
par22.axis["right"].label.set_color(p3.get_color())    

fig.tight_layout()

plt.draw()
plt.show()

结果如下:

result of the above code

希望这有帮助。在

相关问题 更多 >