Python markup with colors matching strings

I want to create a scatter plot using python matplotlib where the color of the point should match a specific line from the data file, so something like this:

data = np.genfromtxt('filename.txt', delimiter=',', dtype=None, names=['a', 'b', 'c'])
plt.scatter(data['a'], data['b'])

      

Thus, the first column of file "a" is the float, the second column "b" is the float, and the third column "c" is the string. The row column contains 5 different words which I would like to plot as 5 different colors is a scatter plot. Any ideas? Many thanks!

+3


source to share


1 answer


Something along these lines should do the trick:



color_dict = { 'Allan':'red', 'Betty':'blue', 'Chris':'black', 'Diane':'green','Eugene':'purple' }

plt.scatter( data['a'], data['b'], color=[ color_dict[i] for i in data['c'] ] )

      

+4


source







All Articles