How to adjust pyplot to screens #3277
-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
Hi @Guilhermwn, The plot looks fine to me, as it nicely fits the width of the mobile screen. The other elements are a bit narrow though. But I probably don't get what you're trying to achieve. 🤔 Apart from that, can you, please, try to create a minimum reproducible code example? This would allow us and the community to help more efficiently. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Before from nicegui import ui
from matplotlib import pyplot
from lxml import etree
@ui.page('/')
def index():
with ui.column().classes('w-full bg-red-200'):
ui.markdown('# Space reserved for content').classes('text-black')
with ui.element('div').classes('w-full bg-green-200'):
ui.markdown('### Original matplotlib plot inserted using ui.pyplot()').classes('text-black')
with ui.pyplot(close=False) as plot1:
ax = plot1.fig.add_subplot()
ax.plot(
[1,2,3,4],
[1,2,3,4],
color = 'green',
linestyle = '-.'
)
def set_svg_size(plot: ui.pyplot=None, width :str='100%', height: str='100%') -> None:
if plot is None:
pass
else:
e = etree.fromstring(plot._props['innerHTML'].encode())
e.set('width', width)
e.set('height', height)
plot._props['innerHTML']=etree.tostring(e).decode()
plot.update()
# way 1:
# set svg's width and height to 100%.
set_svg_size(plot1)
# way 2:
# save as file,and display it by imgage.
# Before image,should hide pyplot.
# Here just shows two ways,without hiding.
plot1.fig.savefig('./plot.svg')
#plot1.set_visibility(False)
ui.image('./plot.svg')
ui.run() |
Beta Was this translation helpful? Give feedback.
-
Ah, now I understand! The question is, how to automatically shrink with ui.card().classes('w-[400px]'):
with ui.pyplot() as plot:
plot.fig.gca().plot() |
Beta Was this translation helpful? Give feedback.
-
Here's a workaround adding "width:100%;height:100%" to the inner HTML: with ui.card().classes('w-[400px]'):
with ui.pyplot() as plot:
plot.fig.gca().plot()
plot._props['innerHTML'] = plot._props['innerHTML'].replace('<svg', '<svg style="width:100%;height:100%"')
plot.update() But I think we can add these attributes by default so that the SVG automatically fills the size of the |
Beta Was this translation helpful? Give feedback.
Here's a workaround adding "width:100%;height:100%" to the inner HTML:
But I think we can add these attributes by default so that the SVG automatically fills the size of the
ui.pyplot
element.