English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Scatter Plot

Scatter plots are used to plot data points on horizontal and vertical axes to try to show the extent to which one variable affects another. Each row in the data table is represented by a marker, the position of which depends on the values set in the columns of X and Y axes. A third variable can be set to correspond to the color or size of the marker, thereby adding another dimension to the graph.

The following script draws a scatter plot with two different colored grade ranges and the grades of boys and girls.

Example code:

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math
 import seaborn as sns
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Original text from [Lidi Huo], commercial use please contact the author for authorization, non-commercial use please retain the original link:
 girls_grades = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]
 boys_grades = [30, 29, 49, 48, 100, 48, 38, 45, 20, 30]
 grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
 #plt.legend(labels=('Boy','Girl'),loc='upper left')
 plt.scatter(grades_range, girls_grades, color='r', alpha=0.5)
 plt.scatter(grades_range, boys_grades, color='b', alpha=0.5)
 plt.title('Scatter Plot Example')#Display chart title
 plt.xlabel('Score Range')#X-axis name
 plt.ylabel('Score Grade')#Y-axis name
 plt.grid(False)#Show grid lines
 plt.legend(labels=('Boy','Girl'),loc='upper right')
 plt.show()

Execute the above example code to get the following results -