使用Tkinter Spinbox中的值单击按钮时使用pyplot绘制线 [英] Plotting lines with pyplot when button clicked with values from tkinter spinbox

查看:75
本文介绍了使用Tkinter Spinbox中的值单击按钮时使用pyplot绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是后续问题的后续问题.我下面有一个代码:

This is a follow up question to the one asked before. I have a code below:

from tkinter import *

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

class PlotClass():
    def __init__(self):
         fig = Figure(figsize=(5,5),dpi=70,facecolor='cyan')
         ax = fig.subplots()
         ax.set_title("Ttile")
         ax.set_ylabel("y")
         ax.set_xlabel("x")
         ax.set_xlim(100,9000)
         ax.set_ylim(130,-10)
         ax.set_facecolor("cyan")

         x = [125,250,500,1000,2000,4000,8000]
         ticks = [125,250,500,"1K","2K","4K","8K"]
         xm = [750,1500,3000,6000]

         ax.set_xscale('log', basex=2)
         ax.set_xticks(x)
         ax.set_xticks(xm, minor=True)
         ax.set_xticklabels(ticks)
         ax.set_xticklabels([""]*len(xm), minor=True)

         ax.yaxis.set_ticks([120,110,100,90,80,70,60,50,40,30,20,10,0,-10])

         self.line, = ax.plot([],[],'r+',markersize=15.0,mew=2)
         self.line2,= ax.plot([],[],'-o',markersize=15.0,mew=2)
         ax.grid(color="grey")
         ax.grid(axis="x", which='minor',color="grey", linestyle="--")
         self.canvas = canvas = FigureCanvasTkAgg(fig, master=master)
         canvas.show()
         canvas.get_tk_widget().grid(column=0,row=2,columnspan=3,rowspan=15)
         self.spin = Spinbox(master, from_=125,to=8000,command=self.action)
         self.spin.grid(column=5,row=2)

         self.spin2 = Spinbox(master, from_=-10,to=125,command=self.action)
         self.spin2.grid(column=5,row=3)

         self.button = Button(master, text="plot here",command=self.plot)
         self.button.grid(column=5,row=4)
    def ok(self, x=1000,y=20):
        self.line.set_data([x],[y])
        self.canvas.draw_idle()

    def action(self):
        self.ok(float(self.spin.get()),float(self.spin2.get()))

    def linecreate(self, x=1000,y=20):
        self.line2.set_data([x],[y])
        self.canvas.draw_idle()

    def plot(self):
        self.linecreate(float(self.spin.get()),float(self.spin2.get()))

master = Tk()
plotter = PlotClass()
plotter.ok(125,10)
master.mainloop()

它通过向x和y轴输入来自两个tkinter旋转框的值来绘制图形,因为旋转框中的值会更改绘图重绘并根据旋转框提供的值来更改标记位置.现在,我添加了一个 tkinter 按钮并将其链接到一个函数,该函数将使用来自旋转框的相同值绘制另一个图,但现在我无法理解如何在按下按钮时不删除以前的图来绘制新图.我的意思是,在图表上绘制新位置(添加更多标记)而不删除通过按下按钮添加的先前绘图位置(先前标记).就像单击按钮时一样,它会根据旋转框值不断添加新标记,而不会删除以前的标记.在上一个问题中,它是删除现有图并在每次Spinbox值更改时添加新图,但现在我不理解如何在不删除前一个图的情况下在新位置上绘制图.

It plots graph by feeding the x and y axis with values from two tkinter spinbox, as the value in spinbox changes the plot redraws and changes the markers location based on the values provided by the spinboxes. Now, I have added a tkinter button and linked it to a function that would draw another plot using the same values coming from the spinboxes but now I could not understand how to draw the new plot without removing the previous plots when the button is pressed. What I mean is, plotting on new locations (adding more markers) on the graph without removing the previous plot locations (previous markers) that has been added by the button press. Like when button clicks it keeps adding the new markers based on the spinbox values without removing the previous ones. In the previous question it was removing the existing plots and adding new ones every time the spinbox value changes but now I could not understand how to plot on new location without removing the previous plot.

推荐答案

您可以将新点附加到现有行的数据上.

You may append you new point to the data of the existing line.

def linecreate(self, x=1000,y=20):
    X,Y = self.line2.get_data()
    X = np.append(X,[x])
    Y = np.append(Y,[y])
    self.line2.set_data(X,Y)
    self.canvas.draw_idle()

这篇关于使用Tkinter Spinbox中的值单击按钮时使用pyplot绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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