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

como desenhar grafico com mat plot lib

como desenhar grafico com mat plot lib

Computação Introdução à programação Python Programação

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()

Cadastre-se ou faça o login para comentar nessa publicação.

Confira artigos similares

Confira mais artigos sobre educação

+ ver todos os artigos

Encontre um professor particular

Busque, encontre e converse gratuitamente com professores particulares de todo o Brasil