Friday, October 10, 2025
HomeLanguagesHow to limit the width and height in Pygal?

How to limit the width and height in Pygal?

Prerequisites: pygal

Pygal is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. While making a plot it is important for us to optimize its size. In this article, we will see how we can resize the plot window in the Pygal module. Here are various ways to change the default plot size as per our required dimensions or resize a given plot. 

Approach:

  • Import required module.
  • Create a chart object.
  • Pass width/height in function.
  • Label the graph.
  • Display Graph.

Implementation of the concept discussed above is given below:

Example 1: 

Python3




# importing pygal
import pygal
import numpy
  
# creating the chart object
chart = pygal.Bar(width=200, height=200)
  
# naming the title
chart.title = 'Bar Chart'
  
# Random data
chart.add('A', numpy.random.rand(2))
chart.add('B', numpy.random.rand(2))
chart.add('C', numpy.random.rand(2))
  
chart.render_to_png('img.png')


Output:

Example 2:

Python3




# importing pygal
import pygal
import numpy
  
# creating the chart object
chart = pygal.Line(width=400, height=200)
  
# naming the title
chart.title = 'Line Chart'
  
# Random data
chart.add('A', numpy.random.rand(2))
chart.add('B', numpy.random.rand(2))
chart.add('C', numpy.random.rand(2))
  
chart.render_to_png('img.png')


Output:

Example 3:

Python3




# importing pygal
import pygal
import numpy
  
# creating the chart object
chart = pygal.HorizontalBar(width=300, height=300)
  
# naming the title
chart.title = 'HorizontalBar Chart'
  
# Random data
chart.add('A', numpy.random.rand(2))
chart.add('B', numpy.random.rand(2))
chart.add('C', numpy.random.rand(2))
  
chart.render_to_png('img.png')


Output:

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

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS