python - Matplotlib - Possible to make sub plots with parasite axes?
问题描述
I'm trying to make a chart with two subplots, each of which has a parasite axis, as shown in the documentation here. However, although I can replicate the example with a single plot, it doesn't seem to work with 2 subplots. Is Matplotlib even capable of doing this?
Here is my code:
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()
When I run that I just end up with a blank set of subplots:
Sorry if I'm doing something dumb!
Thanks a lot, Alex
解决方案
你在里面犯了一些小错误。首先是plt.subplot()
覆盖命令的host_subplot()
命令(请参阅matplotlib.pyplot.subplot() 文档中的注释:“创建子图将删除与它重叠的任何预先存在的子图,超出共享边界”)。此外,您必须分别跟踪两个图的实例。我像这样解决了它,我为第一个和,和第二个创建了host1
,par11
和。整个代码现在看起来像这样:par12
Axes
host2
par21
par22
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()
结果如下:
希望这可以帮助。
推荐阅读
- python - 如何合并两个字符串?- Python
- azure - 什么是 Azure 云服务?
- javascript - 必须指定一个“url”属性或函数backbone.js 保存
- java - 为什么我在空对象引用上得到“void android.view.inputmethod.InputConnection.closeConnection()”?
- c++ - isdigit() 和 isalnum() 给出错误,因为输入是 const char 并且无法转换。其他可能查看输入是否为数字的方法?
- c# - 如何在我的 ASP.NET MVC 应用程序中异步查询 ParseObject 集合?
- ios - 如何使用协议更新 UITableCell 图像
- javascript - angular 2 路由,useHash剥离路由参数
- ssl - 对 https 的“中间人”攻击可以读取所有通信吗?
- html - 删除较小视口上的 CSS 网格值