How to draw a scatter plot, where the points with the same X-values assigned the same color? [duplicate]
Dear experienced friends, I am curious about how to draw a scatter plot where the points with the same X-values have the same color? Suppose we have a dataset like the following:
import matplotlib.pyplot as plt
list1 = [1,1,1,1,2,2,2,2,3,3,3,3]
list2 = [9,8,10,11,1,2,1,2,4,5,6,7]
plt.scatter(list1, list2)
plt.show()
The picture looks like this:
However, I want to draw a picture like the following one. As you can see, the points with the same x-axis value have the same color. How can I achieve this? Thank you so much in advance!
Solution 1:
You can pass the x values to the c
parameter in the plt.scatter
function. It will use those integral values to map the colors to the points. Same integral value would mean having the same color.
import matplotlib.pyplot as plt
list1 = [1,1,1,1,2,2,2,2,3,3,3,3]
list2 = [9,8,10,11,1,2,1,2,4,5,6,7]
plt.scatter(list1, list2, c = list1)
plt.show()