'Assign label to color in matplotlib legend
I have a plot in which I assigned the same colour to lines that correspond to the same model:
Now, I want to add a legend to describe both colours.
I have used the following code to obtain the plot:
def get_plot(measure, clustering):
for num_tops in [ i for i in os.listdir(PATH) if i.endswith('.pickle')]:
infile = open(num_tops,'rb')
dicts = pickle.load(infile)
infile.close()
vectors = [i for i in dicts['Model1'].keys()]
for model in ['Model1','Model2']:
measures = []
if model == 'Model1':
color = 'tab:blue'
else:
color = 'tab:green'
for i in vectors:
measures.append(dicts[model][i][clustering][measure])
plt.plot(vectors, measures, color = color)
plt.legend()
plt.title(' '.join([measure, 'for Model1 and Model2 with',clustering, 'clustering.']))
plt.show()
The legend should show a green line and a blue line (rather than a value for each line in the graph), with 'Model 1' and 'Model 2' as values assigned to it, respectively.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

