Abhinav Goel
Abhinav Goel

Reputation: 421

How to highlight one point in a matplotlib plot

Suppose, I have the following two lists that correspond to x- and y-coordinates.

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

I want the first pair (1,3) to be in a different color or shape.

How can this be done using python?

Upvotes: 25

Views: 49823

Answers (2)

cottontail
cottontail

Reputation: 23509

More flexibility is offered with scatter() call where you can change marker style, size and color more intuitively (e.g. D for diamond).

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

plt.scatter(x[1:], y[1:], c='blue')
plt.scatter(x[0], y[0], c='red', marker='D', s=100);

img

# you can even write text as a marker
plt.scatter(x[0], y[0], c='red', marker=r'$\tau$', s=100);

If the point to highlight is not the first point, then a filtering mask might be useful. For example, the following code highlights the third point.

plt.scatter(*zip(*(xy for i, xy in enumerate(zip(x, y)) if i!=2)), marker=6)
plt.scatter(x[2], y[2], c='red', marker=7, s=200);

Perhaps, the filtering is simpler with numpy.

data = np.array([x, y])                                # construct a single 2d array
plt.scatter(*data[:, np.arange(len(x))!=2], marker=6)  # plot all except the third point
plt.scatter(*data[:, 2], c='red', marker=7, s=200);    # plot the third point

img2

As a side note, you can find the full dictionary of marker styles here or by matplotlib.markers.MarkerStyle.markers.

# a dictionary of marker styles
from matplotlib.markers import MarkerStyle
MarkerStyle.markers

Upvotes: 1

Bill Bell
Bill Bell

Reputation: 21663

One of the simplest possible answers.

import matplotlib.pyplot as plt

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

plt.plot(x[1:], y[1:], 'ro')
plt.plot(x[0], y[0], 'g*')

plt.show()

Upvotes: 31

Related Questions