Scatter plots in Matplotlib – Data Visualization using Python

Scatter plots are one of the basic graphs used for data visualization. Scatter plot has following options, only first two are mandatory, rest all are optional.

plt.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)

Here is simple program using scatter plot

################################################################################################
# name: scatterplot-00.py
# desc: Simple scatter plot
# date: 2018-06-15
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
x= np.arange(1,17,1)
# y= np.arange(2,34,2)
y= np.random.randint(2, 50,size=16)
plt.scatter(x,y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Jagur")
# labels
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is scatter plot using matplotlib')
plt.legend(loc=2)
# Saving image
plt.savefig('scarrerplot-00.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
scatterplot-00.py
hosted with ❤ by GitHub

Output of this program is as below

Here is a bit complex scatter plot using matplotlib

################################################################################################
# name: scatterplot-01.py
# desc: Complex Scatter plot
# date: 2018-06-15
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
x= np.arange(1,17,1)
y1= np.random.randint(2, 50,size=16)
y2= np.random.randint(2, 50,size=16)
plt.scatter(x,y1,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Jagur")
plt.scatter(x,y2,s=None, marker='*',color='r',edgecolors='r',alpha=0.9,label="Range Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is scatter plot using matplotlib')
plt.legend(loc=2)
# Saving image
plt.savefig('scarrerplot-01.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')

view raw
scatterplot-01.py
hosted with ❤ by GitHub

Output of above program

 

You can read documentation about scatter plot here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.