如何在tkin中嵌入xaxis和yaxis

2024-04-26 11:34:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我用jupyter笔记本。在

我成功地嵌入了matplotlib图形,但是x-label、y-label、x-axis和y-axis没有被嵌入。在

我解决不了这个问题

import matplotlib as mpl
import numpy as np
import sys
if sys.version_info[0] < 3:
import Tkinter as tk
else:
    import tkinter as tk
    import matplotlib.backends.tkagg as tkagg
    from matplotlib.backends.backend_agg import FigureCanvasAgg

def draw_figure(canvas, figure, loc=(0, 0)):
    figure_canvas_agg = FigureCanvasAgg(figure)
    figure_canvas_agg.draw()
    figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
    figure_w, figure_h = int(figure_w), int(figure_h)
    photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)

    canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)

    tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)


    return photo
w, h = 400, 400
window = tk.Tk()
window.title("the graph of y") 
canvas = tk.Canvas(window, width=w, height=h)
canvas.pack()


fig = mpl.figure.Figure(figsize=(4,4))
ax = fig.add_axes([0, 0, 1, 1])
ax.plot(x, y,color="red")
ax.set_xlabel("x")
ax.set_ylabel("y")

fig_x=50
fig_y=50
fig_photo = draw_figure(canvas, fig, loc=(fig_x, fig_y))
fig_w, fig_h = fig_photo.width(), fig_photo.height()

tk.mainloop()

x,y是用户输入的数字


Tags: importmatplotlibasfigaxwidthlocagg