Skip to content
Discussion options

You must be logged in to vote

Here is an alternative for SVG:

import base64
import io
import matplotlib.pyplot as plt
from nicegui import events, ui

fig, ax = plt.subplots()
fig.set_dpi(96)
ax.plot([80, 100, 120], [1, 2, 3], 'o-')

buffer = io.BytesIO()
plt.savefig(buffer, format='svg')
svg_b64 = base64.b64encode(buffer.getvalue()).decode()
img_height = fig.get_figheight() * fig.get_dpi()

def handle_click(e: events.MouseEventArguments):
    x_data, y_data = ax.transData.inverted().transform((e.image_x, img_height - e.image_y))
    ui.notify(f'data: ({x_data:.1f}, {y_data:.1f})')

ui.interactive_image(f'data:image/svg+xml;base64,{svg_b64}', on_mouse=handle_click)

ui.run()

Replies: 3 comments 13 replies

Comment options

You must be logged in to vote
6 replies
@dmenne
Comment options

@dmenne
Comment options

@dmenne
Comment options

@falkoschindler
Comment options

@dmenne
Comment options

Comment options

You must be logged in to vote
1 reply
@dmenne
Comment options

Comment options

You must be logged in to vote
6 replies
@falkoschindler
Comment options

Answer selected by dmenne
@dmenne
Comment options

@dmenne
Comment options

@dmenne
Comment options

@falkoschindler
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants