Python使用pylab庫實(shí)現(xiàn)畫線功能的方法詳解
本文實(shí)例講述了Python使用pylab庫實(shí)現(xiàn)畫線功能的方法。分享給大家供大家參考,具體如下:
pylab 提供了比較強(qiáng)大的畫圖功能,但是函數(shù)和參數(shù)都比較多,很容易搞混。我們平常使用最多的應(yīng)該是畫線了。下面,簡單的對一些常用的劃線函數(shù)進(jìn)行了封裝,方便使用。
# -*- coding: utf-8 -*-
import pylab
import random
class 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)行效果圖如下:

附:引用自: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ù)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- python OpenCV學(xué)習(xí)筆記之繪制直方圖的方法
- Python中的pygal安裝和繪制直方圖代碼分享
- python數(shù)字圖像處理實(shí)現(xiàn)直方圖與均衡化
- python opencv 直方圖反向投影的方法
- Python OpenCV 直方圖的計(jì)算與顯示的方法示例
- 詳解python OpenCV學(xué)習(xí)筆記之直方圖均衡化
- python OpenCV學(xué)習(xí)筆記實(shí)現(xiàn)二維直方圖
- python OpenCV學(xué)習(xí)筆記直方圖反向投影的實(shí)現(xiàn)
- Python使用pylab庫實(shí)現(xiàn)繪制直方圖功能示例
相關(guān)文章
Appium+Python自動(dòng)化環(huán)境搭建實(shí)例教程
這篇文章主要介紹了Appium+Python自動(dòng)化環(huán)境搭建實(shí)例教程,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
VSCode2022配置Python3.9.6的詳細(xì)教程
這篇文章主要介紹了VSCode2022配置Python3.9.6教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
基于Tensorflow搭建一個(gè)神經(jīng)網(wǎng)絡(luò)的實(shí)現(xiàn)
神經(jīng)網(wǎng)絡(luò)可能會(huì)讓人感到恐懼,特別是對于新手機(jī)器學(xué)習(xí)的人來說。這篇文章主要介紹了基于Tensorflow搭建一個(gè)神經(jīng)網(wǎng)絡(luò)的實(shí)現(xiàn),從入門開始,感興趣的可以了解一下2021-05-05
python網(wǎng)絡(luò)編程之讀取網(wǎng)站根目錄實(shí)例
這篇文章主要介紹了python網(wǎng)絡(luò)編程之讀取網(wǎng)站根目錄實(shí)例,以quux.org站根目錄為例進(jìn)行了實(shí)例分析,代碼簡單易懂,需要的朋友可以參考下2014-09-09
哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程
這篇文章主要介紹了哈工大自然語言處理工具箱之ltp在windows10下的安裝使用教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

