Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Initial plot window ignoring rcParams figure.figsize #10555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oliversheridanmethven opened this issue Feb 21, 2018 · 12 comments
Closed

Initial plot window ignoring rcParams figure.figsize #10555

oliversheridanmethven opened this issue Feb 21, 2018 · 12 comments

Comments

@oliversheridanmethven
Copy link

Original plotting window does not initially respect rcParams value


Summary

I import matplotlib and then set the rcParams by mpl.rcParams.update( {'figure.figsize': (15, 9.3)}). When I attempt to plot the window, its size is plotted instead as [10.35, 9.3] and not what I originally specified. This has onlst started occuring since I upgraded to a larger monitor. It does not occur for all inputs, and the larger (32, 20) plots correctly the first time.


Details

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. The following demonstrates this (This must be run in stages, see current output section):

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. If however I make the default very much large or smaller, e.g. (32, 19.3) then the figure window is correctly sized. I thought setting rcParams is the recommended route?

Details:

  • Python 2.7.12 (inside a virtual environment, a must).
  • Backend TkAgg (I want this kept as it is).
  • Matplotlib version 2.1.0. (This bug/feature persists in version 2.1.2 also).

I have tried this inside and outside a virtual environment, and also tried Matplotlib versions 2.2 with python 3.5, and still it persists, and with all the backends I can muster (including Qt5Agg).

Possible known issue:

I have found the following issue 2716 which I think could be related, but there don't appear any fixes suitable for the rcParam settings route.

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.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

enter image description here

@ImportanceOfBeingErnest
Copy link
Member

As already commented on the SO question this is hard to reproduce. So anything you can add concerning the configuration in use may help here.

@jklymak
Copy link
Member

jklymak commented Feb 22, 2018

I guess I'm confused why anything plots at all w/o a plt.show() command. Is this some kind of IDE you are running in? Can you reproduce w/o an IDE, and can you perhaps temporarily remove your ~/.matplotlib/matplotlibrc just to make sure you don't have some funky setting.

@oliversheridanmethven
Copy link
Author

I usually use the PyCharm IDE, but I can replicate this from the terminal (which requires using the plt.ion() and the plt.show() commands), in either a normal python session or an IPython session. I have tried removing the matplotlibrc file but then I get the same behaviour (it upsizes the default 6x4 figure size to 10x9, but not the 15x9 requested). I have also tried editing the rcParams file but to no benefit.

To address my configuration, I have noticed this problem since I upgraded to a larger monitor. I now use a 3 monitor setup (laptop + 2 external displays approx 15", 23", and 43" respectively). I am running on a lenovo thinkpad T460 and using Ubuntu 16.04 LTS. Let me know what other details you might need.

@ImportanceOfBeingErnest
Copy link
Member

I suspect the 15" monitor is the primary one in this setup. Hence the figure is resized to match this screen resolution.

@oliversheridanmethven
Copy link
Author

That's correct, changing the larger screen to the primary display did resolve this. One interesting remaining question though is why does this affect not still occur when I set the size to be 30x20? If I set the size to be this large then surprisingly it then works fine.

@oliversheridanmethven
Copy link
Author

I have updated my answer on Stack Overflow, but to summarise:

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. Unfortunately changing what Ubuntu considers to be the primary display is currently an unresolved bug. Hence the problem does not lie with Matplotlib, but with Ubuntu. I was able to resolve this issue by setting the display to the top left monitor in my setup.

@ImportanceOfBeingErnest
Copy link
Member

@beangoben Your issue is completely different. You need to load the inline backend before setting the rcParams.

@jklymak
Copy link
Member

jklymak commented Feb 26, 2018

I'm going to close this as not a MPL bug....

@jklymak jklymak closed this as completed Feb 26, 2018
@ImportanceOfBeingErnest
Copy link
Member

@jklymak Well, it's not a bug, indeed. But the point is maybe that it should be possible to create a figure which is larger than the screen somehow. Ideally I could imagine something like plt.show(force_figsize=True).

@oliversheridanmethven
Copy link
Author

As an extension to the suggestion by @ImportanceOfBeingErnest , perhaps a setting in the rcParams to state that the user is aware that they are asking for a larger plotting window than be displayed on screen.

@QuLogic
Copy link
Member

QuLogic commented Feb 26, 2018

The limit on window size is imposed by the window manager, so there's really nothing that Matplotlib can do to make it bigger. There is the possibility of adding scrollbars as suggested in #7338.

@ImportanceOfBeingErnest
Copy link
Member

@QuLogic Not sure if I understand this restriction. As I see it, it is imposed by matplotlib. As an example, the following creates a Qt Application with a figure size much larger than (my) screen.

import matplotlib.pyplot as plt
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar

class HugeWindow(QtGui.QMainWindow):
    def __init__(self, fig, size=None):
        self.qapp = QtGui.QApplication([])

        QtGui.QMainWindow.__init__(self)
        self.widget = QtGui.QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(QtGui.QVBoxLayout())
        self.widget.layout().setContentsMargins(0,0,0,0)
        self.widget.layout().setSpacing(0)

        self.fig = fig
        self.canvas = FigureCanvas(self.fig)
        self.canvas.draw()

        self.nav = NavigationToolbar(self.canvas, self.widget)
        self.widget.layout().addWidget(self.nav)
        self.widget.layout().addWidget(self.canvas)
        if not size:
            w,h=self.fig.get_size_inches()
        else:
            w,h = size
        self.canvas.setFixedSize(w*self.fig.dpi,h*self.fig.dpi)
        self.show()
        exit(self.qapp.exec_()) 


# create a figure and some subplots
fig, axes = plt.subplots(ncols=4, nrows=5, figsize=(24,16))
for ax in axes.flatten():
    ax.plot([2,3,5,1])

# pass the figure to the custom window
a = HugeWindow(fig)

Of course a Scrollable window would be equally nice, as seen in this SO question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants