25

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?

1
  • Do you want to change the color and shape of the first pair in a scatterplot? Commented Jan 5, 2017 at 16:18

2 Answers 2

31

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()
Sign up to request clarification or add additional context in comments.

1 Comment

It means green asterisk -- like this * but green.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.