Replies: 4 comments 7 replies
-
That is a good question, and I couldn't find an existing solution, so I put this together: from matplotlib.figure import Figure
import solara
@solara.component_vue("viewlistener.vue")
def ViewListener(view_data=None, on_view_data=None, children=[], style={}):
...
@solara.component
def Plot1():
dpi = 100
view_data = solara.use_reactive({"width": 800, "height": 600})
width, height = view_data.value["width"], view_data.value["height"]
fig = Figure(figsize=(width / dpi, height / dpi), dpi=dpi)
ax = fig.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
with ViewListener(view_data=view_data.value, on_view_data=view_data.set, style={"width": "100%", "height": "40vh"}):
solara.FigureMatplotlib(fig)
@solara.component
def Plot2():
dpi = 100
view_data = solara.use_reactive({"width": 800, "height": 600})
width, height = view_data.value["width"], view_data.value["height"]
fig = Figure(figsize=(width / dpi, height / dpi), dpi=dpi)
ax = fig.subplots()
ax.bar([1, 2, 3], [1, 4, 9])
with ViewListener(view_data=view_data.value, on_view_data=view_data.set, style={"width": "100%", "height": "40vh"}):
solara.FigureMatplotlib(fig)
@solara.component
def Page():
with solara.ColumnsResponsive(12, 12, 6):
with solara.Card("This months profits"):
Plot1()
with solara.Card("This months expenses"):
Plot2() With viewlistener.vue this file: <template>
<div :style="style">
<jupyter-widget v-for="child in children" :key="child" :widget="child"></jupyter-widget>
</div>
</template>
<script>
module.exports = {
created() {
this.resizeObserver = new ResizeObserver(entries => {
this._updateViewData();
});
},
mounted() {
this.resizeObserver.observe(this.$el);
this._updateViewData();
},
destroyed() {
this.resizeObserver.unobserve(this.$el);
},
methods: {
_updateViewData() {
const view_data = {
width: this.$el.clientWidth,
height: this.$el.clientHeight,
};
this.view_data = view_data
}
},
}
</script>
<style id="viewlistener"></style>
matplotliv-reactive.mp4We might want to put this component into solara in some form or another. |
Beta Was this translation helpful? Give feedback.
-
Would it not be possible to use ipympl ? |
Beta Was this translation helpful? Give feedback.
-
Hi, Can't get this working with. python 3.10.12 Last part of the stack trace The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/dev_user/code/solara_play/.venv/lib/python3.10/site-packages/reacton/core.py", line 1966, in _reconsolidate
widget, orphan_ids = el._create_widget(kwargs)
File "/home/dev_user/code/solara_play/.venv/lib/python3.10/site-packages/reacton/core.py", line 401, in _create_widget
raise RuntimeError(f"Could not create widget {self.component.widget} with {kwargs}") from e
RuntimeError: Could not create widget <class 'ipyvuetify.generated.Sheet.Sheet'> with {'class_': 'd-flex ma-0', 'style_': 'flex-direction: column; align-items: stretch; row-gap: 12px;;', 'elevation': 0, 'children': [Canvas(header_visible=False, resizable=False, toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous view', 'arrow-left', 'back'), ('Forward', 'Forward to next view', 'arrow-right', 'forward'), ('Pan', 'Left button pans, Right button zooms\nx/y fixes axis, CTRL fixes aspect', 'arrows', 'pan'), ('Zoom', 'Zoom to rectangle\nx/y fixes axis', 'square-o', 'zoom'), ('Download', 'Download plot', 'floppy-o', 'save_figure')]))]} Can someone confirm it's not me being special. Kind regards |
Beta Was this translation helpful? Give feedback.
-
Seems like there was an issue with the call to plt.switch_backend("module://ipympl.backend_nbagg") in the component, causing it to close all figures. Not sure why that was not an issue before. I debugged it at: And moved that particular call at the module level. It seems to work much better now. Let me know if this solves your issue. |
Beta Was this translation helpful? Give feedback.
-
How do I adjust the width of a matplotlib figure to the size available? I know how to change the size of the figure, but how can I obtain the information needed from the context? Or is the wrong angle?
Beta Was this translation helpful? Give feedback.
All reactions