question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Layout Templates and matplotlib figures

See original GitHub issue

see #117 (issue) and #174 (PR) for some discussion on the topic.

@martinRenou I have been playing with using Layout Templates to stack figures in fun ways. Unfortunately, since matplotlib is currently responsible for the figure size, I end up with situations such as the following, where the figures are all cut off a bit. This is particularly noticable in jupyter notebook since the screen width is reduced.

In the following example, I stack a 4x4 of buttons and then a 4x4 of figures. The buttons stretch nicely, but the figures do not.

%matplotlib widget
import matplotlib.pyplot as plt
from ipywidgets import TwoByTwoLayout
from ipywidgets.widgets import Button, Layout

# Buttons
def button(name):
    return Button(description = name, layout=Layout(height='auto', width='auto'))

a,b,c,d =  button("1"), button("2"), button("3"), button("4"),
layout_2x2 = TwoByTwoLayout(top_left=a, 
                            top_right=b,
                            bottom_left=c,
                            bottom_right=d)
display(layout_2x2)

# Figures
figs = []
plt.ioff()
for i in range(4):
    fig, ax = plt.subplots()
    ax.imshow([[1,2], [3,4]])
    figs.append(fig.canvas)

a,b,c,d = figs

layout_2x2 = TwoByTwoLayout(top_left=a, 
                            top_right=b,
                            bottom_left=c,
                            bottom_right=d)
display(layout_2x2)

In jupyter lab the problem is less so, but still unfortunate that figures basically ignore the flexbox layout the widgets are designed with. I would set the size with matplotlib, but since I don’t know the size of the viewport the user is using, it’s likely that the size I choose will be too small or too large depending on the user.

I don’t have a concrete solution at hand, but I’m wondering if we can come up with some logic that preserves aspect ratio but otherwise fills a widget to its fullest extent?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
martinRenoucommented, Sep 8, 2022

Hey Neil 😃

That’s a good question. I guess we could benefit from ipympl supporting SVG outputs, but I suppose such a feature should be an opt-in (as SVG can become huge with many points on a scatter plot, while PNG stays the same size).

1reaction
thomasaarholtcommented, Jun 4, 2020

That could be a solution. We could add some kind of flag to the wigdet, something like stretched or responsive. And when this flag is set to True, instead of requesting the renderer a resize of the available space, we would stretch/shrink the image to the available space respecting the ratio. We would lose quality when stretched, but I guess we don’t have a better solution now.

I would really like this sort of flag. I was trying to add matplotlib widgets to jupyterlab-sidecar just now, and there it would be really awesome to take advantage of the flexbox style resizing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Panel Figure Layouts in Matplotlib with Gridspec
Create Panel Figure Layouts in Matplotlib with Gridspec. Use gridspec to layout your figure without the need to export multiple plots and use ......
Read more >
Examples — Matplotlib 3.6.2 documentation
Resizing axes with constrained layout ... Using Gridspec to make multi-column/row subplot layouts. Nested Gridspecs ... Managing multiple figures in pyplot.
Read more >
Top 50 matplotlib Visualizations - The Master Plots (w
A compilation of the Top 50 matplotlib plots most useful in data analysis ... Simple in design, you don't have to strain in...
Read more >
Theming and templates in Python - Plotly
The theme for a particular graph object figure can be specified by setting the template property of the figure's layout to the name...
Read more >
Visualisation templates for Python | Blog
A selection of visualisation templates for you to build a ... dataset penguins = sns.load_dataset("penguins") # Set figure's size f, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found