简体   繁体   English

matplotlib:使用TkAgg后端减小图形大小时遇到​​麻烦

[英]matplotlib: trouble reducing figure size with TkAgg backend

I am having trouble reducing a matplotlib figure's size with the TkAgg backend. 我在使用TkAgg后端减小matplotlib图的大小时遇到​​问题。

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] . 调用fig.get_size_inches()得到[ 8, 7.475] It is a bit annoying that I get 7.475 instead of 8 , but I can deal with that. 我得到7.475而不是8有点令人讨厌,但是我可以解决这个问题。 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] . 然后调用fig.get_size_inches()得到[ 6, 7.475] A reduction to 6 is a far cry from 2 . 减少到62相差甚远。 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] . 如果我关闭交互模式( plt.ioff() ),则TkAgg后端会给出[8, 8][2, 8] ,但是图形窗口的形状仍然看起来更像[8, 7.475][6, 7.475]

If I switch to the Qt4Agg backend, the problem goes away. 如果我切换到Qt4Agg后端,问题就解决了。 Unfortunately I cannot get the Qt4Agg backend to work with Spyder. 不幸的是,我无法让Qt4Agg后端与Spyder一起使用。 I set Qt4Agg as the backend in Preferences -> Console -> external modules -> GUI backend, and restart Spyder. 我在首选项->控制台->外部模块-> GUI后端中将Qt4Agg设置为后端,然后重新启动Spyder。 A call to mpl.get_backend() returns MacOSX . 调用mpl.get_backend()返回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. 我想要一个与Spyder一起使用的后端,允许我强制将图形窗口置于所有其他窗口的顶部(请参阅此文章 ),并允许我随意更改图形窗口的大小。

In case it matters, I am running matplotlib 1.3.1, and Enthought Canopy Python 2.7.6. 如果很重要,我将运行matplotlib 1.3.1和Enthought Canopy Python 2.7.6。

UPDATE: I got the Qt4Agg backend to work with Spyder. 更新:我有Qt4Agg后端与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. 如果没有人在今天晚上之前发布解决方案以使TkAgg后端正常工作,那么我将发布如何在Spyder中使用Qt4Agg。

Well, perhaps it is not possible to reduce the figure window size with the TkAgg backend. 好吧,也许不可能通过TkAgg后端来减小图形窗口的大小。 Maybe it's a bug. 也许是一个错误。

As a work around, the Qt4Agg lets you change the figure size at will. 作为解决方法,Qt4Agg允许您随意更改图形大小。 Here is how to use the Qt4Agg backend with Spyder 2.3.1. 这是在Spyder 2.3.1中使用Qt4Agg后端的方法。

  1. Go to Preferences -> Console -> external modules 转到首选项->控制台->外部模块
  2. For "Qt-Python bindings library selection" use "PyQt4" 对于“ Qt-Python绑定库选择”,请使用“ PyQt4”
  3. Check the box next to "Install Spyder's input hook for Qt". 选中“为Qt安装Spyder的输入挂钩”旁边的框。
  4. Set GUI backend to "Qt4Agg" (without the quotation marks) 将GUI后端设置为“ Qt4Agg”(不带引号)
  5. Restart Spyder 重新启动Spyder

Note: If you open the preferences pane again after restarting Spyder, you must do step #2 again. 注意:如果在重新启动Spyder之后再次打开首选项窗格,则必须再次执行步骤2。 For some reason, Spyder automatically defaults to "PySide" instead of "PyQt4" any time you open the preferences pane. 由于某些原因,每次打开首选项窗格时,Spyder都会自动默认为“ PySide”而不是“ PyQt4”。 I plan to report this to the Spyder dev team. 我计划将此报告给Spyder开发团队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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