Skip to content
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

ENH: Use layout='constrained' for raw.plot in matplotlib #12082

Closed
larsoner opened this issue Oct 5, 2023 · 3 comments
Closed

ENH: Use layout='constrained' for raw.plot in matplotlib #12082

larsoner opened this issue Oct 5, 2023 · 3 comments
Assignees
Milestone

Comments

@larsoner
Copy link
Member

larsoner commented Oct 5, 2023

There is a lot of subplot / margin / zen mode stuff in the raw.plot matplotlib backend, too much to change safely in #12050. We should see if using layout='constrained' can get us similar (or better) results as it would probably simplify a lot of code, including a lot of subplots_adjust calls in tutorials and examples to make room for fig.suptitle calls that wouldn't otherwise be required.

@larsoner
Copy link
Member Author

larsoner commented Oct 6, 2023

It doesn't seem doable using 'constrained', but here is a proof of concept that it's doable using Divider to conrol the sizes rather than doing subplots_adjust gymnastics:

Code
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import Divider, Size

h_normal = [Size.Fixed(0.25), Size.Scaled(1.), Size.Fixed(0.25)]
v_normal = [Size.Fixed(0.25), Size.Scaled(1.)]
h_zen = [Size.Fixed(0.25), Size.Scaled(1.), Size.Fixed(0)]
v_zen = [Size.Fixed(0), Size.Scaled(1.)]
zen = False

fig = plt.figure(layout=None)
div = Divider(fig, (0, 0, 1, 1), h_normal, v_normal)
ax_main = fig.add_axes(div.get_position(), axes_locator=div.new_locator(nx=1, ny=1))
ax_main.plot([1, 2, 3])
ax_help = fig.add_axes(div.get_position(), axes_locator=div.new_locator(nx=0, ny=0))
ax_help.text(0, 0, 'help')
ax_hscroll = fig.add_axes(div.get_position(), axes_locator=div.new_locator(nx=1, ny=0))
ax_hscroll.text(0, 0, "hscroll")
ax_vscroll = fig.add_axes(div.get_position(), axes_locator=div.new_locator(nx=2, ny=1))
ax_vscroll.text(0, 0, "vscroll", rotation=90)

def on_press(event):
    global zen
    zen = not zen
    if zen:
        h, v = h_zen, v_zen
        visible = False
    else:
        h, v = h_normal, v_normal
        visible = True
    for ax in (ax_help, ax_hscroll, ax_vscroll):
        ax.set_visible(visible)
    div.set_vertical(v)
    div.set_horizontal(h)
    fig.canvas.draw_idle()

fig.canvas.mpl_connect('key_press_event', on_press)

I'll wait until #12050 is merged then work on this.

@larsoner larsoner added this to the 1.6 milestone Oct 6, 2023
@larsoner larsoner self-assigned this Oct 7, 2023
@larsoner
Copy link
Member Author

Going to see how hard this is now...

@larsoner
Copy link
Member Author

Looks like in the end it's quite buggy. You can get there, but there's no way to set padding in some cases. Then the clicks go through to the wrong axes (always to horizontal). So maybe we should try this someday... but the current solution is not so bad in the end!

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

No branches or pull requests

1 participant