Tuesday, November 19, 2024
Google search engine
HomeLanguagesHow to Change the Color of a Graph Plot using pygal?

How to Change the Color of a Graph Plot using pygal?

Prerequisites: pygal

Pygal is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. 
In this article, we will see how we can change chart color in the Pygal module. While making a chart it is important for us to adjust color The following methods are used for the creation of the graph and corresponding color change of the graph. Here we will see some examples to illustrate our point as per our requirement.

Approach:

  • Import required module.
  • Create a chart object.
  • Change the color of the chart.
  • Label the graph.
  • Display Graph.

Implementation of the concept discussed above is given below:

Example 1:

Python3




import pygal
from pygal.style import Style
  
# change graph color
custom_style = Style(
    colors=('#E80080', '#404040', '#9BC850', '#ffeb44', '#ff00ff'))
  
  
# creating Pie chart object
pie_chart = pygal.Pie(inner_radius=1, style=custom_style)
  
pie_chart.title = 'Number of people using social media (in million)'
pie_chart.add('Signal', 14.36)
pie_chart.add('Instagram', 26.01)
pie_chart.add('Twitter', 8.8)
pie_chart.add('Facebook', 30.32)
pie_chart.add('Youtube', 20.3)
  
pie_chart.render_to_png('aa.png')


Output

Example 2:

Python3




# importing pygal
import pygal
from pygal.style import Style
  
# change graph color
custom_style = Style(
    colors=('#ffebcd', '#daa520', '#9BC850', '#ffeb44', '#ff00ff'))
  
# creating Bar chart object
pie_chart = pygal.Bar(style=custom_style)
  
# chart data
pie_chart.add('Hindu', 13.36)
pie_chart.add('Muslim', 21.01)
pie_chart.add('Buddhist', 5.8)
pie_chart.add('Christian', 33.32)
pie_chart.add('Others', 26.3)
  
# naming the title
pie_chart.title = 'World religion population in 2007 (in %)'
  
pie_chart.render_to_png('aa.png')


Output

Example 3:

Python3




# importing pygal
import pygal
from pygal.style import Style
  
# change graph color
custom_style = Style(
    colors=('#ffebcd', '#daa520', '#9BC850', '#ffeb44', '#ff00ff'))
  
# creating Dot chart object
pie_chart = pygal.Dot(style=custom_style)
  
# chart data
pie_chart.add('Hindu', 13.36)
pie_chart.add('Muslim', 21.01)
pie_chart.add('Buddhist', 5.8)
pie_chart.add('Christian', 33.32)
pie_chart.add('Others', 26.3)
  
# naming the title
pie_chart.title = 'World religion population in 2007 (in %)'
  
pie_chart.render_to_png('aa.png')


Output

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

Most Popular

Recent Comments