我正在尝试修改 matplotlib 中 mouseevent 定义中的绘图。我的最终终点是能够让用户拖动标记开始和结束 x 坐标的灰色区域,从中我可以确定局部最大值/最小值。 这就是我现在所在的地方。虽然第一个 avxspan 出现在图中,但第二个没有出现。我知道 mouseevent 被触发,因为 x,y 坐标打印在控制台上。有人能告诉我为什么这行不通吗?非常感谢。
fig=plt.figure()
plt.figure(fig.number)
plt.plot(data)
ax=plt.gca()
plt.figtext(0.25,0.92, "Calculate")
plt.figtext(0.66,0.92, "Clear")
ax.axvspan(1, 100, facecolor='0.5', alpha=0.5)
def start(event):
global ax
ax.axvspan(100, 200, facecolor='0.5', alpha=0.5)
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
cid = fig.canvas.mpl_connect('button_press_event', start)
plt.show()
fig.canvas.mpl_disconnect(cid)
最佳答案
您可能想看一下 matplotlib.widgets
模块。
一个简单的一行,使您能够拖动一个区域,并定义一个回调函数来获取跨越该区域的坐标。
简单的例子
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import SpanSelector
x = np.arange(10)
y = x**2
plt.plot(x,y)
ax=plt.gca()
def onselect(vmin, vmax):
print vmin, vmax
span = SpanSelector(ax, onselect, 'horizontal')
plt.show()
关于python - 访问 def 中的 matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21685390/