缩放后根据Matplotlib中的当前ylim和xlim重新计算x y值 [英] Recalculate x y values after zoom based on current ylim and xlim in Matplotlib

查看:197
本文介绍了缩放后根据Matplotlib中的当前ylim和xlim重新计算x y值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,我要以原点始终为(0,0)且显然值在上的相对距离重新缩放后,重新计算写在图形的刻度标签上的xy值x和y轴保持不变.

Dear all i want to recalculate the x y values written in the tick labeling of my figure after i have zoomed in it in such a way that the origin is always at (0,0) and obviously the relative distances of the values on the x and y axis stay the same.

我认为放大后需要跟踪图形的极限,而不是简单地从实际的x y刻度值中减去当前的xmin和ymin. 我想这可以通过事件处理API来实现 事件处理 正如我在这里学到的: Source1

I think i need to track the limits of my figure after having zoomed in to it and than simply subtract the current xmin and ymin from the actual x y tick values. I guess this can be achived with the event handling API Event handling as i have learned here : Source1

这也是我开始MWE的地方:

this is also the place were i got the start of my MWE:

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(axes):
    a=axes.get_xlim()
    print "updated xlims: ", axes.get_xlim()
    return a

def on_ylims_change(axes):
    a=axes.get_ylim()
    print "updated ylims: ", axes.get_ylim()
    return a

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



#
# Show
plt.show()

但是我真的不知道该怎么走?我是否必须在on_xlims_change函数中进行计算并在那里更改x和y刻度标签?再次,我认为我真的只需要更改标签中给定的值,对吗?还是更容易更改坐标的实际值以使自动刻度标记仍然有效?

But i do not really know how i should go from here? Do i have to do the calculation inside the on_xlims_change function and change the x and y tick labels there? Again, i think i really only need to change the value given in the label, right? or would it be easier to change the actual value of the coordinates such that the automatic tick labeling still works?

感谢我的前进

推荐答案

这可能听起来并不容易.更改限制时,您将更改限制,以使回调无限期运行,从而使窗口崩溃.

This may not be as easy as it sounds. When changing the limits, you would change the limits, such that the callback runs infinitly, making your window crash.

因此,我将使用第二个轴来选择另一种解决方案.假设您有两个轴:

I would hence opt for another solution, using a second axes. So let's say you have two axes:

  • ax2是要绘制到的轴.但是没有框架,也没有刻度标签.这是您可以用来更改限制的轴.
  • ax为空.它最初具有与ax2相同的限制.它将显示ticklabels.
  • ax2 is the axes to plot to. But is has no frame and no ticklabels. This is the axes you can change the limits with.
  • ax is empty. It initially has the same limits as ax2. And it will show the ticklabels.

放大ax2后,回调函数可以根据自己的喜好更改ax的限制.这就是屏​​幕上显示的内容.

Once you zoom in on ax2 the callback function can change the limits of ax to your liking. This is then what is shown on the screen.

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]

# ax is empty
fig, ax = plt.subplots()
ax.set_navigate(False)
# ax2 will hold the plot, but has invisible labels
ax2 = fig.add_subplot(111,zorder=2)
ax2.scatter(x_seq, y_seq)
ax2.axis("off")

ax.set_xlim(ax2.get_xlim())
ax.set_ylim(ax2.get_ylim())

#
# Declare and register callbacks
def on_lims_change(axes):
    # change limits of ax, when ax2 limits are changed.
    a=ax2.get_xlim()
    ax.set_xlim(0, a[1]-a[0])
    a=ax2.get_ylim()
    ax.set_ylim(0, a[1]-a[0])

ax2.callbacks.connect('xlim_changed', on_lims_change)
ax2.callbacks.connect('ylim_changed', on_lims_change)

# Show
plt.show()

这篇关于缩放后根据Matplotlib中的当前ylim和xlim重新计算x y值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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