

How to add multiple labels in legend, one for each curve
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
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()
Recommend
-
43
Derrick Higgins of American Family Insurance presented a talk, “ Classify all the Things (with multi...
-
8
Add a class with numbers to each element li, jquery advertisements Add class with numbers to each li elements, jquery. Here...
-
3
Add the following character to each char in a string advertisements i've to return it with the next character in the ASCII table, example:...
-
11
Add a value to each element of an array in Python In this article, we will learn how to add a number to each element of a NumPy Array in Python. Given a NumPy Array, we need to Add number to each e...
-
8
Blog Create Multiple Resources at Once With Terraform for_each ...
-
15
Uniglo (GLO) Set to Combine Multiple Systems to Challenge Curve (CRV), Uniswap (UNI), and Convex (CVX) Simultaneously
-
7
Piracy settlements — Record labels’ war on ISPs and piracy nets multiple settlements with Charter Confidential settlements could lead to more terminations of Internet subsc...
-
12
How to Add Prefix or Suffix to Each New Line in JavaScriptMarch 30th 2023 New Story4 min by
-
8
GitHub Actions are a powerful tool to automate your workflow. They can be used to run tests, deploy your code, publish a package, and much more. The cool thing is, there's...
-
14
Add Unicode symbols and format text labels in SAS 0 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK