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

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

问题描述

您可以肯定地知道一种快速方法,在放大后如何跟踪数字的极限?我想精确地知道坐标,以便可以用ax.set_xlimax.set_ylim再现图形. 我正在使用标准的qt4agg后端.

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具有事件处理API,您可以使用该API挂接所指的操作. 事件处理页概述了事件API,并且(非常)简短地提到了轴" 页上的x和y限制事件.

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.

Axes实例通过作为CallbackRegistry实例的callbacks属性支持回调.您可以连接的事件是xlim_changedylim_changed,回调将使用func(ax)调用,其中axAxes实例.

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.

在您的方案中,您想在Axes对象的xlim_changedylim_changed事件上注册回调函数.每当用户缩放或移动视口时,就会调用这些功能.

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天全站免登陆