SoFunction
Updated on 2024-11-10

Python using the pylab library to implement the line drawing function in detail

This article example describes how Python uses the pylab library to realize the function of drawing lines. Shared for your reference, as follows:

pylab provides powerful drawing functions, but there are many functions and parameters, it is easy to get confused. We usually use the most should be drawing lines. Below, a simple package of some commonly used line drawing functions, easy to use.

# -*- 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) :
     = ('figsize',None)
     = ('axis',None)
     = ('title','NoName')
     = ('ylabel','NoName')
     = ('grid',False)
    self.xaxis_locator = ('xaxis_locator',None)
    self.yaxis_locator = ('yaxis_locator',None)
    self.legend_loc = ('legend_loc',0)
    if  != None :
      (figsize = )
    if  != None :
      ()
    ()
    ()
    ax = ()
    ()
    if self.xaxis_locator != None :
      .set_major_locator( (self.xaxis_locator) )
    if self.yaxis_locator != None :
      .set_major_locator( (self.yaxis_locator) )
     = []
     = 1
  def addline(self, lineConf) :
    ((, lineConf))
     += 1
    return {'id' :  - 1}
  def removeline(self, lineId) :
    for i in range(len()) :
      id, conf = [i]
      if id == lineId :
        del [i]
        break
    else :
      return {'status' : -1}
    print len()
    return {'status' : 0}
  def __parselineConf(self, lineConf) :
    X = lineConf['X']
    Y = lineConf['Y']
    marker = ('marker',None)
    color = ('color', ())
    markerfacecolor = ('markerfacecolor',color)
    label = ('label','NoName')
    linewidth = ('linewidth',1)
    linestyle = ('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)
    (X, Y, marker = marker, color = color, markerfacecolor = markerfacecolor, label=label, linewidth = linewidth, linestyle = linestyle)
    (loc = self.legend_loc)
  def plot(self) :
    colors = [[i % len()] for i in range(len())]
    for i in range(len()) :
      id, conf = [i]
      if ('color',None) :
        conf['color'] = colors[i]
      X, Y, marker, color, markerfacecolor, label, linewidth, linestyle = self.__parselineConf(conf)
      (X, Y, marker = marker, color = color, markerfacecolor = markerfacecolor, label=label, linewidth = linewidth, linestyle = linestyle)
    (loc = self.legend_loc)
  def show(self) :
    ()
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 = [(1,10) for i in range(10)]
  Y2 = [(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' : '--'
  }
  #(lineConf)
  print (lineConf)
  print (lineConf2)
  #print (1)
  ()
  ()

The running result is shown below:

Attachment:Cited from: /site/guyingbo/matplotlib%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0

Line Properties.

The color (abbreviated as c):

Blue: 'b' (blue)
Green: 'g' (green)
Red: 'r' (red)
Blue-green (dark green): 'c' (cyan)
Reddish purple (magenta): 'm' (magenta)
Yellow: 'y' (yellow)
Black: 'k' (black)
White: 'w' (white)
Gray scale representation: . 0.75 (any floating point number in [0,1])
RGB representation: . '#2F4F4F' or (0.18, 0.31, 0.31)
Any legal color representation in html: . 'red', 'darkslategray'
Line style (abbreviated as ls):

Solid line: '-'
Dotted line: '--'
Dotted line: '-.'
Dotted line: ':'
Dot: '.'
Point type (markersmarker):

Pixels: ','
Circle: 'o'
Upper triangle: '^'
Lower Triangle: 'v'
Left triangle: '<'
Right triangle: '>'
Square: 's'
Plus sign: '+'
Fork: 'x'
Prism: 'D'
Fine prism: 'd'
Tripod down: '1' (that's yah)
Tripod facing up: '2'
Tripod facing left: '3'
Tripod facing right: '4'
Hexagon: 'h'
Rotating Hexagon: 'H'
Pentagon: 'p'
Vertical line: '|'
Horizontal line: '_'
Steps in gnuplot: 'steps' (only in kwarg)
The markersize (abbreviated as ms):

markersize: real number
Markeredgewidth (abbreviated as mew):

markeredgewidth: real number
The markeredgecolor (abbreviated as mec):

markeredgecolor: any value from the color options
The markerfacecolor (abbreviated as mfc):

markerfacecolor: any value from the color options
Transparency (alpha):

alpha: floating point number between [0,1].
Linewidth:

linewidth: real number

Readers interested in more Python related content can check out this site's topic: theSummary of Python image manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.