6.10. Scatter Plot

  • Used to show correlation

6.10.1. Simple Points

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [1, 2, 3, 4]

plt.plot(x, y, 'o')
plt.show()
../../_images/chart-scatter-points.png

6.10.2. Point Scatter

import matplotlib.pyplot as plt


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

plt.scatter(x, y, label='my points', color='black')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot')

plt.show()
../../_images/simple-scatter-1.png

6.10.3. Star Markers

import matplotlib.pyplot as plt


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

plt.scatter(
    x=x,
    y=y,
    label='my points',
    color='black',
    marker='*',        # type of the points
    s=100,             # size
)

plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot')

plt.show()
../../_images/simple-scatter-2.png

6.10.4. Cross markers

import matplotlib.pyplot as plt


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

plt.scatter(
    x=x,
    y=y,
    label='my points',
    color='black',
    marker='x',        # type of the points
    s=50,             # size
)

plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot')

plt.show()
../../_images/simple-scatter-3.png

6.10.5. Examples

import matplotlib.pyplot as plt


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

plt.scatter(x, y, marker='*', color='red', s=500)   # ``s`` = size
plt.scatter(y, x, marker='o', color='blue')

plt.show()
../../_images/matplotlib-plt-scatter.png

6.10.5.1. Multiple Points

Listing 744. Multiple Points
import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')
plt.show()
../../_images/chart-scatter-multiple.png

6.10.5.2. Points with Labels

Listing 745. Points with Labels
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y, 'ro')

# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')

# Pad margins so that markers don't get clipped by the axes
plt.margins(0.2)

# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()
../../_images/chart-scatter-labels.png