【问题标题】:Tkinter canvas to plot windroseTkinter 画布绘制风玫瑰图
【发布时间】:2017-08-24 07:45:37
【问题描述】:

在 tkinter 窗口中绘制 Windrose 时遇到了一些问题。我无法让风玫瑰自行显示,窗口显示情节后面的另一个网格(见下图)。有什么方法可以让这个只显示windrose而没有背景中的额外网格?最终代码将每 x 秒更新一次,这就是情节动画的原因

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()

【问题讨论】:

    标签: python tkinter canvas windrose


    【解决方案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()
    

    【讨论】:

    • 哦,是的,愚蠢的错误,现在完美运行。感谢您澄清这一点,我很感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多