我有两个问题,标记为Q1,Q2下面。Q1是主要的,Q2是次要的。我想从放大的matplotlib.pyplot图中读到新的限制。也就是说,我预计会发生以下类似的情况:
有一组数据,比如i=0...9999
matplotlib.pyplot.plot命令
。
如果Q2是"no“,那么步骤(6-7)将被另一个循环所取代,其中图在(5)中关闭,然后重新绘制并从(1)开始。我可以接受,但我更喜欢"Q2=yes“版本。
基于get the key value when calling matplolib pyplot waitforbuttonpress(),我想plt.waitforbuttonpress()在某种程度上涉及到其中,但我无法破解该链接中的建议。我从来没有在python中使用过“事件”的东西。我希望绘图窗口中已经有一些内置的event,我能以某种方式访问它们吗?示例代码:
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.发布于 2020-08-25 14:33:21
好的,我找到了一个解决办法,虽然我觉得这是很不美观的。我非常欢迎更好的解决办法。这个问题对Q1和Q2都有效,但有两个缺点:(1)它使用了一个全局变量,它告诉我编程(?)很糟糕,(2)它给出了一个弃用警告。代码修改来自https://matplotlib.org/3.3.1/gallery/widgets/rectangle_selector.html (h/t @tmdavison)
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”就会发出:
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,所以它似乎没有被移除,这与警告中的说法相反。
https://stackoverflow.com/questions/63576964
复制相似问题