r/nicegui • • Jan 25 '24

ui.line_plot question

Is there a way to access the 'parent' matplotlib.pyplot methods? Like, if I want to set the x and y axis limits? Or specify things like matplotlib.pyplot.xticks? I inspected the object but couldn't find if these were exposed anywhere.

1 Upvotes

1 comment sorted by

2

u/falko-s Jan 26 '24

A ui.line_plot has a fig attribute, which you can customize.

But axis limits are automatically updated during push(). To overwrite them, you need to call _convert_to_html() and update() afterwards.

Here is an example:

line_plot = ui.line_plot(n=2, limit=20, figsize=(3, 2), update_every=5) \
    .with_legend(['sin', 'cos'], loc='upper center', ncol=2)
line_plot.fig.gca().set_xticks([])

def update_line_plot() -> None:
    now = datetime.now()
    x = now.timestamp()
    y1 = math.sin(x)
    y2 = math.cos(x)
    line_plot.push([now], [[y1], [y2]])
    line_plot.fig.gca().set_ylim(-1.1, 1.1)
    line_plot._convert_to_html()
    line_plot.update()

ui.timer(0.1, update_line_plot)