Skip to content
Home » matplotlib Part 17 – Scatter Plots with the plot Method

matplotlib Part 17 – Scatter Plots with the plot Method

Spread the love

Scatter Plots with the plot Method

So far we’ve been only using line plots. But there’s a great variety of plots we can use in matplotlib. Let’s first talk about scatter plots. In this article we’ll see how to use the plt.plot method to graph scatter plots and in the next article we’ll use the plt.scatter method.

So, let’s jump in.

In scatter plots the points are not joined by segments. They are drawn individually as dots or other shapes.

Here’s an example of a line plot, to start with:

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

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

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

If you pass a third argument to the plot method, you can easily create a scatter plot. Let it consist of circles:

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

Here we passed ‘o’ as the argument, which is a circle marker. To better see the individual points, let’s cut down their number:

In [3]:
x = np.linspace(0, 10, 40)
y = np.sin(x)

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

There are lots of other markers available, let’s check out some of them:

In [4]:
# point marker
fig, ax = plt.subplots()
ax.plot(x, y, '.')
Out[4]:
[<matplotlib.lines.Line2D at 0x21f2939e788>]
In [5]:
# pixel marker
fig, ax = plt.subplots()
ax.plot(x, y, ',')
Out[5]:
[<matplotlib.lines.Line2D at 0x21f293ffa48>]
In [6]:
# triangle_up marker
fig, ax = plt.subplots()
ax.plot(x, y, '^')
Out[6]:
[<matplotlib.lines.Line2D at 0x21f2947bc08>]
In [7]:
# tri_down marker
fig, ax = plt.subplots()
ax.plot(x, y, '1')
Out[7]:
[<matplotlib.lines.Line2D at 0x21f2944e988>]
In [8]:
# square marker
fig, ax = plt.subplots()
ax.plot(x, y, 's')
Out[8]:
[<matplotlib.lines.Line2D at 0x21f29558408>]
In [9]:
# pentagon marker
fig, ax = plt.subplots()
ax.plot(x, y, 'p')
Out[9]:
[<matplotlib.lines.Line2D at 0x21f295ecf48>]
In [10]:
# plus marker
fig, ax = plt.subplots()
ax.plot(x, y, '+')
Out[10]:
[<matplotlib.lines.Line2D at 0x21f296378c8>]
In [11]:
# x marker
fig, ax = plt.subplots()
ax.plot(x, y, 'x')
Out[11]:
[<matplotlib.lines.Line2D at 0x21f296a3308>]
In [12]:
# diamond marker
fig, ax = plt.subplots()
ax.plot(x, y, 'D')
Out[12]:
[<matplotlib.lines.Line2D at 0x21f2970de08>]

If you don’t like the color of the marker, you can combine the color symbol with the marker. For example, here’s how we can produce red square markers:

In [13]:
# red square marker
fig, ax = plt.subplots()
ax.plot(x, y, 'rs')
Out[13]:
[<matplotlib.lines.Line2D at 0x21f29776b48>]

Using the plot method you can keep the line in addition to the markers. You just have to add a hyphen before the marker symbol or the color symbol:

In [14]:
# line + green diamond marker
fig, ax = plt.subplots()
ax.plot(x, y, '-gD')
Out[14]:
[<matplotlib.lines.Line2D at 0x21f297e1988>]

You can pass some more arguments to the plot method to style the markers. Here are some examples:

In [15]:
# markersize
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=15)
Out[15]:
[<matplotlib.lines.Line2D at 0x21f29841a48>]
In [16]:
# markerfacecolor and markeredgecolor
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=15, markerfacecolor='y', markeredgecolor='r')
Out[16]:
[<matplotlib.lines.Line2D at 0x21f298a3508>]
In [17]:
# markeredgewidth
fig, ax = plt.subplots()
ax.plot(x, y, 'o', markersize=15, markerfacecolor='y', markeredgecolor='r', markeredgewidth=6)
Out[17]:
[<matplotlib.lines.Line2D at 0x21f2a8f04c8>]

There are lots of other options. Just check out the documentation to learn more.

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