0

I am using Python 3.5.3 on Debian 9.

Tkinter window crashes when clicking the button "Create Plot" to save a plot created using plotnine. The plot however gets successfully saved in the working directory. The code block stated below is a simple recreation of the aforementioned error I am encountering in a larger tkinter application. I am a newbie to python programming and generally use it for bioinformatics data analysis. Please help me.

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

This is the warning that is generated from the terminal

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

I have also tried to run the application by commenting out the plot.save() line as shown in the code block below. The application does not crash on clicking the "Create Plot". It seems that the error is creeping in while I am trying to save the plot and not during generation of the plot using 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()

My larger application is almost ready except for this nagging error I am encountering. I would like my plot to be saved and the Tkinter window to remain open. I would be very thankful for any suggestions.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • Enclose your `plot.save` with a `try: ... except ...:` block. – stovfl Feb 24 '19 at 14:32
  • Sorry tried it. Not working. Tkinter window still crashes with same warning. Feeling helpless: ( – Rajarshi Gupta Feb 24 '19 at 15:26
  • Remove all unused `import ...` and all `tkinter` and call at last `createPlot()`, same behavior? – stovfl Feb 24 '19 at 15:30
  • 1
    Sorry I don't follow clearly but I have done this. I have tried removing all other import statements. Without tkinter or without trying to save the plot the code works flawlessly. Also to note, the tkinter window does not crash when running the code in python3 IDLE. This problem can be replicated only by trying to save the ggplot object as an image, from a tkinter window when running the code from terminal – Rajarshi Gupta Feb 24 '19 at 16:23
  • `plotnine` are build with [`matplotlib` and `mizani`](https://plotnine.readthedocs.io/en/stable/about-plotnine.html) verify if you are using from both the latest version. The warning are about `Saving 12 x 7 in image` using `300 dpi`. This results in hugh data, try the **half** size and dpi. – stovfl Feb 24 '19 at 18:20
  • Both matplotlib and mizani are upgraded using pip3. The image has been downsized to 3x2 with dpi=100. The problem persists still. No change whatsoever. Even updated plotnine using pip3. :( – Rajarshi Gupta Feb 24 '19 at 23:08

0 Answers0