2017-02-28 7 views
1

これは私の最初のpythonプロジェクトです.MatelotrotをMatplotlibでプロットし、それをtkinterフレームに配置しようとしています。これはすでに完了していますが、GUIと一緒に余分な空の図が表示されます。この空のプロットには目盛りが正しく表示されていますが、GUIのプロットにはプロット上に目盛りが正しく表示されていません(ティック値がどこから来ているのかわかりませんが、ピクセルと思われます)。私はまた、この問題を無駄に解決する方法について、かなりの時間を捜し求めました。私はcanvas.drawを試してみましたが、メソッドとクラスを混乱させましたが、私はまだそれを理解できません...以下は、実行されるコードの抜粋です。表示を指定するコードは、mandelbrot_imageおよびMainPageクラスのメソッドにあります。 ありがとうございます。Extraneous Matplotlib Figure(tkinterのプロット)

import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 

import tkinter as tk 
from tkinter import ttk 
from tkinter import * 


import numpy as np 
from numba import jit 

from matplotlib import pyplot as plt 
from matplotlib import colors 



#maths and display code derived/inspired from Jean Francois Puget 
#https://www.ibm.com/developerworks/community/blogs/jfp/entry/My_Christmas_Gift?lang=en 





@jit 
def mandelbrot(z,maxiter,horizon,log_horizon): 
    c = z 
    for n in range(maxiter): 
     az = abs(z) 
     if az > horizon: 
      return n - np.log(np.log(az))/np.log(2) + log_horizon 
     z = z*z + c 
    return 0 

@jit 
def mandelbrot_set(xmin,xmax,ymin,ymax,width,height,maxiter): 
    horizon = 2.0 ** 40 
    log_horizon = np.log(np.log(horizon))/np.log(2) 
    r1 = np.linspace(xmin, xmax, width) 
    r2 = np.linspace(ymin, ymax, height) 
    n3 = np.empty((width,height)) 
    for i in range(width): 
     for j in range(height): 
      n3[i,j] = mandelbrot(r1[i] + 1j*r2[j],maxiter,horizon, log_horizon) 
    return (r1,r2,n3) 




LARGE_FONT= ("Verdana", 12) 

def mandelbrot_image(xmin=-2.,xmax=0.5,ymin=-1.25,ymax=1.25,width=10,height=10,\ 
      maxiter=1000,cmap='jet',gamma=0.3): 

    dpi = 80 
    img_width = dpi * width 
    img_height = dpi * height 
    x,y,z = mandelbrot_set(xmin,xmax,ymin,ymax,img_width,img_height,maxiter) 

    fig = Figure(figsize=(width, height)) 
    ax = fig.add_subplot(111) 

    ticks = np.arange(0,img_width,3*dpi) 
    x_ticks = xmin + (xmax-xmin)*ticks/img_width 
    plt.xticks(ticks, x_ticks) 
    y_ticks = ymin + (ymax-ymin)*ticks/img_width 
    plt.yticks(ticks, y_ticks) 
    ax.set_title("The Mandelbrot set") 
    norm = colors.PowerNorm(gamma) 
    ax.imshow(z.T,cmap=cmap,origin='lower',norm=norm) 


    return fig 

class base(tk.Tk): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 

     tk.Tk.iconbitmap(self, "iconz.ico") 
     tk.Tk.wm_title(self, "Mandelbrot Renderer") 


     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand = True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 

     for F in (StartPage, MainPage): 

      frame = F(container, self) 

      self.frames[F] = frame 

      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, cont): 

     frame = self.frames[cont] 
     frame.tkraise() 

class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self,parent) 
     label = tk.Label(self, text="Start Page", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 

     button = tk.Button(self, text="Lets Begin", 
        command=lambda: controller.show_frame(MainPage)) 
     button.pack() 
class MainPage(tk.Frame): 
    def getcheckvalue(self): 
     print (self.mvar.get()) 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Graph Page!", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 

     button1 = ttk.Button(self, text="Back to Home", 
         command=lambda: controller.show_frame(StartPage)) 
     button1.pack() 




     fig = mandelbrot_image() 
     canvas = FigureCanvasTkAgg(fig, self) 
     canvas.show() 
     canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) 

     toolbar = NavigationToolbar2TkAgg(canvas, self) 
     toolbar.update() 
     canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True) 

app = base() 
app.geometry ("800x600") 
app.mainloop() 
+0

(答えを受け入れるとは関係ありません) また、私は、標準ツールバーを追加することができますどのように思っていましたプロットに。私はそれを追加しようとしましたが、GUIのプロットではなく、無関係なmatplotlibの図にしか表示されません。 – Simon

+0

[回答](http://stackoverflow.com/a/42483817/4124317) 2つの数字が現れるという問題を解決しましたか?あなたは何の間で変わりましたか? – ImportanceOfBeingErnest

+0

あなたが提供した答えは、tkinterキャンバスにプロットを挿入しました(これは私が感謝しています)。 2番目の図は解決策に伴い発生しました。 – Simon

答えて

1

pyplot.xticks()を呼び出すと、明らかに新しい図が作成されます。この動作は、Python 2.7で再現できないと私はその理由についての不確実だし、解決策はaxesのAPIコマンドを使用することであるが:

さらに合併症を防ぐために、それが賢明かもしれmatplotlib APIを使用してpyroot(plt)を完全に削除してコードを実行します。 NavigationbarをConcering

、私はMainPageさん__init__関数内で、次を使用して、それを得ることができた:

fig = mandelbrot_image() 
    canvas = FigureCanvasTkAgg(fig, self) 
    canvas.show() 
    toolbar = NavigationToolbar2TkAgg(canvas, self) 
    toolbar.update() 
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) 
+0

ありがとう – Simon

関連する問題