24

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?

cottontail
  • 10,268
  • 18
  • 50
  • 51
Abhinav Goel
  • 401
  • 1
  • 6
  • 13

2 Answers2

30

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()
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
0

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
cottontail
  • 10,268
  • 18
  • 50
  • 51