我正在开发一个应用程序,该应用程序以.csv
形式从用户处获取输入,并使用matplotlib
绘制相应值的图形。
def plotgraph():
x = []
y = []
data = text.get("1.0", END)
sepFile = data.split('\n')
for plotPair in sepFile:
xAndY = plotPair.split(',')
if len(xAndY[0]) != 0 and len(xAndY[1]) != 0:
x.append(float(xAndY[0]))
y.append(float(xAndY[1]))
graph = Figure(figsize=(5,4), dpi=100)
a = graph.add_subplot(111)
a.plot(x,y)
a.set_xlabel('Velocity')
a.set_ylabel('Absorbance')
canvas = FigureCanvasTkAgg(graph, master=RightFrame)
canvas.show()
canvas.get_tk_widget().grid(column=2, row=1, rowspan=2, sticky=(N, S, E, W))
我希望在Tkinter
中使用与此Matplotlib: draw a selection area in the shape of a rectangle with the mouse类似的功能,在选择后会给我x0, x1, y0, y1
。我可以把已经问过的问题做成工作&根据我的需要定制它,但不知道我在__init__(self)
root = Tk()
class Annotate(object):
def __init__(self):
self.fig = mplfig.Figure(figsize=(1.5, 1.5))
self.ax = self.fig.add_subplot(111)
self.ax.plot([0,1,2,3,4],[0,8,9,5,3])
self.canvas = tkagg.FigureCanvasTkAgg(self.fig, master=root)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
当我运行此代码时,我得到一个空白的Tk窗口。任何人都可以告诉我该怎么办&我做错了什么
答案 0 :(得分:1)
要使用列表中需要的类
class Annotate(object):
def __init__(self):
print "Annotate is runing"
# rest of your code
root = Tk()
my_object = Annotate()
root.mainloop()
可能你需要做更多的工作。