2017-08-24 109 views
1

在tkinter窗口內繪製Windrose時遇到一些麻煩。我不能讓windrose自己顯示,窗口顯示劇情背後的另一個網格(見下圖)。有什麼方法可以讓我只顯示背景中沒有額外網格的windrose?最終的代碼將更新每隔X秒,這是該地塊動畫Tkinter帆布繪製風玫瑰

from tkinter import * 
from windrose import WindroseAxes 
from matplotlib import pyplot as plt 
import numpy as np 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
import matplotlib.animation as animation 
from matplotlib import style 
import tkinter as tk 


root = Tk() 
style.use("ggplot") 

fig = plt.figure(figsize=(6,4),dpi=100) 
a = fig.add_subplot(111) 

def animate(i): 
    ws = np.random.random(500) * 6 
    wd = np.random.random(500) * 360 

    a.clear() 
    rect = [0.1,0.1,0.8,0.8] 
    wa = WindroseAxes(fig, rect) 
    fig.add_axes(wa) 
    wa.bar(wd, ws, normed=True, opening=0.8, edgecolor='white') 
    wa.set_legend() 

canvas = FigureCanvasTkAgg(fig,root) 
canvas.show() 
canvas.get_tk_widget().pack(side=tk.BOTTOM,fill=tk.BOTH, 
        expand=True,anchor='s') 

ani = animation.FuncAnimation(fig, animate, interval=1000) 
root.mainloop() 

Windrose with two grids

回答

1

你是在彼此頂部繪製兩幅地塊的原因:

from tkinter import * 
from windrose import WindroseAxes 
from matplotlib import pyplot as plt 
import numpy as np 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
import matplotlib.animation as animation 
from matplotlib import style 
import tkinter as tk 


root = Tk() 
style.use("ggplot") 

fig = plt.figure(figsize=(6,4),dpi=100) 
#a = fig.add_subplot(111) # this gives you the square grid in the background 

def animate(i): 
    ws = np.random.random(500) * 6 
    wd = np.random.random(500) * 360 

# a.clear() # not needed if plot doesn't exist 
    fig.clear() # we need to clear the figure you're actually plotting to 
    rect = [0.1,0.1,0.8,0.8] 
    wa = WindroseAxes(fig, rect) 
    fig.add_axes(wa) 
    wa.bar(wd, ws, normed=True, opening=0.8, edgecolor='white') 
    wa.set_legend() 

canvas = FigureCanvasTkAgg(fig,root) 
canvas.show() 
canvas.get_tk_widget().pack(side=tk.BOTTOM,fill=tk.BOTH, 
        expand=True,anchor='s') 

ani = animation.FuncAnimation(fig, animate, interval=1000) 
root.mainloop() 
+0

哦,是啊愚蠢的錯誤,現在完美。感謝您清理那些,我很感激 –