设为首页 收藏本站
查看: 1069|回复: 0

[经验分享] 使用python pylab库 画线

[复制链接]

尚未签到

发表于 2015-4-23 08:36:45 | 显示全部楼层 |阅读模式
  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
45             if id == lineId :
46                 del self.lineList
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
73             if conf.get('color',None) :
74                 conf['color'] = colors
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()
  
  
DSC0000.png

附:引用自: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: 实数

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59821-1-1.html 上篇帖子: Python的基础语法 下篇帖子: 利用google的API获取世界城市经纬度(python实现)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表