Related Articles

Related Articles

PyCairo – Drawing different Pen dashes
  • Last Updated : 12 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


context.set_dash ( [ 14.0, 6.0 ] )

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

Output :



Example 3 :

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


context.set_dash ( [ 1.0 ] )

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

Output :


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :