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

发布于 2024-10-03 01:18:35 字数 716 浏览 1 评论 0原文

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

我怀疑我拥有所有的碎片,只需要最后一条宝贵的线索......

-BobC

I have plots of 3-axis accelerometer time-series data (t,x,y,z) in separate subplots I'd like to zoom together. That is, when I use the "Zoom to Rectangle" tool on one plot, when I release the mouse all 3 plots zoom together.

Previously, I simply plotted all 3 axes on a single plot using different colors. But this is useful only with small amounts of data: I have over 2 million data points, so the last axis plotted obscures the other two. Hence the need for separate subplots.

I know I can capture matplotlib/pyplot mouse events (http://matplotlib.sourceforge.net/users/event_handling.html), and I know I can catch other events (http://matplotlib.sourceforge.net/api/backend_bases_api.html#matplotlib.backend_bases.ResizeEvent), but I don't know how to tell what zoom has been requested on any one subplot, and how to replicate it on the other two subplots.

I suspect I have the all the pieces, and need only that one last precious clue...

-BobC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心欲静而疯不止 2024-10-10 01:18:35

最简单的方法是在创建坐标区时使用 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(...)

The easiest way to do this is by using the sharex and/or sharey keywords when creating the axes:

from matplotlib import pyplot as plt

ax1 = plt.subplot(2,1,1)
ax1.plot(...)
ax2 = plt.subplot(2,1,2, sharex=ax1)
ax2.plot(...)
蓝海 2024-10-10 01:18:35

如果这是您的风格,您也可以使用 plt.subplots 来完成此操作。

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

You can also do this with plt.subplots, if that's your style.

fig, ax = plt.subplots(3, 1, sharex=True, sharey=True)
北方。的韩爷 2024-10-10 01:18:35

我在绘制图后调用以下函数将它们链接在一起。它将获取当前图形中的所有子图,并链接它们的 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)

I call the following function after making plots to get them linked together. It will get all subplots from the current figure, and link their x axes.

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)
意中人 2024-10-10 01:18:35

交互地,这适用于不同的轴

for ax in fig.axes:
    ax.set_xlim(0, 50)
fig.draw()

Interactively this works on separate axes

for ax in fig.axes:
    ax.set_xlim(0, 50)
fig.draw()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文