首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从放大的matplotlib.pyplot图形中读取新的限制?

如何从放大的matplotlib.pyplot图形中读取新的限制?
EN

Stack Overflow用户
提问于 2020-08-25 10:31:01
回答 1查看 149关注 0票数 0

我有两个问题,标记为Q1,Q2下面。Q1是主要的,Q2是次要的。我想从放大的matplotlib.pyplot图中读到新的限制。也就是说,我预计会发生以下类似的情况:

有一组数据,比如i=0...9999

  • Full set使用matplotlib.pyplot.plot命令

  • 绘制的数据集,用户查看数据并使用内置的数字窗口放大镜操作

  • 放大数据,当用户对缩放感到满意时,他/她给出了一条消息(Q1:如何?)对于“我对这些(水平的)限制很满意,阅读并保存它们”

  • ,代码读取当前的水平值(Q2: ),图形窗口能保持打开吗?如果是,那么步骤(6-7)应用

  • 如果用户想选择另一个间隔,他/她单击“重置图片”按钮,然后返回到步骤(3)

  • 当用户高兴时,他/她关闭了图

如果Q2是"no“,那么步骤(6-7)将被另一个循环所取代,其中图在(5)中关闭,然后重新绘制并从(1)开始。我可以接受,但我更喜欢"Q2=yes“版本。

基于get the key value when calling matplolib pyplot waitforbuttonpress(),我想plt.waitforbuttonpress()在某种程度上涉及到其中,但我无法破解该链接中的建议。我从来没有在python中使用过“事件”的东西。我希望绘图窗口中已经有一些内置的event,我能以某种方式访问它们吗?示例代码:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
N = 9999
dt = 0.1 / 180 * np.pi  # 0.1 degree steps
tt = np.arange(0, N*dt, dt)
yy = np.sin(tt) + 0.05 * np.random.random(tt.shape)  # include some noise
fig = plt.figure()
plt.plot(tt,yy)
plt.show()
# now the user zooms in... and questions Q1, Q2 apply.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-25 14:33:21

好的,我找到了一个解决办法,虽然我觉得这是很不美观的。我非常欢迎更好的解决办法。这个问题对Q1和Q2都有效,但有两个缺点:(1)它使用了一个全局变量,它告诉我编程(?)很糟糕,(2)它给出了一个弃用警告。代码修改来自https://matplotlib.org/3.3.1/gallery/widgets/rectangle_selector.html (h/t @tmdavison)

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt

def press_key_in_figure(event):
    global xlimits
    print("You pressed %s" % event.key)
    if event.key == 'a':
        xlimits = ax.get_xlim()  # floats
        print("new x-limits: %.2f %.2f" % xlimits)
    if event.key == 'b':
        ylimits = ax.get_ylim()  # floats
        print("new y-limits: %.2f %.2f" % ylimits)

xlimits = (0, 0)
N = 9999
dt = 0.1 / 180 * np.pi  # 0.1 degree steps
tt = np.arange(0, N*dt, dt)
yy = np.sin(tt) + 0.05 * np.random.random(tt.shape)  # include some noise
fig, ax = plt.subplots()
ax.plot(tt,yy)

fig.canvas.mpl_connect('key_press_event', press_key_in_figure)
plt.show()
# now the user zooms in... and questions Q1, Q2 apply.
print("The new x-limits after the window was closed are", xlimits)

当我运行它并放大图片时,按"a“或"b”就会发出:

代码语言:javascript
复制
C:\python\lib\tkinter\__init__.py:1705: MatplotlibDeprecationWarning: Toggling axes navigation from the keyboard is deprecated since 3.3 and will be removed two minor releases later.
  return self.func(*args)
You pressed a
new x-limits: 3.62 6.48
You pressed b
new y-limits: -0.73 -0.23

我不知道“切换轴导航”是什么意思,我在这里什么都不动?有趣的是,我的python --version是3.7.2,所以它似乎没有被移除,这与警告中的说法相反。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63576964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档