【问题标题】:How do I make matplotlib open a box for user comments?如何让 matplotlib 打开一个用户评论框?
【发布时间】:2017-10-13 21:36:14
【问题描述】:

我有一个交互式绘图,可以监听某些按键和点击,但我希望用户能够添加评论。我知道艺术家事件通常不允许这样做(他们正在侦听个别新闻!但我可以让 matplotlib 打开一个新窗口,其中有一个小的“插入评论”区域吗?理想情况下,窗口退出并返回到主窗口(原始) 用户点击返回时的窗口。

import numpy as np
import matplotlib.pyplot as plt

def onpick(event):
    ''' '''
    if event.mouseevent.button == 1: #only want lmb clicks
        selection = event.artist
        xdata = selection.get_xdata()
        ydata = selection.get_ydata()
        ind = event.ind
        point = tuple(zip(xdata[ind], ydata[ind]))
        xclick,yclick = point[0] 

        print('[x,y]=',xclick,yclick)   

def on_key(event):
    ''' 
        Handles predefined key-press events 
    ''' 
    print('Key press:\'%s\'' %(event.key))
    if event.key == ' ': #spacebar
        print 'Space'
        #do a thing
    if event.key == 'e': 
        print 'eeeeee'
        #do another thing
    if event.key == 'C':
        print 'How do make a comment. ...'
        comment = 'Whatever the user entered'
        return comment

        # when done return

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, facecolor='#FFFFCC')

x, y = 4*(np.random.rand(2, 100) - .5)
ax.plot(x, y, 'o', picker = 6)
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

keyID = fig.canvas.mpl_connect('key_press_event', on_key)
clickID = fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

【问题讨论】:

标签: python python-2.7 matplotlib


【解决方案1】:

Matplotlib will soon 现在引入了一个 TextBox Widget。在this example 中查看它的用法。

或者,您可以使用 Tkinter 的 tkSimpleDialog 请求用户发表评论。

w = tkSimpleDialog.askstring("Title", "Please type comment")

然后,您可以在最后选择的点上添加注释。

完整示例(在 python 2.7 中运行):

import numpy as np
import matplotlib.pyplot as plt
import Tkinter, tkSimpleDialog

xy = [(0,0)]

def onpick(event):
    ''' '''
    if event.mouseevent.button == 1: #only want lmb clicks
        selection = event.artist
        xdata = selection.get_xdata()
        ydata = selection.get_ydata()
        ind = event.ind
        point = tuple(zip(xdata[ind], ydata[ind]))
        xclick,yclick = point[0] 
        xy[0] = (xclick,yclick)
        print('[x,y]=',xclick,yclick)   

def on_key(event):
    print('Key press:\'%s\'' %(event.key))
    if event.key == 'c':
        root = Tkinter.Tk()
        root.withdraw()
        w = tkSimpleDialog.askstring("Title", "Please type comment")
        if w != None:
            ax.annotate(w, xy=xy[0], xytext=(20,-20), 
                    arrowprops=dict(facecolor='black', width=2, headwidth=6),
                    textcoords='offset points')
            ax.figure.canvas.draw_idle()

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, facecolor='#FFFFCC')

x, y = 4*(np.random.rand(2, 100) - .5)
ax.plot(x, y, 'o', picker = 6)
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

keyID = fig.canvas.mpl_connect('key_press_event', on_key)
clickID = fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

【讨论】:

  • 谢谢,这正是我所想象的!
  • 嗯,似乎遇到了一些奇怪的错误Python[50938:397346] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f836c97e400 ...互联网似乎认为 Tkinter 已过时,但我会使用您的解决方案并对其进行修改以与@一起使用987654323@
  • Tkinter 并没有过时。它是 python 2.x 的标准 GUI 界面。在 python 3.x 中,您需要 tkinter。当然还有可用的替代品,如 PyQt、GTK、wx 等,但 T(t)kinter 的优势在于它已经与 python 一起提供。关于错误,需要有完整的问题描述来帮助您。
  • 你说得对,看起来 pyqt 和 gtk 并不理想,因为我会让同事使用这个软件,他们可能不想搞砸安装。那么我是否应该针对该错误提出一个新问题,或者您是否乐于对this mess 提供一些见解?那是从文件 (jnk.py) 运行您上面给出的确切代码。
  • 所以上述问题是通过使用 TKagg 后端而不是 osx 来修复的(非常典型)。但是,脚本现在运行,但是当您的注释工作时,它不会将键盘输入返回给事件侦听器。也就是只能做一个注解,其他的'key_press_event's都不能注册。
猜你喜欢
  • 1970-01-01
  • 2012-05-11
  • 2018-03-08
  • 1970-01-01
  • 2021-07-12
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多