Matplotlib:缩放后找出 xlim 和 ylim [英] Matplotlib: Finding out xlim and ylim after zoom

查看:46
本文介绍了Matplotlib:缩放后找出 xlim 和 ylim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你肯定知道在放大后如何快速追踪我的身材极限的方法吗?我想准确地知道坐标,以便我可以用 ax.set_xlimax.set_ylim 重现该图.我正在使用标准的 qt4agg 后端.

我知道我可以使用光标来找出下角和上角的两个位置,但也许有正式的方法可以做到这一点?

解决方案

matplotlib 有一个事件处理 API,您可以使用它来连接到您所指的操作之类的操作.事件处理页面概述了事件 API,并且(非常)简要地提到了 页面上的 x 和 y 限制事件.><块引用>

Axes 实例通过回调属性支持回调,该属性是一个 CallbackRegistry 实例.您可以连接的事件是 xlim_changedylim_changed 并且回调将使用 func(ax) 调用,其中 ax> 是 Axes 实例.

在您的场景中,您希望在 Axes 对象的 xlim_changedylim_changed 事件上注册回调函数.每当用户缩放或移动视口时,就会调用这些函数.

这是一个最低限度的工作示例:

Python 2

将 matplotlib.pyplot 导入为 plt## 一些玩具数据x_seq = [x/100.0 for x in xrange(1, 100)]y_seq = [x**2 for x in x_seq]## 散点图图, ax = plt.subplots(1, 1)ax.scatter(x_seq, y_seq)## 声明和注册回调def on_xlims_change(event_ax):打印更新的 xlims:", event_ax.get_xlim()def on_ylims_change(event_ax):打印更新的 ylims:",event_ax.get_ylim()ax.callbacks.connect('xlim_changed', on_xlims_change)ax.callbacks.connect('ylim_changed', on_ylims_change)## 展示plt.show()

<小时>

Python 3

将 matplotlib.pyplot 导入为 plt## 一些玩具数据x_seq = [x/100.0 for x in range(1, 100)]y_seq = [x**2 for x in x_seq]## 散点图图, ax = plt.subplots(1, 1)ax.scatter(x_seq, y_seq)## 声明和注册回调def on_xlims_change(event_ax):打印(更新xlims:",event_ax.get_xlim())def on_ylims_change(event_ax):打印(更新ylims:",event_ax.get_ylim())ax.callbacks.connect('xlim_changed', on_xlims_change)ax.callbacks.connect('ylim_changed', on_ylims_change)## 展示plt.show()

you for sure know a fast way how I can track down the limits of my figure after having zoomed in? I would like to know the coordinates precisely so I can reproduce the figure with ax.set_xlim and ax.set_ylim. I am using the standard qt4agg backend.

edit: I know I can use the cursor to find out the two positions in the lower and upper corner, but maybe there is formal way to do that?

解决方案

matplotlib has an event handling API you can use to hook in to actions like the ones you're referring to. The Event Handling page gives an overview of the events API, and there's a (very) brief mention of the x- and y- limits events on the Axes page.

The Axes instance supports callbacks through a callbacks attribute which is a CallbackRegistry instance. The events you can connect to are xlim_changed and ylim_changed and the callback will be called with func(ax) where ax is the Axes instance.

In your scenario, you'd want to register callback functions on the Axes object's xlim_changed and ylim_changed events. These functions will get called whenever the user zooms or shifts the viewport.

Here's a minimum working example:

Python 2

import matplotlib.pyplot as plt

#
# Some toy data
x_seq = [x / 100.0 for x in xrange(1, 100)]
y_seq = [x**2 for x in x_seq]

#
# Scatter plot
fig, ax = plt.subplots(1, 1)
ax.scatter(x_seq, y_seq)

#
# Declare and register callbacks
def on_xlims_change(event_ax):
    print "updated xlims: ", event_ax.get_xlim()

def on_ylims_change(event_ax):
    print "updated ylims: ", event_ax.get_ylim()

ax.callbacks.connect('xlim_changed', on_xlims_change)
ax.callbacks.connect('ylim_changed', on_ylims_change)

#
# Show
plt.show()


Python 3

import matplotlib.pyplot as plt

#
# Some toy data
x_seq = [x / 100.0 for x in range(1, 100)]
y_seq = [x**2 for x in x_seq]

#
# Scatter plot
fig, ax = plt.subplots(1, 1)
ax.scatter(x_seq, y_seq)

#
# Declare and register callbacks
def on_xlims_change(event_ax):
    print("updated xlims: ", event_ax.get_xlim())

def on_ylims_change(event_ax):
    print("updated ylims: ", event_ax.get_ylim())

ax.callbacks.connect('xlim_changed', on_xlims_change)
ax.callbacks.connect('ylim_changed', on_ylims_change)

#
# Show
plt.show()

这篇关于Matplotlib:缩放后找出 xlim 和 ylim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