0

I have a plot in which I assigned the same colour to lines that correspond to the same model:

enter image description here

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.

Emil
  • 1,153
  • 1
  • 16
  • 30
  • 1
    Add a label definition to the condition for which the model is being judged. Since you will probably see multiple legends that are the same, make them each a single one as shown in the following [Duplicate items in legend in matplotlib?](https://stackoverflow.com/questions/19385639/duplicate-items-in-legend-in-matplotlib). – r-beginners Mar 07 '22 at 10:06

0 Answers0