posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.1k stars 62 forks source link

Can one make render.plot and ui.output_plot non dynamic? #1443

Open frere-jacques opened 1 month ago

frere-jacques commented 1 month ago

I have a library with a module that creates and plots matplotlib figures with multiple subplots.

The figsize is set dynamically based on the number and layout of the subplots. There are arrays plotted with imshow, overlayed with markers etc. The figsize, markersize etc. are all aligned to give a nice result.

If I plot this generated figures from terminal, from jupyter, from Pycharm, store it as png, whatsoever, it looks everywhere almost identical.

I want now to make use of that plots in a shiny app. But the format gets completely messed up. Spaces between subplots, labels, colorbars, ticks etc.

If I set the width and height both fixed in px like width="1200px", height='800px' it looks okay. But the next plot might be quadratic and would need width="800px", height='800px'.

Is there any way to extract the aspect ratio out of the returned figure (or maybe I can provide the aspect ratio myself) and use the aspect ration determined in the call to use that for the plot?

Edit: As a workaround I already stored the image to a file and then rendered that file. But I am afraid that this will cause conflicts between sessions or polution with files.