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?

cottontail
25k25 gold badges178 silver badges173 bronze badges
asked Jan 5, 2017 at 16:06
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()
answered Jan 5, 2017 at 16:20
1
  • 2
    It means green asterisk -- like this * but green. Commented Jan 5, 2017 at 17:05
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
answered May 5, 2023 at 8:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.