como desenhar grafico com mat plot lib
Foto de Bruno A.
Por: Bruno A.
26 de Dezembro de 2022

como desenhar grafico com mat plot lib

como desenhar grafico com mat plot lib

Computação Linguagens de Programação Python

import matplotlib.pyplot as plt

# create a figure and an axes
fig, ax = plt.subplots()

# plot some data
ax.plot([1, 2, 3, 4], [1, 4, 9, 16])

# create a variable to store the annotation text
annotation_text = ""

# create a function to update the annotation text
def update_annotation(x, y):
    global annotation_text
    annotation_text = f"({x:.2f}, {y:.2f})"

# create a function to display the annotation
def hover(event):
    global annotation_text
    if event.inaxes:
        # get the x and y coordinates of the mouse
        x, y = event.xdata, event.ydata
        # update the annotation text
        update_annotation(x, y)
        # remove the previous annotation
        for text in ax.texts:
            text.set_visible(False)
        # add the new annotation
        ax.annotate(annotation_text, (x, y))
        # redraw the plot
        fig.canvas.draw_idle()


def click(event):
    global annotation_text
    if event.inaxes:
        # get the x and y coordinates of the mouse
        x, y = event.xdata, event.ydata
        # update the annotation text
        update_annotation(x, y)
        # remove the previous annotation
        for text in ax.texts:
            text.set_visible(False)
        # add the new annotation
        ax.annotate(annotation_text, (x, y))
        # redraw the plot
        fig.canvas.draw_idle()


# connect the hover function to the mouse hover event
#fig.canvas.mpl_connect("motion_notify_event", hover)

# connect the click function to the mouse click event
fig.canvas.mpl_connect("button_press_event", click)

# show the plot
plt.show()

Confira artigos similares

Aprenda sobre qualquer assunto