Skip to content
Home » matplotlib Part 18 – Scatter Plots with the scatter Method

matplotlib Part 18 – Scatter Plots with the scatter Method

Spread the love

Scatter Plots with the scatter Method

In the previous article we were using the plot method to create scatter plots. But there’s a specialized and more powerful method to do that. In this article we’re going to be using it. Let’s have a look at the example from the previous article:

In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 40)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y, 'o')
Out[1]:
[<matplotlib.lines.Line2D at 0x1c4ac899688>]

Now let’s use the scatter method instead of the plot method. Here we must explicitly specify what the third argument is:

In [2]:
fig, ax = plt.subplots()
ax.scatter(x, y, marker='o')
Out[2]:
<matplotlib.collections.PathCollection at 0x1c4ae112188>

You can also use other markers:

In [3]:
fig, ax = plt.subplots()
ax.scatter(x, y, marker='s')
Out[3]:
<matplotlib.collections.PathCollection at 0x1c4ae116108>

Unlike the plot method, scatter lets us control each point individually.

In [4]:
# Here's how we can control the sizes of the points. Let's define a list 
# of sizes from smallest to greatest using a list comprehension.
sizes = [i for i in range(40)]

# And now let's use the sizes to plot the function.
fig, ax = plt.subplots()
ax.scatter(x, y, s=sizes)
Out[4]:
<matplotlib.collections.PathCollection at 0x1c4aed82688>

Here’s another example:

In [6]:
# Let's randomize the sizes. This code will produce a different output each time you execute it.
sizes = [i for i in 100 * np.random.randn(10)]
fig, ax = plt.subplots()
ax.scatter(x, y, s=sizes, marker='<')
Out[6]:
<matplotlib.collections.PathCollection at 0x1c4aeeabdc8>

We can also control the colors of the markers:

In [7]:
# Let's create a list of colors and use it in the scatter method.
# Here we're using 4 colors. As there are 40 points, we multiply  
# the list by 10 to repeat its elements 10 times, thus getting 
# 40 elements altogether.
colors = 10 * ['b', 'r', 'g', 'y']
fig, ax = plt.subplots()
ax.scatter(x, y, c=colors)
Out[7]:
<matplotlib.collections.PathCollection at 0x1c4aeee9bc8>

Here’s an example with random colors:

In [8]:
# This code will produce a different output each time you execute it.
colors = np.random.randn(40)
fig, ax = plt.subplots()
ax.scatter(x, y, c=colors)
Out[8]:
<matplotlib.collections.PathCollection at 0x1c4aef5ff08>

You can also set the alpha value:

In [9]:
# Here the points are pretty big, so you can watch them overlap.
fig, ax = plt.subplots()
ax.scatter(x, y, marker='o', s=1000, alpha=.5)
Out[9]:
<matplotlib.collections.PathCollection at 0x1c4aefce308>

If you can achieve your goal with the plot method, you should choose it over scatter because it’s more efficient. It’s especially important for large datasets.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply