1 #-*- coding: utf-8 -*-
2 importpylab3 importrandom4
5 classMiniPlotTool :6 '''
7 A mini tool to draw lines using pylab8 '''
9 basecolors = ['red','green','yellow','blue','black','cyan','magenta']10
11 def __init__(self, baseConfig) :12 self.figsize = baseConfig.get('figsize',None)13 self.axis = baseConfig.get('axis',None)14 self.title = baseConfig.get('title','NoName')15 self.ylabel = baseConfig.get('ylabel','NoName')16 self.grid = baseConfig.get('grid',False)17 self.xaxis_locator = baseConfig.get('xaxis_locator',None)18 self.yaxis_locator = baseConfig.get('yaxis_locator',None)19 self.legend_loc = baseConfig.get('legend_loc',0)20
21 if self.figsize !=None :22 pylab.figure(figsize =self.figsize)23 if self.axis !=None :24 pylab.axis(self.axis)25
26 pylab.title(self.title)27 pylab.ylabel(self.ylabel)28 ax =pylab.gca()29 pylab.grid(self.grid)30 if self.xaxis_locator !=None :31 ax.xaxis.set_major_locator( pylab.MultipleLocator(self.xaxis_locator) )32 if self.yaxis_locator !=None :33 ax.yaxis.set_major_locator( pylab.MultipleLocator(self.yaxis_locator) )34 self.lineList =[]35 self.id = 1
36
37 defaddline(self, lineConf) :38 self.lineList.append((self.id, lineConf))39 self.id += 1
40 return {'id' : self.id - 1}41
42 defremoveline(self, lineId) :43 for i inrange(len(self.lineList)) :44 id, conf =self.lineList[i]45 if id ==lineId :46 delself.lineList[i]47 break
48 else:49 return {'status' : -1}50 printlen(self.lineList)51 return {'status': 0}52
53 def __parselineConf(self, lineConf) :54 X = lineConf['X']55 Y = lineConf['Y']56 marker = lineConf.get('marker',None)57 color = lineConf.get('color', random.choice(MiniPlotTool.basecolors))58 markerfacecolor = lineConf.get('markerfacecolor',color)59 label = lineConf.get('label','NoName')60 linewidth = lineConf.get('linewidth',1)61 linestyle = lineConf.get('linestyle','-')62 returnX, Y, marker, color, markerfacecolor, label, linewidth, linestyle63
64 defplotSingleLine(self, lineConf):65 X, Y, marker, color, markerfacecolor, label, linewidth, linestyle = self.__parselineConf(lineConf)66 pylab.plot(X, Y, marker = marker, color = color, markerfacecolor = markerfacecolor, label=label, linewidth = linewidth, linestyle =linestyle)67 pylab.legend(loc =self.legend_loc)68
69 defplot(self) :70 colors = [MiniPlotTool.basecolors[i % len(MiniPlotTool.basecolors)] for i inrange(len(self.lineList))]71 for i inrange(len(self.lineList)) :72 id, conf =self.lineList[i]73 if conf.get('color',None) :74 conf['color'] =colors[i]75 X, Y, marker, color, markerfacecolor, label, linewidth, linestyle = self.__parselineConf(conf)76 pylab.plot(X, Y, marker = marker, color = color, markerfacecolor = markerfacecolor, label=label, linewidth = linewidth, linestyle =linestyle)77 pylab.legend(loc =self.legend_loc)78
79 defshow(self) :80 pylab.show()81
82
83 if __name__ == '__main__':84 #test
85 baseConfig ={86 #'figsize' : (6,8),
87 #'axis': [0,10,0,10],
88 #'title' : 'hello title',
89 #'ylabel' : 'hello ylabel',
90 'grid': True,91 #'xaxis_locator' : 0.5,
92 #'yaxis_locator' : 1,
93 #'legend_loc' : 'upper right'
94 }95 tool =MiniPlotTool(baseConfig)96 X = [ i for i in range(10)]97 Y = [random.randint(1,10) for i in range(10)]98 Y2 = [random.randint(1,10) for i in range(10)]99 lineConf ={100 'X': X,101 'Y': Y102 #'marker' : 'x',
103 #'color' : 'b',
104 #'markerfacecolor' : 'r',
105 #'label' : '222',
106 #'linewidth' : 3,
107 #'linestyle' : '--'
108 }109 lineConf2 ={110 'X': X,111 'Y': Y2,112 'marker' : 'o',113 'color' : 'b',114 'markerfacecolor' : 'r',115 'label' : '222',116 'linewidth' : 3,117 'linestyle' : '--'
118 }119 #tool.plotSingleLine(lineConf)
120 printtool.addline(lineConf)121 printtool.addline(lineConf2)122
123 #print tool.removeline(1)
124 tool.plot()125 tool.show()