Open In App

PyCairo- Drawing Function curve

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will learn how we can draw a simple function curve using PyCairo in python. n mathematics, the graph of a function f is the set of ordered pairs, where f(x) = y. In the common case where x and f(x) are real numbers, these pairs are Cartesian coordinates of points in two-dimensional space.

PyCairo : Pycairo is a Python module providing bindings for the cairo graphics library.This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read only) is with a modern web browser like Chrome, Firefox, Edge, or Internet Explorer—nearly all of them should provide some sort of rendering support for the SVG format.  

SVG file is a graphics file that uses a two-dimensional vector graphic format created by the World Wide Web Consortium (W3C). It describes images using a text format that is based on XML. SVG files are developed as a standard format for displaying vector graphics on the web.

Steps of Implementation :

  1. Import the Pycairo & Math module.
  2. Create a SVG surface object and add context to it.
  3. Creating list for creating points for functional curve
  4. Traversing and plotting Points
  5. Setting color of the context & line width

Below is the Implementation :

Python3




# importing pycairo
import cairo
# importing Math to Sin & exp Function
import math
 
# Variable X
x = 0
# Creating list for creating points
points = []
# Creating Loop for points
while x < 5:
    # Creating point corresponding x
    y = math.sin(10*x)*math.exp(-x/2)
    # Adding points to the list
    points.append((x*100 + 50, y*100 + 200))
    # Incrementing by 0.01 Variable x
    x += 0.01
 
# creating a SVG surface
# here geek95 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
 
    # creating a cairo context object for SVG surface
    # using Context method
    context = cairo.Context(surface)
 
    context.move_to(*points[0])
    # Traversing Points
    for p in points[1:]:
        # Plotting point
        context.line_to(*p)
 
    # setting width of the context
    context.set_line_width(2)
    # setting color of the context
    context.set_source_rgb(0.2, 1, 0.2)
    # stroke out the color and width property
    context.stroke()
 
# printing message when file is saved
print("File Saved")


Output :



Similar Reads

PyCairo - Drawing Bezier curve
In this article we will learn how we can draw a simple bezier curve using PyCairo in python. A Bezier curve is a versatile mathematical curve that can be used to create a wide variety of different shapes in vector graphics. PyCairo : Pycairo is a Python module providing bindings for the cairo graphics library.This library is used for creating SVG i
2 min read
PyCairo - Drawing different type of line caps
In this article, we will learn how we can draw different line caps types using PyCairo in python. The line caps are endpoints of lines. Steps of Implementation : Import the Pycairo module.Create a SVG surface object and add context to it.Setting color of the context &amp; line widthSetting of line cap style using set_line_cap( )Creating a line. The
3 min read
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 t
3 min read
PyCairo - Drawing different type of line Joins
In this article, we will learn how lines can be joined using different join styles like Bevel, Round, and Miter in python using PyCairo. Pycairo is a Python module providing bindings for the Cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read-onl
3 min read
PyCairo - Drawing the Roundrect
In this article, we will learn how we can draw a simple Roundrect shape using PyCairo in python. A roundrect is a rectangle with rounded corners PyCairo : Pycairo is a Python module providing bindings for the cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to
2 min read
PyCairo - Drawing the outline
In this article, we will learn how we can draw an outline to a solid surface using PyCairo in python. The outline is basically a border to the object, it is used to highlight the object. PyCairo: Pycairo is a Python module providing bindings for the Cairo graphics library. This library is used for creating SVG i.e vector files in python. The easies
3 min read
PyCairo - Drawing negative arc
In this article we will learn how we can draw a simple negative arc using PyCairo in python. Adds a circular arc of the given radius to the current path. The arc is centered at (xc, yc), begins at angle1 and proceeds in the direction of decreasing angles to end at angle2. If the angle2 is greater than angle1 it will be progressively decreased by 2π
2 min read
Creating SVG Image using PyCairo
In this article we will see how we can create SVG file using PyCairo in Python. SVG : An SVG file is a graphics file that uses a two-dimensional graphic vector format that defines images using an XML-based text format. As a standard format for showing vector graphics on the web, SVG files are developed. PyCairo : It is a Python module providing bin
2 min read
Pycairo - Creating different shapes
In this article we will see how to create different shapes using Pycairo in Python. In pycairo there are mainly two shapes rectangle and the arch which is used to make other shapes like square or semi-circle. Polygon shapes are created with the help of lines. An SVG file is a graphics file that uses a two-dimensional graphic vector format that defi
2 min read
PyCairo - Saving SVG Image file to PNG file
In this article, we will see how we can save an SVG file to a PNG file using PyCairo in Python. We can create an SVG file using SVGSurface method. An SVG file is a graphics file that uses a two-dimensional vector graphic format created by the World Wide Web Consortium (W3C). It describes images using a text format that is based on XML. SVG files ar
2 min read
Article Tags :
Practice Tags :