Scatter plot in Python using matplotlib

In this Tutorial we will learn how to create Scatter plot in python with matplotlib. This python Scatter plot tutorial also includes the steps to create scatter plot by groups in which scatter plot is created for different groups.


Create Scatter plot in Python:

This example we will create scatter plot for weight vs height

import matplotlib.pyplot as plt


plt.xlabel('weight', fontsize=16)
plt.ylabel('height', fontsize=16)
plt.title('scatter plot - height vs weight',fontsize=20)

Line 1: Imports the pyplot function of matplotlib library in the name of plt.
Line 3 and Line 4: Inputs the arrays to the variables named weight1 and height1.
Line 6: scatter function which takes takes x axis (weight1) as first argument, y axis (height1) as second argument, colour is chosen as blue in third argument and marker=’o’ denotes the type of plot, Which is dot in our case.
Line 7 and Line 8: x label and y label with desired font size is created.
Line 9 and Line 10: Mentions the Chart Title with font size and scatter plot is shown.

scatter plot in python 1


Different values for markers and their representation is shown below.

Marker meaning
. point
, pixel
o circle
v triangle_down
^ triangle_up
8 octagon
s sqaure
p pentagon
* star
h hexagon
+ plus
D diamond


Create Scatter plot by Groups in Python:

Example of scatter plot for three different groups

import matplotlib.pyplot as plt
import numpy as np





color_array = ['b'] * 15 + ['g'] * 17 + ['r']*15

plt.scatter(weight, height, marker='*', c=color_array)

plt.xlabel('weight', fontsize=16)
plt.ylabel('height', fontsize=16)
plt.title('grouped scatter plot - height vs weight',fontsize=20)
  • weight1, weight2 and weight3 is concatenated to form a weight array.
  • height1, height2 and height3 is concatenated to form a height array.
  • Corlor_array with first 15 blue color, second 15 green color and last 15 red color is created.
  • All the above three arguments along with the marker=’*’ is passed to Scatter function.

Which results in plotting the scatter plot of 3 different groups with 3 different colors with ‘*’ being used as plot.  So the resultant chart will be

fix grouped scatter plot in matplotlib 7


previous scatter plot in python                                                                                                          next scatter plot in python

Do NOT follow this link or you will be banned from the site!