Open In App

Pygal Dot Chart

Pygal library in Python is an open-source library used for data visualization. We can draw various interactive plots and charts using different datasets. For example, bar charts, line charts, pie charts, radar charts, etc.

Install Pygal in Python

To draw a dot chart using Pygal in Python. We need the Pygal library to be installed in Python so, install the Pygal library by executing the below command in the terminal or command prompt:



pip install pygal

What is Dot Chart in Pygal?

A dot chart, also known as a dot plot or strip plot is used to visualize the data points of a data set in a simple and effective way. It represents each data point as a dot along an axis. It is mostly used when we have to visualize small datasets.

Create Dot Chart using Pygal in Python

In this, we have drawn a basic dot chart of sample data “Monthly courses sales data“. We import the Pygal module and then create a dot chart object using pygal.Dot() method. Set the title to the dot chart and labels for the x-axis. After that add sales data to the dot chart using add() method by passing the label ‘Sales’ as the first argument and the list of data as the second argument. Finally, save the dot chart to an SVG file using the render_to_file() method of Pygal. In the output, we can see that dot chart and when we hover over any of the dots it shows the stats for that particular dot.






# Import Pygal
import pygal
 
# Create a dot chart object
dot_chart = pygal.Dot()
 
# Set title to dot chart
dot_chart.title = 'Monthly Courses Sales Data'
 
# Set labels to x-axis
dot_chart.x_labels = ['System Design',
                      'Java Backend',
                      'CIP',
                      'DSA self paced', 'CP']
 
# Adding data to dot chart
dot_chart.add('Sales', [105, 210, 390, 540, 198])
 
# Save dot chart to svg file
dot_chart.render_to_file('dotChart.svg')

Output:

Create a Dot Chart with Multiple Series using Pygal in Python

In this example, we will create a dot chart with multiple data series. Firstly we import the Pygal module using pygal.Dot() method with argument “x_label_rotation=30” that sets the rotation angle of the x-axis labels to 30 degrees to prevent overlapping if labels are too long. Set the title for the dot chart, x-axis, and y-axis, and then set the labels of the x-axis with a list of names. After that, we add the data of Aptitude, OOPs, and DSA using an add() method and finally, save the plot to dot_chart.svg file using render_to_file() function.




# import pygal module
import pygal
 
# Create a dot_chart object
dot_chart = pygal.Dot(x_label_rotation=30)
 
# Set title to chart, x-axis, and y-axis
dot_chart.title = 'GeeksforGeeks Hackathon results'
dot_chart.x_title = 'Students names'
dot_chart.y_title = 'Section'
 
# Set labels to x-axis
dot_chart.x_labels = ['Ramesh', 'Suresh',
                      'Carry', 'Tiger',
                      'Jonny', 'Lakhan',
                      'Sita', 'Krishna']
 
# Add data to Dot Chart
dot_chart.add('Aptitude', [69, 82, 75, 72, 64, 60, 21, 86])
dot_chart.add('OOPs', [74, 99, 77, 51, 61, 44, 97, 94])
dot_chart.add('DSA', [34, 29, 42, 59, 58, 28, 90, 69])
 
# Save file to SVG
dot_chart.render_to_file('dot_chart.svg')

Output:

Create Dot Chart with Negatives using Pygal in Python

In this example, we draw a dot chart with negative values, Firstly, we create margin data of different courses stored in a Python dictionary. Create a dot chart object “dot_chart_margins” and then add data to the dot chart using add() method by traversing over the dictionary “margins”. Add a title to the dot chart, x-axis, and y-axis. Finally, save the dot chart to the ‘course_margins.svg’ file. The negative values are represented by holo circles in the dot chart.




# Import Pygal module
import pygal
 
# Sample data for positive and negative values dot chart
margins = {
    'Java Backend': -10,
    'System Design': 5,
    'DSA Self Paced': 15,
    'CP': -5,
    'CIP': 8
}
 
# Create a dot chart object
dot_chart_margins = pygal.Dot()
 
# Add data to the dot chart
for department, profit_margin in margins.items():
    dot_chart_margins.add(department, profit_margin)
 
# Customize the chart and set titles
dot_chart_margins.title = 'Profit Margins of Courses'
dot_chart_margins.x_title = 'Courses'
dot_chart_margins.y_title = 'Profit Margin (%)'
 
# Save the dot chart to a file
dot_chart_margins.render_to_file('course_margins.svg')

Output:


Article Tags :