如何使matplotlib打开一个供用户评论的框? [英] How do I make matplotlib open a box for user comments?
问题描述
我有一个交互式绘图,可以侦听某些按键和单击,但是我希望用户能够添加评论.我知道艺术家活动通常不允许这样做(他们正在听个人新闻!但是我可以让matplotlib打开一个新的窗口,该窗口有一个小的插入评论"区域吗?理想情况下,该窗口退出并返回主窗口(原始),当用户点击return时.
I have an interactive plot that listens for certain keypresses and clicks but I want the user to be able to add a comment. I know that artist events don't typically allow this (they're listening for individual presses! but can I have matplotlib open a new window that has a small "insert comment" area? Ideally the window exits and returns to the main (original) window when the user hits return.
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()
推荐答案
Matplotlib 将很快现在引入了TextBox Widget.在此示例中查看其用法.
Matplotlib will soon has now introduced a TextBox Widget. See it's usage in this example.
或者,您可以使用Tkinter的tkSimpleDialog
向用户提出评论.
Alternatively, you can use Tkinter's tkSimpleDialog
to ask the user for a comment.
w = tkSimpleDialog.askstring("Title", "Please type comment")
然后您可以在注释中注释最后选择的点.
You may then annotate the last picked point with the comment.
完整示例(在python 2.7中运行):
Complete example (which runs in 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()
这篇关于如何使matplotlib打开一个供用户评论的框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!