lgd
lgd

Reputation: 1462

plotting points with different colors by name in matplotlib

how can different points be plotted in different colors in matplotlib when the colors are named and not referred to by number? eg

import matplotlib.pylab as plt
# this fails
plt.plot([1,2,3],[4,5,6],c=["r", "k", "b"]) 

c only takes numeric values. is there a way to pass it names of colors instead?

Upvotes: 2

Views: 1481

Answers (2)

DilithiumMatrix
DilithiumMatrix

Reputation: 18657

I think you want plt.scatter, in which case your code works:

plt.scatter([1,2,3],[4,5,6],c=["r", "k", "b"]) 

Upvotes: 4

dslack
dslack

Reputation: 845

Try this:

import matplotlib.pylab as plt
xx = [1, 2, 3]
yy = [4, 5, 6]
colors = ['r', 'k', 'b']
for ii in range(len(xx)):
    plt.plot(xx[ii], yy[ii], 'o', color=colors[ii])

Upvotes: 1

Related Questions