How to get a Matplotlib figure to scroll + resize properly in a Tkinter GUI

后端 未结 2 1924
小蘑菇
小蘑菇 2020-12-15 13:59

I have a Tkinter GUI which displays a Matplotlib plot (Python 2.7.3 with Matplotlib 1.2.0rc2) and lets the user configure certain aspects of the plot. The plots tend to get

2条回答
  •  萌比男神i
    2020-12-15 14:22

    I just bumped in the same problem - and as far as I can see (by experimentation), beyond figure.set_size_inches(), you must also set the new size of mplCanvas and of the canvas-created window for it, before doing figure.canvas.draw() (which then also forces one to use global vars - or class definitions). Also, no need to "grid" mplCanvas apparently - as it is already a child of canvas, which is already "grid"ded. And probably want to anchor NW, so at each resize, the plot is redrawn at 0,0 at top-left corner.

    Here is what worked for me (I also tried with "internal" frame as in Python Tkinter scrollbar for frame, but that didn't work; some of that is left at end of snippet):

    import math
    import sys
    if sys.version_info[0] < 3:
      from Tkinter import Tk, Button, Frame, Canvas, Scrollbar
      import Tkconstants
    else:
      from tkinter import Tk, Button, Frame, Canvas, Scrollbar
      import tkinter.constants as Tkconstants
    
    from matplotlib import pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    import pprint
    
    frame = None
    canvas = None
    
    def printBboxes(label=""):
      global canvas, mplCanvas, interior, interior_id, cwid
      print("  "+label,
        "canvas.bbox:", canvas.bbox(Tkconstants.ALL),
        "mplCanvas.bbox:", mplCanvas.bbox(Tkconstants.ALL))
    
    def addScrollingFigure(figure, frame):
      global canvas, mplCanvas, interior, interior_id, cwid
      # set up a canvas with scrollbars
      canvas = Canvas(frame)
      canvas.grid(row=1, column=1, sticky=Tkconstants.NSEW)
    
      xScrollbar = Scrollbar(frame, orient=Tkconstants.HORIZONTAL)
      yScrollbar = Scrollbar(frame)
    
      xScrollbar.grid(row=2, column=1, sticky=Tkconstants.EW)
      yScrollbar.grid(row=1, column=2, sticky=Tkconstants.NS)
    
      canvas.config(xscrollcommand=xScrollbar.set)
      xScrollbar.config(command=canvas.xview)
      canvas.config(yscrollcommand=yScrollbar.set)
      yScrollbar.config(command=canvas.yview)
    
      # plug in the figure
      figAgg = FigureCanvasTkAgg(figure, canvas)
      mplCanvas = figAgg.get_tk_widget()
      #mplCanvas.grid(sticky=Tkconstants.NSEW)
    
      # and connect figure with scrolling region
      cwid = canvas.create_window(0, 0, window=mplCanvas, anchor=Tkconstants.NW)
      printBboxes("Init")
      canvas.config(scrollregion=canvas.bbox(Tkconstants.ALL),width=200,height=200)
    
    def changeSize(figure, factor):
      global canvas, mplCanvas, interior, interior_id, frame, cwid
      oldSize = figure.get_size_inches()
      print("old size is", oldSize)
      figure.set_size_inches([factor * s for s in oldSize])
      wi,hi = [i*figure.dpi for i in figure.get_size_inches()]
      print("new size is", figure.get_size_inches())
      print("new size pixels: ", wi,hi)
      mplCanvas.config(width=wi, height=hi) ; printBboxes("A")
      #mplCanvas.grid(sticky=Tkconstants.NSEW)
      canvas.itemconfigure(cwid, width=wi, height=hi) ; printBboxes("B")
      canvas.config(scrollregion=canvas.bbox(Tkconstants.ALL),width=200,height=200)
      figure.canvas.draw() ; printBboxes("C")
      print()
    
    if __name__ == "__main__":
      root = Tk()
      root.rowconfigure(1, weight=1)
      root.columnconfigure(1, weight=1)
    
      frame = Frame(root)
      frame.grid(column=1, row=1, sticky=Tkconstants.NSEW)
      frame.rowconfigure(1, weight=1)
      frame.columnconfigure(1, weight=1)
    
      figure = plt.figure(dpi=150, figsize=(4, 4))
      plt.plot(range(10), [math.sin(x) for x in range(10)])
    
      addScrollingFigure(figure, frame)
    
      buttonFrame = Frame(root)
      buttonFrame.grid(row=1, column=2, sticky=Tkconstants.NS)
      biggerButton = Button(buttonFrame, text="larger",
                            command=lambda : changeSize(figure, 1.5))
      biggerButton.grid(column=1, row=1)
      smallerButton = Button(buttonFrame, text="smaller",
                             command=lambda : changeSize(figure, .5))
      smallerButton.grid(column=1, row=2)
    
      root.mainloop()
    
    """
      interior = Frame(canvas) #Frame(mplCanvas) #cannot
      interior_id = canvas.create_window(0, 0, window=interior)#, anchor=Tkconstants.NW)
      canvas.config(scrollregion=canvas.bbox("all"),width=200,height=200)
      canvas.itemconfigure(interior_id, width=canvas.winfo_width())
    
      interior_id = canvas.create_window(0, 0, window=interior)#, anchor=Tkconstants.NW)
      canvas.config(scrollregion=canvas.bbox("all"),width=200,height=200)
      canvas.itemconfigure(interior_id, width=canvas.winfo_width())
    """
    

    An interesting note is that mplCanvas will obey the sizing if it grows larger (as in click on "larger") - but keep the old size if it gets smaller:

    $ python2.7 test.py 
    ('  Init', 'canvas.bbox:', (0, 0, 610, 610), 'mplCanvas.bbox:', (0, 0, 600, 600))
    ## here click "larger":
    ('old size is', array([ 4.06666667,  4.06666667]))
    ('new size is', array([ 6.1,  6.1]))
    ('new size pixels: ', 915.0, 915.0)
    ('  A', 'canvas.bbox:', (0, 0, 925, 925), 'mplCanvas.bbox:', (0, 0, 926, 926))
    ('  B', 'canvas.bbox:', (0, 0, 915, 915), 'mplCanvas.bbox:', (0, 0, 926, 926))
    ('  C', 'canvas.bbox:', (0, 0, 915, 915), 'mplCanvas.bbox:', (0, 0, 926, 926))
    ()
    ## here click "larger":
    ('old size is', array([ 6.1,  6.1]))
    ('new size is', array([ 9.15,  9.15]))
    ('new size pixels: ', 1372.4999999999998, 1372.4999999999998)
    ('  A', 'canvas.bbox:', (0, 0, 915, 915), 'mplCanvas.bbox:', (0, 0, 926, 926))
    ('  B', 'canvas.bbox:', (0, 0, 1372, 1372), 'mplCanvas.bbox:', (0, 0, 926, 926))
    ('  C', 'canvas.bbox:', (0, 0, 1372, 1372), 'mplCanvas.bbox:', (0, 0, 1372, 1372))
    ()
    ## here click "smaller":
    ('old size is', array([ 9.14666667,  9.14666667]))
    ('new size is', array([ 4.57333333,  4.57333333]))
    ('new size pixels: ', 686.0, 686.0)
    ('  A', 'canvas.bbox:', (0, 0, 1372, 1372), 'mplCanvas.bbox:', (0, 0, 1372, 1372))
    ('  B', 'canvas.bbox:', (0, 0, 686, 686), 'mplCanvas.bbox:', (0, 0, 1372, 1372))
    ('  C', 'canvas.bbox:', (0, 0, 686, 686), 'mplCanvas.bbox:', (0, 0, 1372, 1372))
    ()
    

    Same behavior of mplCanvas can be seen in Python3.2, as well... not sure if this is a bug of sorts, or I too am not understanding something right :)

    Note also that this scaling in this way doesn't handle resizing of fonts of axes/tics etc (fonts will try to remain the same size); this is what I can eventually get with above code (truncated tics):

    screenshot of code

    ... and it gets even worse if you add axis labels, etc.

    Anyways, hope this helps,
    Cheers!

提交回复
热议问题