【发布时间】:2017-06-17 17:21:07
【问题描述】:
在我的代码下方。为什么标题不是每次都更新?我读到了这个:Matplotlib animation with blit -- how to update plot title?,但它没有用。
#! /usr/bin/python3
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import random as rr
plt.rc('grid', color='#397939', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=5)
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
fig = plt.figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, facecolor='#cfd98c')
ax.set_rmax(20.0)
plt.grid(True)
plt.title("")
def data_gen(t=0):
tw = 0
phase = 0
ctn = 0
while True:
ctn += 1
if ctn == 1000:
phase=round(rr.uniform(0,180),0)
tw = round(rr.uniform(0,20),0)
ctn = 0
yield tw, phase
def update(data):
tw, phase = data
print(data)
ax.set_title("|TW| = {}, Angle: {}°".format(tw, phase))
arr1 = plt.arrow(phase, 0, 0, tw, alpha = 0.5, width = 0.080,
edgecolor = 'red', facecolor = 'red', lw = 2, zorder = 5)
return arr1,
ani = animation.FuncAnimation(fig, update, data_gen, interval=100, blit=True, repeat=False)
plt.show()
编辑 n。 1 在@eyllanesc 回答之后,我编辑了这段代码:
def data_gen(t=0):
tw = 10
phase = 0
ctn = 0
while True:
if phase < 2*180:
phase += 1
else:
phase=0
yield tw, phase
def update(data):
tw, phase = data
angolo = phase /180 * np.pi
print("|TW| = {}, Angle = {}°".format(tw,phase))
ax.set_title("|TW| = {}, Angle: {}°".format(tw, phase))
arr1 = plt.arrow(angolo, 0, 0, tw, alpha = 0.5, width = 0.080, edgecolor = 'red', facecolor = 'red', lw = 2, zorder = 5)
plt.draw()
return arr1,
现在文本可以正常工作,但箭头更新不是“流畅的”(每次更新都会出现和消失)。
【问题讨论】:
-
更好地解释自己,你的问题是什么?
-
我需要在每一帧更新中,情节的标题也会更新。
-
箭头如果更新了但标题没有更新,对吗?
-
请看我的回答
标签: python matplotlib