In matplotlib, I am using a loop to plot a graph, with each step only plotting one data point (X[index], Y[index]) with a specific color C[index], so that it will assign a color to each point. In total, I use 8 colors to represent my data points, so I hope I can have a legend with these 8 colors and each one represents one meaning.
This is my code. Thanks for help. : )
for index in range(0,len(X)):
plt.plot(X[index],Y1[index], marker = 'o', markersize = 10, color = C[index])
plt.xlabel("the percentage of students' credit hours found in instructor data", fontsize=14, color ="blue")
plt.ylabel("total number of writing assinments", fontsize=14, color="blue")
plt.axis([0, 100, 0, max(Y1)])
plt.show()
An example straight from the matplotlib documentation:
So, if you set the
labelproperty for each line, you should just be able to calllegend()afterwards and it will automagically generate the legend with appropriate labels.If you want to do something fancier with the legend, they also have documentation for that.