我在这里重新绘制图形时遇到了问题。我允许用户指定时间刻度(x轴)中的单位,然后重新计算并调用此函数plots()
。我想简单地更新绘图,而不是将另一个绘图附加到图中。
def plots():
global vlgaBuffSorted
cntr()
result = collections.defaultdict(list)
for d in vlgaBuffSorted:
result[d['event']].append(d)
result_list = result.values()
f = Figure()
graph1 = f.add_subplot(211)
graph2 = f.add_subplot(212,sharex=graph1)
for item in result_list:
tL = []
vgsL = []
vdsL = []
isubL = []
for dict in item:
tL.append(dict['time'])
vgsL.append(dict['vgs'])
vdsL.append(dict['vds'])
isubL.append(dict['isub'])
graph1.plot(tL,vdsL,'bo',label='a')
graph1.plot(tL,vgsL,'rp',label='b')
graph2.plot(tL,isubL,'b-',label='c')
plotCanvas = FigureCanvasTkAgg(f, pltFrame)
toolbar = NavigationToolbar2TkAgg(plotCanvas, pltFrame)
toolbar.pack(side=BOTTOM)
plotCanvas.get_tk_widget().pack(side=TOP)
发布于 2018-03-22 02:54:00
您还可以像下面这样做:这将在for循环的50个循环中绘制一个10x1随机矩阵数据。
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
for i in range(50):
y = np.random.random([10,1])
plt.plot(y)
plt.draw()
plt.pause(0.0001)
plt.clf()
发布于 2015-01-25 01:46:00
这对我很有效。每次都会重复调用更新图形的函数。
import matplotlib.pyplot as plt
import matplotlib.animation as anim
def plot_cont(fun, xmax):
y = []
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def update(i):
yi = fun()
y.append(yi)
x = range(len(y))
ax.clear()
ax.plot(x, y)
print i, ': ', yi
a = anim.FuncAnimation(fig, update, frames=xmax, repeat=False)
plt.show()
"fun“是一个返回整数的函数。FuncAnimation将重复调用"update",它将执行"xmax“次。
发布于 2014-09-08 06:45:14
我已经发布了一个名为python-drawnow的包,它提供了让图形更新的功能,这通常是在一个for循环中调用的,类似于Matlab的drawnow
。
示例用法:
from pylab import figure, plot, ion, linspace, arange, sin, pi
def draw_fig():
# can be arbitrarily complex; just to draw a figure
#figure() # don't call!
plot(t, x)
#show() # don't call!
N = 1e3
figure() # call here instead!
ion() # enable interactivity
t = linspace(0, 2*pi, num=N)
for i in arange(100):
x = sin(2 * pi * i**2 * t / 100.0)
drawnow(draw_fig)
这个包可以处理任何matplotlib图形,并提供在每次图形更新或放入调试器后等待的选项。
https://stackoverflow.com/questions/4098131
复制相似问题