matplotlib onclick事件重复 [英] matplotlib onclick event repeating

查看:69
本文介绍了matplotlib onclick事件重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 onclick 方法在matplotlib图上选择一系列数据.但是问题是,我不能不止一次这样做并更新情节.我有一些想法可以做到这一点,其中之一就是在添加新图片后列出要跳转到新索引的绘图列表...但是大多数情况下,我希望能够存储来自单击的信息( event.xdata )两次为该部分中图形下方的区域着色-但对于初学者而言,无论我单击何处都可以画点.但是我认为有比将 plt.draw()放在 onclick 函数中更好的解决方案吗?

I want to use the onclick method to select a range of data on my matplotlib graph. But the thing is, I can't do that more than once and update the plot. I have some ideas to do this, one of them would be to make a list of plots where I jump to new indices after I appended the new picture ... but mostly I would like to be able to store the information from the click (event.xdata) two times to color the area under the graph in that section - but for starters it would already be an achievement to draw points wherever I click. But I kind of think there is a better solution than putting a plt.draw() in the onclick function?

import numpy as np
import matplotlib.pyplot as plt
from itertools import islice

class ReadFile():
    def __init__(self, filename):
        self._filename = filename

    def read_switching(self):
        return np.genfromtxt(self._filename, unpack=True, usecols={0}, delimiter=',')

def onclick(event):
    global ix, iy
    ix, iy = event.xdata, event.ydata
    global coords
    coords.append((ix, iy))
    print(coords)
    fig.canvas.mpl_disconnect(cid)
    return coords

coords = []    
filename = 'test.csv'
fig = plt.figure()
ax = fig.add_subplot(111)
values = (ReadFile(filename).read_switching())
steps = np.arange(1, len(values)+1)*2
graph_1, = ax.plot(steps, values, label='original curve')
cid = fig.canvas.mpl_connect('button_press_event', onclick)
print(coords)
graph_2, = ax.plot(coords, marker='o')
plt.show()

例如,我具有以下功能(图片),并且我想单击两个坐标并为图形下方的区域上色,可能使用 plt.draw().

For example I have the following function (picture) and I want to click on two coordinates and color the area under the graph, probably with a plt.draw().

推荐答案

问题是您要断开回调中的 on_click 事件.相反,您需要更新 graph_2 对象的 xdata ydata .然后强制重新绘制图形

The issue is that you're disconnecting the on_click event inside of your callback. Instead, you'll want to update the xdata and ydata of your graph_2 object. Then force the figure to be redrawn

import numpy as np
import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.add_subplot(111)

# Plot some random data
values = np.random.rand(4,1);
graph_1, = ax.plot(values, label='original curve')
graph_2, = ax.plot([], marker='o')

# Keep track of x/y coordinates
xcoords = []
ycoords = []

def onclick(event):
    xcoords.append(event.xdata)
    ycoords.append(event.ydata)

    # Update plotted coordinates
    graph_2.set_xdata(xcoords)
    graph_2.set_ydata(ycoords)

    # Refresh the plot
    fig.canvas.draw()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

这篇关于matplotlib onclick事件重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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