我在单独的子图中有 3 轴加速度计时间序列数据 (t、x、y、z) 的图,我想一起放大。也就是说,当我在一个图上使用“缩放到矩形”工具时,当我释放鼠标时,所有 3 个图一起缩放。
之前,我只是使用不同的颜色将所有 3 个轴绘制在一个图上。但这仅对少量数据有用:我有超过 200 万个数据点,因此绘制的最后一个轴遮住了其他两个轴。因此需要单独的子图。
我知道我可以捕获 matplotlib/pyplot 鼠标事件 (http://matplotlib.sourceforge.net/users/event_handling.html),我知道我可以捕获其他事件 (http://matplotlib.sourceforge.net/api/ backend_bases_api.html#matplotlib.backend_bases.ResizeEvent),但我不知道如何判断在任何一个子图中请求了什么样的缩放,以及如何在其他两个子图中复制它。
我怀疑我拥有所有的碎片,只需要最后一条宝贵的线索......
-鲍勃C
最简单的方法是在创建轴时使用
sharex
和/或 sharey
关键字:
from matplotlib import pyplot as plt
ax1 = plt.subplot(2,1,1)
ax1.plot(...)
ax2 = plt.subplot(2,1,2, sharex=ax1)
ax2.plot(...)
如果这是你的风格,你也可以使用
plt.subplots
来做到这一点。
fig, ax = plt.subplots(3, 1, sharex=True, sharey=True)
import matplotlib.pyplot as plt
def linkx():
# Get current figure
axes = plt.gcf().axes
parent = axes[0]
# Loop over other axes and link to first axes
for i in range(1,len(axes)):
axes[i].sharex(parent)
交互地,这在不同的轴上工作
for ax in fig.axes:
ax.set_xlim(0, 50)
fig.draw()