使用python pylab庫 畫線

weixin_33831673發表於2013-06-20

pylab 提供了比較強大的畫圖功能,但是函式和引數都比較多,很容易搞混。我們平常使用最多的應該是畫線了。下面,簡單的對一些常用的劃線函式進行了封裝,方便使用。

  1 # -*- coding: utf-8 -*-
  2 import pylab
  3 import random
  4 
  5 class MiniPlotTool :
  6     '''
  7     A mini tool to draw lines using pylab
  8     '''
  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     def addline(self, lineConf) :
 38         self.lineList.append((self.id, lineConf))
 39         self.id += 1
 40         return {'id' : self.id - 1}
 41 
 42     def removeline(self, lineId) :
 43         for i in range(len(self.lineList)) :
 44             id, conf = self.lineList[i]
 45             if id == lineId :
 46                 del self.lineList[i]
 47                 break
 48         else :
 49             return {'status' : -1}
 50         print len(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         return X, Y, marker, color, markerfacecolor, label, linewidth, linestyle
 63 
 64     def plotSingleLine(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     def plot(self) :
 70         colors = [MiniPlotTool.basecolors[i % len(MiniPlotTool.basecolors)] for i in range(len(self.lineList))]
 71         for i in range(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     def show(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' : Y
102         #'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     print tool.addline(lineConf)
121     print tool.addline(lineConf2)
122 
123     #print tool.removeline(1)
124     tool.plot()
125     tool.show()

 

 

 

附:引用自:https://sites.google.com/site/guyingbo/matplotlib%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0

線屬性:

顏色(color 簡寫為 c):

  • 藍色: 'b' (blue)
  • 綠色: 'g' (green)
  • 紅色: 'r' (red)
  • 藍綠色(墨綠色): 'c' (cyan)
  • 紅紫色(洋紅): 'm' (magenta)
  • 黃色: 'y' (yellow)
  • 黑色: 'k' (black)
  • 白色: 'w' (white)
  • 灰度表示: e.g. 0.75 ([0,1]內任意浮點數)
  • RGB表示法: e.g. '#2F4F4F' 或 (0.18, 0.31, 0.31)
  • 任意合法的html中的顏色表示: e.g. 'red', 'darkslategray'

線型(linestyle 簡寫為 ls):

  • 實線: '-'
  • 虛線: '--'
  • 虛點線: '-.'
  • 點線: ':'
  • 點: '.' 

點型(標記marker):

  • 畫素: ','
  • 圓形: 'o'
  • 上三角: '^'
  • 下三角: 'v'
  • 左三角: '<'
  • 右三角: '>'
  • 方形: 's'
  • 加號: '+' 
  • 叉形: 'x'
  • 稜形: 'D'
  • 細稜形: 'd'
  • 三腳架朝下: '1'(就是丫)
  • 三腳架朝上: '2'
  • 三腳架朝左: '3'
  • 三腳架朝右: '4'
  • 六角形: 'h'
  • 旋轉六角形: 'H'
  • 五角形: 'p'
  • 垂直線: '|'
  • 水平線: '_'
  • gnuplot 中的steps: 'steps' (只能用於kwarg中)

標記大小(markersize 簡寫為 ms): 

  • markersize: 實數 

標記邊緣寬度(markeredgewidth 簡寫為 mew):

  • markeredgewidth:實數

標記邊緣顏色(markeredgecolor 簡寫為 mec):

  • markeredgecolor:顏色選項中的任意值

標記表面顏色(markerfacecolor 簡寫為 mfc):

  • markerfacecolor:顏色選項中的任意值

透明度(alpha):

  • alpha: [0,1]之間的浮點數

線寬(linewidth):

  • linewidth: 實數

相關文章