【问题标题】:Plotting lines with pyplot when button clicked with values from tkinter spinbox使用 tkinter spinbox 中的值单击按钮时使用 pyplot 绘制线条
【发布时间】:2018-05-30 07:45:14
【问题描述】:

这是one asked before 的后续问题。 我在下面有一个代码:

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

【问题讨论】:

    标签: python matplotlib plot tkinter


    【解决方案1】:

    您可以将新点附加到现有行的数据中。

    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()
    

    【讨论】:

    • 实际上我忘了在问题中添加一些内容。 facepalm 如果考虑这个:x = [125] and y = [50],那么问题应该是如果最初 x 和 y 是 x[125]y[50],那么每当值更改为 x[125]y[90] 时只有当 x 相同时,才可以仅更改 y 的值,而不是再次绘制它。
    • 那么找出该值是否已经在数组中,如果是,则替换 y 值,如果没有追加它。
    • 我做到了,但似乎它也在改变那些与 x 不同的情节。你能给我一个示例代码吗?非常感谢。 :)
    • 您到底尝试了什么? (不知道怎么能知道问题所在?)
    • if x in X: 检测值是否存在但无法实现,我的意思是替换那个x对应的y值,你能帮帮我吗。
    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多