我使用jupyter笔记本。
我成功嵌入了matplotlib图,但是未嵌入x标签,y标签,x轴和y轴。
我不能解决这个问题
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是用户输入的数字