Shrinking down a grid layout containing many figures

26 views
Skip to first unread message

Brad Buran

unread,
Jan 20, 2022, 7:25:40 PM1/20/22
to Enaml
I'm trying to develop an app that presents a grid of Matplotlib figures. While it properly lays out the figures, the default size of the window is way too big for many standard computer monitors. I think this is because QFrame is returning a size hint of 640 x 480.

from enaml.core.api import Looper
from enaml.layout.api import grid
from enaml.widgets.api import Container, MainWindow, MPLCanvas

import matplotlib.pyplot as plt
import numpy as np

def generate_figure():
    figure, axes = plt.subplots(1, 1)
    axes.plot(np.random.uniform(size=50))
    return figure

enamldef Main(MainWindow):

    Container:
        layout_constraints => ():
            plots = [c for c in children if not isinstance(c, Looper)]

            constraints = []
            for p in plots[1:]:
                constraints.append(p.width == plots[0].width)
                constraints.append(p.height == plots[0].height)

            # Organize into a grid
            n_cols = 4
            n_rows = 4
            rows = []
            for i in range(n_rows):
                lb = i * n_cols
                ub = lb + n_cols
                rows.append(plots[lb:ub])
            constraints.append(grid(*rows, column_align='width', row_align='v_center'))
            return constraints

        Looper:
            iterable << range(16)

            MPLCanvas:
                figure << generate_figure()

Is there a way I can change the default size hint for each MPLCanvas so it starts with a more reasonable size (e.g., 300 x 300). If I set the width/height explicitly, e.g.,:

            for p in plots:
                constraints.append(p.width >= 100)
                constraints.append(p.height >= 100)
                constraints.append(p.width <= 300)
                constraints.append(p.height <= 300)

It starts off at a good size, but then the MPLCanvas objects no longer resize if I change the overall window size. Is there a good way to change the default size hint for the MPLCanvas?

I do realize that I could just create a subplot grid in a single figure, but I want to use Enaml to include some input widgets next to each MPLCanvas.

Brad

Chris Colbert

unread,
Jan 20, 2022, 10:59:48 PM1/20/22
to Brad Buran, Enaml
This question comes up enough that we should probably expose a way to override the toolkit-supplied size hint for situations like this.

The short answer is: No, you can't currently override the toolkit supplied hint, but, you *can* ignore it.

Set the following on your MPLCanvas:

resist_width = 'ignore'
resist_height = 'ignore'
hug_width = 'ignore'
hug_height = 'ignore'

Then in your constraints list for that same widget, you can add:

[..., (width == 123) | 'strong', (height == 456) | 'strong', ...]

Links that should help explain:

--
You received this message because you are subscribed to the Google Groups "Enaml" group.
To unsubscribe from this group and stop receiving emails from it, send an email to enaml+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/enaml/8ca57721-0f07-4e75-be48-e8f953837430n%40googlegroups.com.

Brad Buran

unread,
Jan 21, 2022, 10:45:16 AM1/21/22
to Enaml
Thanks! This was very helpful and works perfectly. I looked at those two links and it helped, but I also needed to look at the following two links:


After looking through what you sent along with these two, it finally clicked into place for me.

Brad
Reply all
Reply to author
Forward
0 new messages