Thursday, January 22, 2026
HomeLanguagesPyCairo – Drawing different Pen dashes

PyCairo – Drawing different Pen dashes

Each line can be drawn with different pen dashes. A pen dash can be defined as the style of a line. The dash pattern is specified by the set_dash( ) method. The pattern is set by the dash list which of floating value. They can set the on and off part of the dashes in pattern. The dashes is used by the stroke( ) method to create a line pattern. If the number of dashes is 0, dashing is disabled and a plane simple line will be drawn. If the number of dashes is 1, a symmetric pattern is assumed with alternative on and off portion of the size specified as the single values in dash.

Steps of Implementation :

  1. Import the Pycairo module.
  2. Create a SVG surface object and add context to it.
  3. Setting color of the context & line width
  4. Setting of line pattern using set_dash( )
  5. Creating a line.

Example 1 :

Python3




# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([4.0, 21.0, 2.0])
  
    # move the context to x,y position
    context.move_to(40, 30
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


context.set_dash ( [ 4.0, 21.0, 2.0 ] )

We have 4 points drawn, 21 not drawn, and 2 drawn, then 4 points not drawn, 21 points drawn. and 2 not drawn.

Output :

Example 2 :

Python3




# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([14.0, 6.0])
  
    # move the context to x,y position
    context.move_to(40, 30
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


context.set_dash ( [ 14.0, 6.0 ] )

In this pattern, we have always 14 points drawn and 6 not drawn.

Output :

Example 3 :

Python3




# importing pycairo
import cairo
  
# creating a SVG surface 
# here geekline is file name & 700, 700 is dimension
with cairo.SVGSurface("geekline.svg", 700, 700) as surface:
      
    # creating a cairo context object
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
      
    # setting of line width
    context.set_line_width(4)
  
    # setting of line pattern
    context.set_dash([1.0])
  
    # move the context to x,y position
    context.move_to(40, 30
      
    #creating a line
    context.line_to(250, 30)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


context.set_dash ( [ 1.0 ] )

we create a pen dash of a symmetric pattern of alternating single on and off point.

Output :

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS