Python Matplotlib scatter - Basic usage and how to change color and size of point

pyplot module in Python Matplotlib has several drawing methods. scatter is one of them.

from matplotlib import pyplot

x = 1
y = 3

pyplot.scatter(x, y)
pyplot.savefig('plot.png')

scatter plot one point (1, 3) in the plane. Note that savefig is a function that saves the graph Matplotlib has created.

Multiple points in plane

from matplotlib import pyplot

x = [1, 2, 3]
y = [7, -4, 5]

pyplot.scatter(x, y)
pyplot.savefig('plot.png')

(1, 7), (2, -4), (3, 5) are plotted.

Change color and point size

Color and size of point can be set in scatter.

from matplotlib import pyplot

x = [1, 2, 3]
y = [7, -4, 5]

a = [5, 6, 7, 8]
b = [-1, -2, -3, -4]

pyplot.scatter(x, y, c='green')
pyplot.scatter(a, b, s=500, c='red')

pyplot.savefig('plot.png')

Comments

Powered by Markdown