国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用pylab庫實(shí)現(xiàn)畫線功能的方法詳解

2020-01-04 17:17:43
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python使用pylab庫實(shí)現(xiàn)畫線功能的方法。分享給大家供大家參考,具體如下:

pylab 提供了比較強(qiáng)大的畫圖功能,但是函數(shù)和參數(shù)都比較多,很容易搞混。我們平常使用最多的應(yīng)該是畫線了。下面,簡單的對(duì)一些常用的劃線函數(shù)進(jìn)行了封裝,方便使用。

# -*- coding: utf-8 -*-import pylabimport randomclass MiniPlotTool :  '''  A mini tool to draw lines using pylab  '''  basecolors = ['red','green','yellow','blue','black','cyan','magenta']  def __init__(self, baseConfig) :    self.figsize = baseConfig.get('figsize',None)    self.axis = baseConfig.get('axis',None)    self.title = baseConfig.get('title','NoName')    self.ylabel = baseConfig.get('ylabel','NoName')    self.grid = baseConfig.get('grid',False)    self.xaxis_locator = baseConfig.get('xaxis_locator',None)    self.yaxis_locator = baseConfig.get('yaxis_locator',None)    self.legend_loc = baseConfig.get('legend_loc',0)    if self.figsize != None :      pylab.figure(figsize = self.figsize)    if self.axis != None :      pylab.axis(self.axis)    pylab.title(self.title)    pylab.ylabel(self.ylabel)    ax = pylab.gca()    pylab.grid(self.grid)    if self.xaxis_locator != None :      ax.xaxis.set_major_locator( pylab.MultipleLocator(self.xaxis_locator) )    if self.yaxis_locator != None :      ax.yaxis.set_major_locator( pylab.MultipleLocator(self.yaxis_locator) )    self.lineList = []    self.id = 1  def addline(self, lineConf) :    self.lineList.append((self.id, lineConf))    self.id += 1    return {'id' : self.id - 1}  def removeline(self, lineId) :    for i in range(len(self.lineList)) :      id, conf = self.lineList[i]      if id == lineId :        del self.lineList[i]        break    else :      return {'status' : -1}    print len(self.lineList)    return {'status' : 0}  def __parselineConf(self, lineConf) :    X = lineConf['X']    Y = lineConf['Y']    marker = lineConf.get('marker',None)    color = lineConf.get('color', random.choice(MiniPlotTool.basecolors))    markerfacecolor = lineConf.get('markerfacecolor',color)    label = lineConf.get('label','NoName')    linewidth = lineConf.get('linewidth',1)    linestyle = lineConf.get('linestyle','-')    return X, Y, marker, color, markerfacecolor, label, linewidth, linestyle  def plotSingleLine(self, lineConf):    X, Y, marker, color, markerfacecolor, label, linewidth, linestyle = self.__parselineConf(lineConf)    pylab.plot(X, Y, marker = marker, color = color, markerfacecolor = markerfacecolor, label=label, linewidth = linewidth, linestyle = linestyle)    pylab.legend(loc = self.legend_loc)  def plot(self) :    colors = [MiniPlotTool.basecolors[i % len(MiniPlotTool.basecolors)] for i in range(len(self.lineList))]    for i in range(len(self.lineList)) :      id, conf = self.lineList[i]      if conf.get('color',None) :        conf['color'] = colors[i]      X, Y, marker, color, markerfacecolor, label, linewidth, linestyle = self.__parselineConf(conf)      pylab.plot(X, Y, marker = marker, color = color, markerfacecolor = markerfacecolor, label=label, linewidth = linewidth, linestyle = linestyle)    pylab.legend(loc = self.legend_loc)  def show(self) :    pylab.show()if __name__ == '__main__' :  #test  baseConfig = {    #'figsize' : (6,8),    #'axis': [0,10,0,10],    #'title' : 'hello title',    #'ylabel' : 'hello ylabel',    'grid' : True,    #'xaxis_locator' : 0.5,    #'yaxis_locator' : 1,    #'legend_loc' : 'upper right'  }  tool = MiniPlotTool(baseConfig)  X = [ i for i in range(10)]  Y = [random.randint(1,10) for i in range(10)]  Y2 = [random.randint(1,10) for i in range(10)]  lineConf = {    'X' : X,    'Y' : Y    #'marker' : 'x',    #'color' : 'b',    #'markerfacecolor' : 'r',    #'label' : '222',    #'linewidth' : 3,    #'linestyle' : '--'  }  lineConf2 = {    'X' : X,    'Y' : Y2,    'marker' : 'o',    'color' : 'b',    'markerfacecolor' : 'r',    'label' : '222',    'linewidth' : 3,    'linestyle' : '--'  }  #tool.plotSingleLine(lineConf)  print tool.addline(lineConf)  print tool.addline(lineConf2)  #print tool.removeline(1)  tool.plot()  tool.show()

