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

Skip to content

ENH: macosx allow figures to be opened in tabs or windows #26071

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

Merged
merged 2 commits into from
Jul 21, 2023

Conversation

greglucas
Copy link
Contributor

@greglucas greglucas commented Jun 4, 2023

PR summary

Allow users to select how new figures are created when opening new windows. The default is to keep using the system preferences, but one can also select "tab" or "window" to override the default system preferences. I've called the new rcParam figure.window_mode, but definitely open to other suggestions. This is currently only enabled on macosx backend, but I could see it being possible on others in the future as well which is why I put it under figure.* and not under the more specific macosx.*, but I could also see how this would lead to confusion when users set this and don't get the desired result.

closes #13164

Example of the first figure opening in a new window, and the next two figures opening in a separate window with tabs.

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

x = np.linspace(0, 2, 100)

mpl.rcParams["macosx.window_mode"] = "window"
f1 = plt.figure(1)
plt.plot(x,x**2)

mpl.rcParams["macosx.window_mode"] = "tab"
f2 = plt.figure(2)
plt.plot(x,x**2)

f3 = plt.figure(3)
plt.plot(x,x**2)

plt.show()

PR checklist

@greglucas greglucas changed the title ENH: macosx allow figures can be opened in tabs or windows ENH: macosx allow figures to be opened in tabs or windows Jun 4, 2023
@efiring
Copy link
Member

efiring commented Jun 5, 2023

My suspicion is that this is so Mac-specific that #13164 (comment) ("osx.window_mode") makes more sense than "figure.window_mode".

Getting tabs in other backends will require putting in tabbed-window widgets. Instead of using an rcParam, it would be more natural to explicitly instantiate a TabContainer when needed. Functions creating a Figure could then have an optional kwarg specifying that TabContainer as a destination. All that is a lot of work, multiplied by the number of backends to be supported. It would be a nice feature, but on my mental list of GUI enhancements it's not at the top--which is auto-scrollbars, so that large figures can be viewed on a modest screen without having to mess with figure size and/or dpi (#7338). That would also be a lot of work to implement.

@greglucas greglucas force-pushed the macosx-tabs branch 2 times, most recently from c345f0c to 1226e03 Compare June 15, 2023 13:52
@greglucas
Copy link
Contributor Author

Updated to macosx.window_mode.

@tacaswell tacaswell added this to the v3.8.0 milestone Jun 16, 2023
@tacaswell
Copy link
Member

To do this for all backends the right hook is

if new_figure_manager is None:
# Only try to get the canvas class if have opted into the new scheme.
canvas_class = backend_mod.FigureCanvas
def new_figure_manager_given_figure(num, figure):
return canvas_class.new_manager(figure, num)
def new_figure_manager(num, *args, FigureClass=Figure, **kwargs):
fig = FigureClass(*args, **kwargs)
return new_figure_manager_given_figure(num, fig)
def draw_if_interactive():
if matplotlib.is_interactive():
manager = _pylab_helpers.Gcf.get_active()
if manager:
manager.canvas.draw_idle()
backend_mod.new_figure_manager_given_figure = \
new_figure_manager_given_figure
backend_mod.new_figure_manager = new_figure_manager
backend_mod.draw_if_interactive = draw_if_interactive
which hits which hits
@classmethod
def new_manager(cls, figure, num):
"""
Create a new figure manager for *figure*, using this canvas class.
Notes
-----
This method should not be reimplemented in subclasses. If
custom manager creation logic is needed, please reimplement
``FigureManager.create_with_canvas``.
"""
return cls.manager_class.create_with_canvas(cls, figure, num)
which hits
@classmethod
def create_with_canvas(cls, canvas_class, figure, num):
"""
Create a manager for a given *figure* using a specific *canvas_class*.
Backends should override this method if they have specific needs for
setting up the canvas or the manager.
"""
return cls(canvas_class(figure), num)

I think that the path to getting tabbed versions of the other backends would be a sub-class with either a custom __init__ or over-riding that class method, but nothing as simple as the OSX change!

@timhoffm
Copy link
Member

Since this is basically an OS feature and the only Matplotlib API involved is an rcParam, I would be ok to add this only for osx.

Copy link
Member

@melissawm melissawm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, works great on my intel mac.

@ksunden
Copy link
Member

ksunden commented Jul 16, 2023

There have been various various requests and attempts at implementing tabs in the past, the conversation has mostly been directed to #2194. There have been reservations raised on the basis of parity between GUI backends.

If this is something we want for all gui backends, then I'm not sure about making the rcparam specific to macosx, but also don't like having options without the implementation for the other backends, so not sure...

I think we should add this to the meeting schedule to discuss in a higher bandwidth setting.

Allow users to select how new figures are created when opening
new windows. The default is to keep using the system preferences,
but one can also select "tab" or "window" to override the default
system preferences.
Co-authored-by: Kyle Sunden <[email protected]>
@ksunden ksunden merged commit 4dbba98 into matplotlib:main Jul 21, 2023
@greglucas greglucas deleted the macosx-tabs branch July 21, 2023 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Figures in windows not tabs
7 participants