[New post] [matplotlib]ValueError: ‘color’ kwarg must be an color or sequence of color specs. For a sequence of values to be color-mapped, use the ‘c’ argument instead
leopsidom posted: " 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,"
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()
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.