A scatter plot is used to visualize data where data points are used to show the relation between the variables that are placed between an X and Y-axis. When these data points are plotted on a graph they look scattered therefore named scatter plot. In Python, we can plot scatter plots using numerous libraries available in Python. In this article, we will learn how to plot scatter plots using the Pygal library in Python.
Required Library
To draw a Scatter plot 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
Pygal Scatter Plot
In the below code, we have drawn a scatter plot with a single series of sample data using the Pygal library in Python. Firstly, import the Pygal module and then create a scatter plot object using pygal.XY() method with 'stroke=False' so that data points are not connected through a line. Set the scatter plot title, x-axis title, and y-axis title. After that create a sample data of "Happiness score VS Income per person" and add this data to the scatter plot using add() method. Finally, save the plot to an SVG file with the name "scatter_plot.svg" using the render_to_file() method.
Python3
# import pygal module
import pygal
# Create scatter plot object
scatter_plot = pygal.XY(stroke=False)
# Set title for scatter plot
scatter_plot.title = 'Happiness Index VS Income per person'
# Set title for x-axis and y-axis
scatter_plot.x_title = 'Average Happiness score'
scatter_plot.y_title = 'Average Income per person'
# Create sample data
data = [(10, 20000), (20, 39000),
(30, 43000), (40, 55000),
(50, 60000), (40,20000),
(70, 95000), (80, 79000),
(55, 55000), (65, 75000),
(85, 98000), (45,35000)]
# Add data to scatter plot
scatter_plot.add('Series', data)
# Save plot to a file
scatter_plot.render_to_file('scatter_plot.svg')
Output:

Scatter Plot with Customized Style
In this, we will customize the theme of the scatter from the default theme(Light theme) to the dark theme. We take the example of employee age vs salary scatter plot of HR and sales department so create a sample data for these and add the data to the scatter plot using add() method of pygal method. After that set the titles for the x-axis and y-axis and change the scatter plot theme using "scatter_chart.style = pygal.style.DarkStyle". Finally, save the plot to an SVG file with the name "scatterPlot.svg" using the render_to_file() method.
Python3
# Import pygal module
import pygal
# Create a scatter plot object
# XY for scatter plot, stroke=False
# to disable line connecting points
scatter_chart = pygal.XY(stroke=False)
# Set title of Graph
scatter_chart.title = 'Employee Salary Vs Age graph'
# Create a Sample data for the scatter plot
hr_data = [(10, 20000), (25, 30000), (30, 40000), (40, 50000), (53, 60000)]
sales_data = [(14, 10000), (20, 25000), (35, 30000), (44, 40000), (50, 55000)]
# Adding data to scatter plot
scatter_chart.add('HR Dept', hr_data)
scatter_chart.add('Sales Dept', sales_data)
# Set title to x-axis and y-axis
scatter_chart.x_title = 'Age'
scatter_chart.y_title = 'Salary'
# Customized the graph theme from light to dark
scatter_chart.style = pygal.style.DarkStyle
# Save the scatter plot to file
scatter_chart.render_to_file('scatterPlot.svg')
Output:

Scatter Plot with Single Series using Pygal
In the below code, we have drawn a scatter plot of sample data using the Pygal library methods. First, we imported the Pygal library. We define sample data with some values of the x and y-axis to be plotted on the graph and create the scatter plot object using pygal.XY() method with 'stroke=False' so that data points are not connected through a line. Plot all the data points on the plot using a for loop that iterates over the array 'data'. Now, Set the title of the plot, the x-axis title, the y-axis title, the legend of the plot, and the x-axis labels. Finally, save the output as 'scatter_plot.svg' using the render_to_file() method.
Python3
import pygal
# Create a Sample data for the scatter plot
data = [
{'x': 1, 'y': 5},
{'x': 2, 'y': 3},
{'x': 3, 'y': 7},
{'x': 4, 'y': 2},
{'x': 5, 'y': 8},
{'x': 6, 'y': 4},
{'x': 7, 'y': 6},
{'x': 8, 'y': 9},
{'x': 9, 'y': 2},
{'x': 10, 'y': 5}
]
# Create a scatter plot object
# XY for scatter plot, stroke=False
# to disable line connecting points
scatter_plot = pygal.XY()
# Add data to the scatter plot
for point in data:
scatter_plot.add('Data',
[(point['x'],
point['y'])])
# Customize the chart
scatter_plot.title = 'Sample Scatter Plot'
scatter_plot.x_title = 'X-axis'
scatter_plot.y_title = 'Y-axis'
# Disable the legend
scatter_plot.show_legend = False
# Set custom labels for the X-axis
scatter_plot.x_labels = map(str, range(1, 11))
# Save the scatter plot to a file in svg format
scatter_plot.render_to_file('scatter_plot.svg')
# Below line to used render the plot in the browser
# scatter_plot.render_in_browser()
Output:

Similar Reads
Scatter Plot
Scatter plot is a mathematical technique that is used to represent data. Scatter plot also called a Scatter Graph, or Scatter Chart uses dots to describe two different numeric variables. The position of each dot on the horizontal and vertical axis indicates values for an individual data point. In th
7 min read
PyQtGraph - Scatter Plot Graph
In this article we will see how we can create a scatter plot graph using PyQtGraph module. Â PyQtGraph is a graphics and user interface Python library for functionalities commonly required in designing and science applications. Its provides fast, interactive graphics for displaying data (plots, video
3 min read
Matplotlib Scatter
matplotlib.pyplot.scatter() is used to create scatter plots, which are essential for visualizing relationships between numerical variables. Scatter plots help illustrate how changes in one variable can influence another, making them invaluable for data analysis.A basic scatter plot can be created us
4 min read
Python Altair - Scatter Plot
In this article, we will learn a Simple Scatter plot with Altair using python. Altair is one of the latest interactive data visualizations library in python. Altair is based on vega and vegalite- A grammar of interactive graphics. Â Here we will import the Altair library for using it. And then we wil
2 min read
Pygal Box Plot
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 PythonPygal library in Python is an open-source library used
5 min read
What Is a Scatter Plot in Python?
Scatter plots are a fundamental tool in data visualization, providing a visual representation of the relationship between two variables. In Python, scatter plots are commonly created using libraries such as Matplotlib and Seaborn. This article will delve into the concept of scatter plots, their appl
6 min read
Scatter Plot in MATLAB
Scatter Plot is a popular type of graph plot that plots pairs of coordinate points discretely rather than continuously. These plots are extensively used in various industries such as the data science industry because of their statistical importance in visualizing vast datasets. Scatter Plots in MAT
3 min read
PyQtGraph â Getting Data Scatter Plot Graph
In this article, we will see how we can get the data of the scatter plot graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactiv
3 min read
Scatter plots in R Language
A scatter plot is a set of dotted points representing individual data pieces on the horizontal and vertical axis. In a graph in which the values of two variables are plotted along the X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them. R - Scatter plots We can
4 min read
Scatter plot using Plotly in Python
Plotly Python is a library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. Plotly python is an interactive visualization
5 min read