運(yùn)行效果圖如下:

Python,pylab庫,畫線

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

線屬性:

顏色(color 簡寫為 c):

藍(lán)色: 'b' (blue)
綠色: 'g' (green)
紅色: 'r' (red)
藍(lán)綠色(墨綠色): 'c' (cyan)
紅紫色(洋紅): 'm' (magenta)
黃色: 'y' (yellow)
黑色: 'k' (black)
白色: 'w' (white)
灰度表示: e.g. 0.75 ([0,1]內(nèi)任意浮點(diǎn)數(shù))
RGB表示法: e.g. '#2F4F4F' 或 (0.18, 0.31, 0.31)
任意合法的html中的顏色表示: e.g. 'red', 'darkslategray'
線型(linestyle 簡寫為 ls):

實(shí)線: '-'
虛線: '--'
虛點(diǎn)線: '-.'
點(diǎn)線: ':'
點(diǎn): '.'
點(diǎn)型(標(biāo)記marker):

像素: ','
圓形: 'o'
上三角: '^'
下三角: 'v'
左三角: '<'
右三角: '>'
方形: 's'
加號(hào): '+'
叉形: 'x'
棱形: 'D'
細(xì)棱形: 'd'
三腳架朝下: '1'(就是丫)
三腳架朝上: '2'
三腳架朝左: '3'
三腳架朝右: '4'
六角形: 'h'
旋轉(zhuǎn)六角形: 'H'
五角形: 'p'
垂直線: '|'
水平線: '_'
gnuplot 中的steps: 'steps' (只能用于kwarg中)
標(biāo)記大小(markersize 簡寫為 ms):

markersize: 實(shí)數(shù)
標(biāo)記邊緣寬度(markeredgewidth 簡寫為 mew):

markeredgewidth:實(shí)數(shù)
標(biāo)記邊緣顏色(markeredgecolor 簡寫為 mec):

markeredgecolor:顏色選項(xiàng)中的任意值
標(biāo)記表面顏色(markerfacecolor 簡寫為 mfc):

markerfacecolor:顏色選項(xiàng)中的任意值
透明度(alpha):

alpha: [0,1]之間的浮點(diǎn)數(shù)
線寬(linewidth):

linewidth: 實(shí)數(shù)

 

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 襄汾县| 三河市| 富阳市| 花垣县| 湖南省| 安平县| 和林格尔县| 资中县| 定远县| 皋兰县| 黎平县| 桂阳县| 昌江| 斗六市| 恭城| 万荣县| 隆化县| 武鸣县| 江川县| 车致| 建阳市| 天镇县| 金阳县| 朔州市| 岢岚县| 即墨市| 隆昌县| 泌阳县| 民权县| 临朐县| 额济纳旗| 当雄县| 襄垣县| 慈溪市| 儋州市| 江北区| 蚌埠市| 济阳县| 额尔古纳市| 嫩江县| 隆回县|