matplotlib 强制平移/缩放限制到 x 轴 [英] matplotlib forcing pan/zoom to constrain to x-axes

查看:82
本文介绍了matplotlib 强制平移/缩放限制到 x 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib 有一个功能,如果您按住x"或y"键,它会限制平移或缩放到相应的轴.

Matplotlib has a feature where if you hold down the "x" or "y" keys it constrains panning or zooming to the corresponding axis.

是否可以将其设置为默认值?由于某种原因,当按住字母键时,CPU不允许触摸板移动.而且我只希望平移/缩放 x 轴,而不是 y 轴.

Is there a way to cause this to be the default? For some reason my CPU does not allow touchpad movement when a letter key is held down. And I only want the x-axis to be pan/zoomed, not the y-axis.

https中找到了平移/缩放功能://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes.py#L3001

其中包含一个内部函数 format_deltas.但是我不知道当从Figure对象自动创建Axes对象时,如何用子类覆盖Axes类.

which contains an internal function format_deltas. But I have no idea how to override the Axes class with a subclass when Axes objects are created automatically from Figure objects.

推荐答案

可以使用自己的axes类.在您的情况下,您可以从 matplotlib.axes.Axes 继承并更改 drag_pan 方法以始终像按"x"键一样起作用.但是,缩放似乎并未在该类中定义.以下内容仅允许x轴平移:

It is possible to use your own axes class. In your case you can inherit from matplotlib.axes.Axes and change the drag_pan method to always act as though the 'x' key is being pressed. However the zooming doesn't seem to be defined in that class. The following will only allow x axis panning:

import matplotlib
import matplotlib.pyplot as plt    

class My_Axes(matplotlib.axes.Axes):
    name = "My_Axes"
    def drag_pan(self, button, key, x, y):
        matplotlib.axes.Axes.drag_pan(self, button, 'x', x, y) # pretend key=='x'

matplotlib.projections.register_projection(My_Axes)

figure = plt.figure()
ax = figure.add_subplot(111, projection="My_Axes")
ax.plot([0, 1, 2], [0, 1, 0])
plt.show()

对于缩放,您可能需要查看工具栏控件本身. NavigationToolbar2 类具有 drag_zoom 方法似乎与此处相关,但由于不同的后端都有自己的版本(例如 NavigationToolbar2TkAgg

For the zooming, you may have to look at the toolbar control itself. The NavigationToolbar2 class has the drag_zoom method which seems to be what's relevant here, but tracking down how that works is quickly complicated by the fact that the different backends all have their own versions (e.g. NavigationToolbar2TkAgg

修改

可以在以下内容中修改所需的行为:

You can monkeypatch the desired behaviour in:

import types
def press_zoom(self, event):
    event.key='x'
    matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg.press_zoom(self,event)
figure.canvas.toolbar.press_zoom=types.MethodType(press_zoom, figure.canvas.toolbar)

您可以正确地执行此操作并创建工具栏的子类,但是您必须随后创建 Figure、FigureCanvas 和您的 NavigationToolbar 的实例,并将它们放入 Tk 应用程序或其他东西中.我认为没有一种非常简单的方法可以通过简单的绘图界面使用自己的工具栏.

You could do it properly and make a subclass of the toolbar, but you have to then create instances of Figure, FigureCanvas and your NavigationToolbar and put them in a Tk app or something. I don't think there's a really straightforward way to just use your own toolbar with the simple plotting interface.

这篇关于matplotlib 强制平移/缩放限制到 x 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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