Answers for "plt.legend()"

7

matplotlib legend

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, "-b", label="sine")
plt.plot(x, y2, "-r", label="cosine")
plt.legend(loc="upper left")
plt.ylim(-1.5, 2.0)
plt.show()
Posted by: Guest on June-01-2020
3

plt.legend(

plt.legend(['first', 'second']);
Posted by: Guest on January-13-2021
4

python plot legend

# Short answer:
# matplotlib.pyplot places the legend in the "best" location by default
# To add a legend to your plot, call plt.legend()

# Example usage:
import matplotlib.pyplot as plt
x1 = [1, 2, 3] # Invent x and y data to be plotted
y1 = [4, 5, 6]
x2 = [1, 3, 5]
y2 = [6, 5, 4]

plt.plot(x1, y1, label="Dataset_1") # Use label="data_name" so that the 
									# legend is easy to interpret
plt.plot(x2, y2, label="Dataset_2")
plt.legend(loc='best')
plt.show()

# Other legend locations you can specify:
Location String		Location Code (e.g. loc=1)
'best'				0
'upper right'		1
'upper left'		2
'lower left'		3
'lower right'		4
'right'				5
'center left'		6
'center right'		7
'lower center'		8
'upper center'		9
'center'			10
Posted by: Guest on October-03-2020
0

python no label in legend matplot

ax.plot(randn(1000).cumsum(), 'k.', label='_nolegend_')
Posted by: Guest on May-08-2020
0

matplotlib pyplot legend location

figlegend( (line1, line2, line3),
           ('label1', 'label2', 'label3'),
           'upper right' )
Posted by: Guest on May-04-2020
-1

python plt show legend

import numpy as np
import matplotlib.pyplot as plt

def example_legend():
    plt.clf()
    x = np.linspace(0, 1, 101)
    y1 = np.sin(x * np.pi / 2)
    y2 = np.cos(x * np.pi / 2)
    plt.plot(x, y1, label='sin')
    plt.plot(x, y2, label='cos')
    plt.legend()
Posted by: Guest on April-21-2021

Python Answers by Framework

Browse Popular Code Answers by Language