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

首頁 > 編程 > Python > 正文

Python使用pylab庫實現畫線功能的方法詳解

2019-11-25 16:07:02
字體:
來源:轉載
供稿:網友

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

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

# -*- 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()

運行效果圖如下:

附:引用自: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: 實數

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥阳县| 化隆| 云安县| 井冈山市| 沿河| 娄底市| 奇台县| 阿合奇县| 西峡县| 惠州市| 莱芜市| 洛川县| 莲花县| 东乌珠穆沁旗| 望城县| 富蕴县| 广南县| 贡觉县| 安丘市| 黄陵县| 陇南市| 海伦市| 柳江县| 涪陵区| 嘉兴市| 灵璧县| 容城县| 嘉荫县| 德安县| 滦南县| 通海县| 商水县| 禄丰县| 建平县| 南宁市| 开江县| 治多县| 台南市| 治多县| 岳阳县| 修武县|