Matplotlib/Pyplot:如何将子图一起缩放?

问题描述 投票:0回答:4

我在单独的子图中有 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),但我不知道如何判断任何一个子图上请求了什么缩放,以及如何在其他两个子图上复制它。

zooming matplotlib
4个回答
151
投票

最简单的方法是在创建轴时使用

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(...)

70
投票

如果您喜欢的话,您也可以使用

plt.subplots
来完成此操作。

fig, ax = plt.subplots(3, 1, sharex=True, sharey=True)

-1
投票

我在绘制图后调用以下函数将它们链接在一起。它将获取当前图形中的所有子图,并链接它们的 x 轴。

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)

-2
投票

交互式地在不同的轴上工作

for ax in fig.axes:
    ax.set_xlim(0, 50)
fig.draw()
© www.soinside.com 2019 - 2024. All rights reserved.