4

How to add multiple labels in legend, one for each curve

 2 years ago
source link: https://www.codesd.com/item/how-to-add-multiple-labels-in-legend-one-for-each-curve.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to add multiple labels in legend, one for each curve

advertisements

I have written a code to generate a graph for 3 different vector sets.
I am able to add the label for my first diagonal line as label='Diagonal' but when I plot data into a loop and try to represent a label for each data set it is now showing on the graph. Only the first label shows up.

How can I show the other labels as well?

import numpy as np
import pylab as pl

pl.plot([0, 1], [0, 1], '--',label='AUC', lw=2)
pl.xlim([-0.05, 1.05])
pl.ylim([-0.05, 1.05])
pl.xlabel('FP Rate',fontsize=22)
pl.tick_params(axis='x', labelsize=22)
pl.tick_params(axis='y', labelsize=22)
pl.ylabel('TP Rate',fontsize=22)
pl.legend(loc="lower right")
pl.axis('tight')

n = ("data_1", "data_2", "data_3", "data_4")
for x, i in enumerate(range(0,3)):

    sampl_1 = sorted(np.random.uniform(low=0, high=1.0, size=(20,)))
    sampl_2 = sorted(np.random.uniform(low=0, high=1.0, size=(20,)))
    pl.plot(sampl_1, sampl_2, '-', label=n[x], lw=2)

pl.show()


You're creating the legend before adding the additional lines to the plot. In order to let the legend include all the labeled object in the plot, create the legend at the end of the script.

import numpy as np
import pylab as plt

plt.plot([0, 1], [0, 1], '--',label='AUC', lw=2)
plt.xlabel('FP Rate')
plt.ylabel('TP Rate')
plt.axis('tight')

n = ("data_1", "data_2", "data_3", "data_4")
for x, i in enumerate(range(0,3)):

    sampl_1 = sorted(np.random.uniform(low=0, high=1.0, size=(20,)))
    sampl_2 = sorted(np.random.uniform(low=0, high=1.0, size=(20,)))
    plt.plot(sampl_1, sampl_2, '-', label=n[x], lw=2)

plt.legend(loc="lower right")
plt.show()


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK