Skip to content

Instantly share code, notes, and snippets.

@NanoExplorer
Created July 22, 2018 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NanoExplorer/fee9247c84f985c9ba1e252745502c46 to your computer and use it in GitHub Desktop.
Save NanoExplorer/fee9247c84f985c9ba1e252745502c46 to your computer and use it in GitHub Desktop.
a thingy that acts like a cpu monitor
class animatedplot():
def __init__(self,size):
self.size=size
self.utiliz=np.ones(self.size)*np.nan
self.timearr=np.ones(self.size)*np.nan
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1,1,1)
self.ln, = plt.plot(self.utiliz,self.timearr)
self.anim = FuncAnimation(self.fig,self.update,frames = np.arange(1000),
#init_func=self.animinit(),
blit=False
)
plt.show()
def animinit(self):
self.ax.set_xlim(np.nanmin(self.timearr)*0.99,np.nanmax(self.timearr)*1.01)
self.ax.set_ylim(np.nanmin(self.utiliz)*0.99,np.nanmax(self.utiliz)*1.01)
return self.ln,
def update(self,frame):
new_timearr = np.empty_like(self.timearr)
new_utiliz = np.empty_like(self.utiliz)
new_timearr[1:] = self.timearr[:-1]
new_utiliz[1:] = self.utiliz[:-1]
self.timearr = new_timearr
self.utiliz = new_utiliz
percent = psutil.cpu_percent(interval=None)
self.utiliz[0]=percent
self.timearr[0] = time.time()
self.ln.set_data(self.timearr,self.utiliz)
self.ax.set_xlim(np.nanmin(self.timearr)-0.1,np.nanmax(self.timearr)+0.1)
self.ax.set_ylim(np.nanmin(self.utiliz)-0.1,np.nanmax(self.utiliz)+0.1)
#self.ax.figure.canvas.draw()
return self.ln,
a = animatedplot(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment