Форум сайта python.su
Как интерфейса Tk(или аналогичного) вызвать построение графика matplotlib библиотеки. Есть несколько полей ввода(для ввода цифр) и есть кнопка, например построить. как вызвать с её помощью фунцию из другой библиотеки?
Ранее обсуждалось тут http://python.su/forum/topic/20240/
Офлайн
Ну вот. Списбо, что перенесли.
Держите…
import Tkinter import Image import ImageTk import sys import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.path as path def die(event = None): root.destroy() sys.exit(0) def plot_in_root(event = None): global Img_x data = int(entry_data.get()) step = int(entry_step.get()) plot_figure(data, step) Img = Image.open("aa.png").resize((400,300)) Img_x = ImageTk.PhotoImage(Img) cnv.create_image(200, 150, image=Img_x, anchor = "center") root = Tkinter.Tk() root.geometry("600x600+100+100") root.protocol("WM_DELETE_WINDOW", die) lfr_data = Tkinter.LabelFrame(root) lfr_data["text"] = "Введите количесво исходных данных" lfr_data.place(relx = 0.25, rely = 0.2, anchor = "center") entry_data = Tkinter.Entry(lfr_data) entry_data["width"] = 38 entry_data.pack() entry_data.focus_force() lfr_step = Tkinter.LabelFrame(root) lfr_step["text"] = "Введите количесво диапазонов данных" lfr_step.place(relx = 0.75, rely = 0.2, anchor = "center") entry_step = Tkinter.Entry(lfr_step) entry_step["width"] = 38 entry_step.pack() bt = Tkinter.Button(root) bt["text"] = u"Построить" bt.bind("<Button-1>", plot_in_root) bt.place(relx = 0.5, rely = 0.3, anchor = "center") bt_ex = Tkinter.Button(root) bt_ex["text"] = u"Выход" bt_ex.bind("<Button-1>", die) bt_ex.place(relx = 0.5, rely = 0.9, anchor = "center") cnv = Tkinter.Canvas(root) cnv["width"] = 400 cnv["height"] = 300 cnv["bg"] ="grey" cnv.place(relx = 0.5, rely = 0.6, anchor = "center") def plot_figure(*argv): fig = plt.figure() ax = fig.add_subplot(111) # histogram our data with numpy data = np.random.randn(argv[0]) n, bins = np.histogram(data, argv[1]) # get the corners of the rectangles for the histogram left = np.array(bins[:-1]) right = np.array(bins[1:]) bottom = np.zeros(len(left)) top = bottom + n nrects = len(left) # here comes the tricky part -- we have to set up the vertex and path # codes arrays using moveto, lineto and closepoly # for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the # CLOSEPOLY; the vert for the closepoly is ignored but we still need # it to keep the codes aligned with the vertices nverts = nrects*(1+3+1) verts = np.zeros((nverts, 2)) codes = np.ones(nverts, int) * path.Path.LINETO codes[0::5] = path.Path.MOVETO codes[4::5] = path.Path.CLOSEPOLY verts[0::5,0] = left verts[0::5,1] = bottom verts[1::5,0] = left verts[1::5,1] = top verts[2::5,0] = right verts[2::5,1] = top verts[3::5,0] = right verts[3::5,1] = bottom barpath = path.Path(verts, codes) patch = patches.PathPatch(barpath, facecolor='red', edgecolor='yellow', alpha=0.5) ax.add_patch(patch) ax.set_xlim(left[0], right[-1]) ax.set_ylim(bottom.min(), top.max()) plt.savefig("aa.png") root.mainloop()
Отредактировано 4kpt (Дек. 12, 2012 23:53:49)
Офлайн
Спасибо, очень полезный для изучения пример, буду с ним работать.
Офлайн