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: