用matplotlib制作的可视化累加器,主要用于查找算法的可视化。

matplotlib 简介

matplotlib 是一个2D的python图形库,主要用于在多个平台上生产可交互的高质量图像,并提供多种文件格式供保存。matplotlib可用于多种python平台(如:python shell,ipython,web应用,以及六种gui toolkits)。

例如:

可视化累加器

用于查找及搜索算法的可视化,动态显示算法的时间消耗。

api

1
2
3
4
5
6
7
8
9
class VisualAccumulator(object):
def add_point(self, x, y):
# 在图片上添加点(x, y)
...
def add_data_value(self, value=0):
# 添加数据value,并计算平均值
...

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import warnings
import matplotlib.cbook
import matplotlib.pyplot as plt
from matplotlib.pyplot import xlim,ylim
from pylab import plot,gcf
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
class VisualAccumulator(object):
plt.figure(num=None, figsize=(8, 6), dpi=100, facecolor='w', edgecolor='k')
plt.ion()
ax = plt.gca()
ax.set_autoscale_on(True)
fig = gcf()
fig.canvas.set_window_title('VisualAccumulator')
line, = plot([], [], linestyle='none', marker='.', color='grey', lw=0.2)
line_ave, = plot([], [], linestyle='none', marker='.', color='black', lw=0.2)
xdata, ydata = [], []
xdata_ave, ydata_ave = [], []
def __init__(self, x=1000, y=1000):
self.n = 0
self.total = 0
xlim(0, x)
ylim(0, y)
def add_point(self, x, y, ave=False):
if not ave:
# add data to xdata and ydata
self.xdata.append(x)
self.ydata.append(y)
self.line.set_data(self.xdata, self.ydata)
else:
# add data to xdata_ave and ydata_ave
self.xdata_ave.append(x)
self.ydata_ave.append(y)
self.line_ave.set_data(self.xdata_ave, self.ydata_ave)
# Recompute the data limits based on current artists.
# Artist: Abstract base class for someone who renders into a FigureCanvas.
self.ax.relim()
# autoscale_view(tight=None, scalex=True, scaley=True):
# Autoscale the view limits using the data limits.
self.ax.autoscale_view(True, True, True)
xmin, xmax = xlim()
if x>=xmax:
xlim(xmin,xmax*1.5)
ylim(xlim())
plt.draw()
plt.pause(0.00000000000001)
def add_data_value(self, value=0):
self.n +=1
self.total += value
self.add_point(self.n, value)
self.add_point(self.n, self.total/self.n, ave=True)
# 测试用例
if __name__ == '__main__':
va_test = VisualAccumulator(20, 20)
va_test.add_data_value(3)
va_test.add_data_value(7)
va_test.add_data_value(15)
va_test.add_data_value(2)
plt.pause(10)

应用

可视化查找算法,如:顺序查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class SequentialSearchST(object):
def __init__(self):
...
def get(self, key):
# 根据指定的key返回data,没找到key则返回None
...
def put(self, key, data):
# 将指定的key的data值置为data,没找到key则插入为头节点
...
def __contains__(self, key):
# 判断指定的key值是否存在
...
@property
def keys(self):
# 以列表形式返回所有key
...
@timeit
def main(file, n=None):
va = VisualAccumulator(20,20)
st = SequentialSearchST()
with open(file, 'r') as f:
for line in f:
for word in line.split():
if len(word) < n:
continue
elif word not in st:
cmp_times = st.put(word, 1)
va.add_data_value(cmp_times)
else:
cmp_times = st.put(word, st.get(word) + 1)
va.add_data_value(cmp_times)
st.max.data = 0
for key in st.keys:
tmp = st.get(key)
if tmp > st.max.data:
st.max.key = key
st.max.data = tmp
print(st.max)
if __name__ == '__main__':
min_len = 8
# 查找双城记的前2000行,过滤长度小于8的单词
main(file='tale2k.txt', n=min_len)
  • 查找过程:

  • 运行结果:

本文地址: http://www.cuixiaochen.com/2016/08/01/Matplotlib-制作可视化累加器-visual-accumulator/