0

我在 Debian 9 上使用 Python 3.5.3。

单击“创建绘图”按钮以保存使用 plotnine 创建的绘图时,Tkinter 窗口崩溃。然而,该图已成功保存在工作目录中。下面所述的代码块是我在更大的 tkinter 应用程序中遇到的上述错误的简单再现。我是 python 编程的新手,通常将其用于生物信息学数据分析。请帮我。

import tkinter as tk
from fpdf import FPDF
import pandas as pd
from plotnine import *

def createPlot():
    df = {"dates":[1,2,3,4,5,6], "amount":[21,22,18,19,25,15]}
    df = pd.DataFrame(df)

    plot = ggplot(aes(x="dates", y="amount"), data=df) + xlab("Dates") + ylab("Amount") #Create the base plot and axes
    plot = plot + scale_x_continuous(breaks = list(df.dates)) #Format the axes

    plot = plot + geom_line(aes(x=list(df.dates), y=list(df.amount)), data=df) #Create the actual data plot

    plot.save(filename = 'plot.png', dpi=300, width=12, height=7, units="in")  #Save the plot as a PNG image



# The GUI Mainloop
root=tk.Tk()
root.title("Test")
root.minsize(width=200,height=200)
root.maxsize(width=200,height=200)

CreatePlotButton=tk.Button(root,text="Create Plot",command= createPlot) #Button to create plots
CreatePlotButton.pack()
CreatePlotButton.place(x=20,y=100)

root.mainloop()

这是从终端生成的警告

/usr/local/lib/python3.5/dist-packages/plotnine/ggplot.py:706: UserWarning: Saving 12 x 7 in image.
from_inches(height, units), units))
/usr/local/lib/python3.5/dist-packages/plotnine/ggplot.py:707: UserWarning: Filename: plot.png
warn('Filename: {}'.format(filename))

我还尝试通过注释掉 plot.save() 行来运行应用程序,如下面的代码块所示。单击“创建绘图”时应用程序不会崩溃。似乎在我尝试保存情节时错误正在蔓延,而不是在使用 plotnine 生成情节期间。

import tkinter as tk
from fpdf import FPDF
import pandas as pd
from plotnine import *

def createPlot():
    df = {"dates":[1,2,3,4,5,6], "amount":[21,22,18,19,25,15]}
    df = pd.DataFrame(df)

    plot = ggplot(aes(x="dates", y="amount"), data=df) + xlab("Dates") + ylab("Amount") #Create the base plot and axes
    plot = plot + scale_x_continuous(breaks = list(df.dates)) #Format the axes

    plot = plot + geom_line(aes(x=list(df.dates), y=list(df.amount)), data=df) #Create the actual data plot

    #plot.save(filename = 'plot.png', dpi=300, width=12, height=7, units="in")  #Save the plot as a PNG image



# The GUI Mainloop
root=tk.Tk()
root.title("Test")
root.minsize(width=200,height=200)
root.maxsize(width=200,height=200)

CreatePlotButton=tk.Button(root,text="Create Plot",command= createPlot) #Button to create plots
CreatePlotButton.pack()
CreatePlotButton.place(x=20,y=100)

root.mainloop()

除了我遇到的这个烦人的错误之外,我的更大的应用程序几乎准备好了。我希望我的情节被保存并且 Tkinter 窗口保持打开状态。我会非常感谢任何建议。

4

0 回答 0