Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在tkinter中嵌入x轴和y轴_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python 如何在tkinter中嵌入x轴和y轴

Python 如何在tkinter中嵌入x轴和y轴,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我用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 fro

我用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()
将matplotlib导入为mpl
将numpy作为np导入
导入系统
如果系统版本信息[0]<3:
将Tkinter作为tk导入
其他:
将tkinter作为tk导入
将matplotlib.backends.tkagg作为tkagg导入
从matplotlib.backends.backend_agg导入图Canvasagg
def draw_图形(画布,图形,位置=(0,0)):
figure_canvas_agg=FigureCanvasAgg(图)
图_canvas_agg.draw()
图x、图y、图w、图h=figure.bbox.bounds
图w,图h=int(图w),int(图h)
photo=tk.PhotoImage(母版=canvas,宽度=figure\u w,高度=figure\u h)
画布。创建图片(位置[0]+图w/2,位置[1]+图h/2,图片=照片)
blit(照片,图形画布,图像获取渲染器()。\u渲染器,颜色模式=2)
返回照片
w、 h=400400
window=tk.tk()
窗口标题(“y图”)
canvas=tk.canvas(窗口,宽度=w,高度=h)
canvas.pack()
图=mpl.图.图(图尺寸=(4,4))
ax=图添加轴([0,0,1,1])
轴图(x,y,color=“红色”)
ax.集合标签(“x”)
ax.set_ylabel(“y”)
图x=50
图y=50
fig_photo=绘制图(画布,图,位置=(图x,图y))
fig_w,fig_h=fig_photo.width(),fig_photo.height()
tk.mainloop()

x、 y是用户输入的数字

我不知道“嵌入轴”的确切含义。但是如果问题是你看不到轴装饰器,这可能是因为它们在图形之外。在这种情况下,使用
ax=fig.add_子图(111)
而不是
ax=fig.add_轴([0,0,1,1])
。OMG genius!!!!!!!谢谢你的好意,很抱歉我的英语不好。