![]() |
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 PlotIn 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
Output: Scatter Plot with Customized StyleIn 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
Output: Scatter Plot with Single Series using PygalIn 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
Output: |
Reffered: https://www.geeksforgeeks.org
Python |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |