Python-based Pylab library Complete example of line drawing function

Source: Internet
Author: User
Tags color representation dashed line
This article mainly introduces the method that Python uses Pylab library to realize the line-drawing function, and analyzes the operation skill of Python using the correlation function of Pylab library to realize the function of drawing line, with the function of correlation function and parameter, and the need of friends can refer to

The examples in this paper describe how Python uses the Pylab library to implement the line drawing function. Share to everyone for your reference, as follows:

Pylab provides powerful drawing capabilities, but functions and parameters are much more easily confused. What we usually use most is to draw a line. Below, simple to some commonly used the dash function to encapsulate, is convenient to use.


#-*-Coding:utf-8-*-import pylabimport randomclass miniplottool: ' A mini tool to draw lines using Pylab ' ' bas ecolors = [' Red ', ' green ', ' yellow ', ' blue ', ' black ', ' cyan ', ' Magenta '] def __init__ (self, baseconfig): Self.figsize = Bas    Econfig.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 = basec Onfig.get (' Xaxis_locator ', none) Self.yaxis_locator = Baseconfig.get (' Yaxis_locator ', none) Self.legend_loc = baseConf      Ig.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.gr ID) 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 RA     Nge (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, Lin econf): 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.G ET (' 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, MA Rker = 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.basec      Olors)] 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 = MARKERFACEC    Olor, 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], # ' Tit    Le ': ' Hello title ',# ' Ylabel ': ' Hello Ylabel ', ' grid ': True, # ' Xaxis_locator ': 0.5, # ' Yaxis_locator ': 1, # ' Legend_loc ': ' Uppe  R right '} tool = Miniplottool (baseconfig) X = [I-I in range] Y = [Random.randint (1,10)-I in range (10)]     Y2 = [Random.randint (1,10) for I in range] 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) #pri NT Tool.removeline (1) tool.plot () tool.show ()

Run as follows:

attached: referenced from: https://sites.google.com/site/guyingbo/matplotlib%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0

Line properties:

Color (abbreviated as C):

Blue: ' B ' (blue)
Greens: ' G ' (green)
Red: ' R ' (red)
Turquoise (dark green): ' C ' (cyan)
Red Purple (Magenta): ' m ' (magenta)
Yellow: ' Y ' (yellow)
Black: ' K '
White: ' W '
Grayscale representation: e.g. 0.75 (any floating-point number in [0,1])
RGB notation: e.g. ' #2F4F4F ' or (0.18, 0.31, 0.31)
Color representation in any valid HTML: e.g. ' red ', ' Darkslategray '
Linetype (LineStyle abbreviated to LS):

Solid line: '-'
Dashed line: '--'
Imaginary dot line: '-. '
Dot line: ': '
Point: '. '
Point type (Mark Marker):

Pixels: ', '
Circle: ' O '
Upper triangle: ' ^ '
Lower triangle: ' V '
Left triangle: ' < '
Right triangle: ' > '
Square: ' s '
Plus: ' + '
Fork shape: ' x '
Prism: ' D '
Fine prism: ' d '
Tripod facing down: ' 1 ' (yes Ya)
Tripod facing up: ' 2 '
Tripod facing left: ' 3 '
Tripod facing right: ' 4 '
Hex shape: ' H '
Rotation six angle: ' H '
V-shaped: ' P '
Vertical line: ' | '
Horizontal line: ' _ '
Steps in Gnuplot: ' Steps ' (can only be used in Kwarg)
Mark Size (Markersize abbreviated as MS):

Markersize: Real numbers
Mark Edge Width (markeredgewidth abbreviated to MEW):

Markeredgewidth: Real numbers
Mark Edge Color (markeredgecolor abbreviated to MEC):

Markeredgecolor: Any value in the color option
Mark Surface Color (markerfacecolor abbreviated to MFC):

Markerfacecolor: Any value in the color option
Transparency (Alpha):

Alpha: Floating-point number between [0,1]
Line width (linewidth):

LineWidth: Real numbers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.