Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1741691
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-06-23 09:41:59

感谢Vamei的matplotlib 的文章:
http://www.cnblogs.com/vamei/archive/2013/01/30/2879700.html

代码如下:
首先用psutil 获得进程的CPU 使用率,然后再用matplotlib绘图。

点击(此处)折叠或打开

  1. import psutil
  2. import sys
  3. import re
  4. ll=[]
  5. def processinfo(pid):
  6.     p=psutil.Process(pid)
  7.     for i in range(10):
  8.         #print(p.cpu_percent(interval=3))
  9.         ll.append(p.cpu_percent(interval=1))
  10.     return ll


  11. #print(processinfo(1051))
  12. import random
  13. from matplotlib.figure import Figure
  14. from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
  15. def plot_cpu_usage(y):
  16.     x=list(range(3,31,3)) #x
  17.     #I will manipulate Y if ll is empty 
  18.     if not y:
  19.         y=[random.randint(1,5) for i in range(10)] #y
  20.    
  21.     fig = Figure()
  22.     canvas = FigureCanvas(fig)
  23.     ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
  24.     line, = ax.plot(x,y)
  25.     ax.set_title("cpu usage for PID 1051")
  26.     ax.set_xlabel(" time in seconds ")
  27.     ax.set_ylabel(" cpu usage in %")
  28.     canvas.print_figure('demo.jpg')

  29. #processinfo(1051)
  30. plot_cpu_usage(ll)

另外我发现3.5的python 跟matplotlib 配合使用的时候,无法使用GTKAgg 作为backend. 

点击(此处)折叠或打开

  1. #next 5 lines error out on Kali linux with python3.5, but works with python2.7
  2. import matplotlib as mpl
  3. mpl.use('GTKAgg')        #use GTK UI        
  4. mpl.use('GTKCairo')
  5. import matplotlib.pyplot as plt
  6. plt.plot([1,3,2,4])
  7. plt.show()



但是我可以正常的import Gtk 

点击(此处)折叠或打开

  1. python3
  2. import gi
  3. gi.require_version('Gtk','3.0')
  4. from gi.repository import Gtk as gtk


阅读(1465) | 评论(0) | 转发(0) |
0

上一篇:从BFS到Dijkstra

下一篇:linux spell check脚本

给主人留下些什么吧!~~