简体   繁体   English

Matplotlib Figuresize被rcParams忽略

[英]Matplotlib Figuresize being ignored by rcParams

I have previously used the following to ensure my figure-size in my plots is a consistent size: 我以前使用以下方法来确保绘图中的图形尺寸是一致的尺寸:

import matplotlib as mpl
rc_fonts = {'figure.figsize': (15, 9.3)}
mpl.rcParams.update(rc_fonts)
import matplotlib.pylab as plt

However, I am now finding that for my usual default values (15, 9.3) this is being ignored. 但是,我现在发现对于我通常的默认值(15, 9.3)这被忽略了。 The following demonstrates this: 以下内容说明了这一点:

import matplotlib as mpl
rc_fonts = {'figure.figsize': (15, 9.3)}
mpl.rcParams.update(rc_fonts)
import matplotlib.pylab as plt
# I draw some boring plot. 
plt.clf()
plt.plot(*[range(10)]*2)
print plt.gcf().get_size_inches()
print mpl.rcParams['figure.figsize']
plt.gcf().set_size_inches(15, 9.3, forward=True)
print plt.gcf().get_size_inches()

The initial plot size is [10.35, 9.3] and after it is [15, 9.3] as desired. 初始绘图大小为[10.35, 9.3] ,之后为所需的[15, 9.3] If however I make the default very much large or smaller, eg (32, 19.3) then the figure window is correctly sized. 但是,如果我将默认值设置得更大或更小,例如(32,19.3),则正确设置了图形窗口的大小。 I would like to keep my desired route of changing rcParams to set the default, rather than trying to set it twice by making an interim dummy plot. 我想保留更改rcParams期望路线以设置默认值,而不是尝试通过制作临时虚拟图来两次设置它。 Is this a bug, or am I going about this the wrong way? 这是一个错误,还是我走错路了?

Details: 细节:

  • Python 2.7.12 (inside a virtual environment, a must). Python 2.7.12(在虚拟环境中,必须)。
  • Backend TkAgg (I want this kept as it is). 后端TkAgg (我希望保持原样)。
  • Matplotlib version 2.1.0. Matplotlib版本2.1.0。 (This bug/feature persists in version 2.1.2 also). (此错误/功能在版本2.1.2中也仍然存在)。

PS - I prefer avoiding having to make matplotlib fig and ax objects and instead use the plt interface directly. PS-我更喜欢避免制作matplotlib figax对象,而直接使用plt接口。 If possible I would like to keep it this way with any solutions. 如果可能的话,我想在所有解决方案中都保持这种方式。

Possible known issue: 可能的已知问题:

I have found the following issue 2716 on github which I think is causing this, but there don't appear any fixes suitable for the rcParam settings route. 我在github上发现了以下问题2716 ,我认为是引起此问题的原因,但是没有出现适合rcParam设置路由的修复程序。 So any help or suggestions are still welcome. 因此,仍然欢迎任何帮助或建议。

Current output: 电流输出:

Following the comments below is some example output (done using Python 3 to allow me to install the latest version of matplotlib): 以下评论是一些示例输出(使用Python 3完成后,我可以安装最新版本的matplotlib):

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
>>> 
... import matplotlib as mpl
... print(mpl.__version__)
... rc_fonts = {'figure.figsize': (15, 9.3)}
... mpl.rcParams.update(rc_fonts)
... import matplotlib.pylab as plt
... plt.plot(*[range(10)]*2)
... 
Backend Qt4Agg is interactive backend. Turning interactive mode on.
2.2.0rc1+124.gf1f83f6
>>> 
... print(plt.gcf().get_size_inches())
... print(mpl.rcParams['figure.figsize'])
... plt.gcf().set_size_inches(15, 9.3, forward=True)
... print(plt.gcf().get_size_inches())
... 
[ 10.35   9.3 ]
[15.0, 9.3]
[ 15.    9.3]

DEMONSTRATION 示范

在此处输入图片说明

Root of the problem 问题的根源

As explained in the accepted answer, the issue is that I am using more than one display, and Matplotlib cannot produce a window that is larger than the primary display. 如已接受的答案中所述,问题是我使用了多个显示器,而Matplotlib无法产生比主显示器大的窗口。 Unfortunately changing what Ubuntu considers to be the primary display is currently an unresolved bug. 不幸的是,更改Ubuntu认为是主要显示的内容目前尚未解决。 Hence the problem does not lie with Matplotlib, but with Ubuntu. 因此,问题不在于Matplotlib,而在于Ubuntu。 I was able to resolve this issue by setting the display to the top left monitor in my setup. 通过将显示设置为设置中的左上方监视器,我能够解决此问题。

The problem occurs because there are several screens in use and the one the figure is shown in is not the primary one. 发生问题是因为有多个屏幕正在使用中 ,图中显示的不是主要屏幕

There is currently no way to automatically show the figure larger than a maximized window on the primary screen would allow for. 当前,无法自动显示超出主屏幕上允许的最大化窗口的数字。 If the figure is larger than that, it is still shrunk to fit the plotting window. 如果图形大于该数字,则仍会缩小以适合绘图窗口。

After the window is initiated, one may indeed resize the figure to any other size, as is being done in the question using 窗口启动后,实际上可以将图形调整为任何其他尺寸,就像在问题中使用

plt.gcf().set_size_inches(15, 9.3, forward=True) 

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

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