简体   繁体   中英

matplotlib: trouble reducing figure size with TkAgg backend

I am having trouble reducing a matplotlib figure's size with the TkAgg backend.

First, let me show that I can increase a figure's size. After running the following code,

import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize = [2, 8])
fig.set_size_inches([8,8], forward = True)
fig.canvas.draw()
print fig.get_size_inches()

a call to fig.get_size_inches() gives [ 8, 7.475] . It is a bit annoying that I get 7.475 instead of 8 , but I can deal with that. Next, when I run the following code

import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure(figsize = [8, 8])
fig.set_size_inches([2,8], forward = True)
fig.canvas.draw()
print fig.get_size_inches()

then a call to fig.get_size_inches() gives [ 6, 7.475] . A reduction to 6 is a far cry from 2 . Note that interactive mode must be on. If I turn interactive mode off ( plt.ioff() ) the TkAgg backend gives [8, 8] and [2, 8] , but the shape of the figure windows still look more like [8, 7.475] and [6, 7.475] .

If I switch to the Qt4Agg backend, the problem goes away. Unfortunately I cannot get the Qt4Agg backend to work with Spyder. I set Qt4Agg as the backend in Preferences -> Console -> external modules -> GUI backend, and restart Spyder. A call to mpl.get_backend() returns MacOSX .

I would like a backend that works with Spyder, allows me to force the figure window to be on top of all other windows (see this post ), and allows me to change the figure window size at will.

In case it matters, I am running matplotlib 1.3.1, and Enthought Canopy Python 2.7.6.

UPDATE: I got the Qt4Agg backend to work with Spyder. If no one posts a solution to make the TkAgg backend work properly by this evening, then I will post how to use Qt4Agg with Spyder.

Well, perhaps it is not possible to reduce the figure window size with the TkAgg backend. Maybe it's a bug.

As a work around, the Qt4Agg lets you change the figure size at will. Here is how to use the Qt4Agg backend with Spyder 2.3.1.

  1. Go to Preferences -> Console -> external modules
  2. For "Qt-Python bindings library selection" use "PyQt4"
  3. Check the box next to "Install Spyder's input hook for Qt".
  4. Set GUI backend to "Qt4Agg" (without the quotation marks)
  5. Restart Spyder

Note: If you open the preferences pane again after restarting Spyder, you must do step #2 again. For some reason, Spyder automatically defaults to "PySide" instead of "PyQt4" any time you open the preferences pane. I plan to report this to the Spyder dev team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM