This error happens when you try to do a colored plot with matplotlib, instead of passing in a list of colors, you passed in a list of values that can not be understood as colors. For instance:

 import matplotlib.pyplot as plt  y = [1,1,2,2,3,2,1,3,3,2,1,3,2,1,1] x = range(len(y))   plt.scatter(x, y, color=y) 

will result in an error as in title.

The reason is that the color argument expects a list of colors, like blue, green and red, but we are providing a list of integers.

Solution:

We have two options to resolve the issue:

Option one: the most straightforward solution to the problem is to use c argument, which automatically transform y into certain colors. i.e.

 plt.scatter(x, y, c=y) plt.show() 

Option two: map y to actual colors:

 plt.scatter(x, y, color=['red' if v == 1 else 'green' if v == 2 else 'blue' for v in y]) plt.show() 

You can also check the full working example here: https://akuiper.com/console/XL88aspRjY75

Or try it in the PyConsole browser extension:

Just remember to activate matplotlib first using the %activate matplotlib command.


This post is ad-supported