首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制windrose的Tkinter画布

绘制windrose的Tkinter画布
EN

Stack Overflow用户
提问于 2017-08-24 15:45:37
回答 1查看 370关注 0票数 1

在tkinter窗口中绘制Windrose遇到一些问题。我不能让windrose自己显示,窗口在绘图后面显示了另一个网格(见下图)。有没有办法让它只显示windrose而不在背景中显示额外的网格?最终的代码将每x秒更新一次,这就是动画效果的原因

代码语言:javascript
复制
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()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-24 16:04:09

您正在绘制两个相互重叠的绘图:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45855788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档