python plot 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()

4
4
Ben Kane 130 points

                                    
import matplotlib.pyplot as plt

#define x and ysin
plt.plot(x,ysin,label='sin(x)')
plt.legend()
plt.show()

4 (4 Votes)
0
4.13
8
A bsss 115 points

                                    plt.legend(['first', 'second']);

4.13 (8 Votes)
0
4.5
6
Bmcl 140 points

                                    plt.legend(loc='upper right')

4.5 (6 Votes)
0
3.83
6
GreyShift 85 points

                                    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()

3.83 (6 Votes)
0
3.88
12

                                    # 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

3.88 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
plt.legend(loc=); add each plot to legend matplotlib legend matlibplot legend name python matlibplot matplotlib legend x axis plt.figure add legend add legend to plot plt python plt add manually legend how to change legend position in matplotlib set custom legend for figure matplotlib df.plot with legend add legend to plot python plot legend title legend plot ypthon set legend pandas plot how to add legend to df.plot() plt.plot dataframe legend matplotlib get_legend() matplotlib legend attributes set legend manually matplotlib matplotlib additional legend how to set location of legend matplotlib matplotlib set legend location put legend inside plot matplotlib plot legend in python plt add legend to entire plot add legend plot dataframe how to put a legend in matplotlib add plot legend plt.plot legend example label legend of plot matplotlib matplotlib legend for plots in different methods how to show legend in matplotlib plt.plot legend position place the legend in pyplotlib plotting a legend in pyplot plt.legend() matplotlib axis.legend matplotlib how to add legend in plot python legend show with labels matplotlib matplotlib plt.plot legend matplatlib 3. Add a legend adding legend to plot in python change legend location matplotlib matplotlib legend set order of labels plt.legend meaning show legend matplot lib legend of matplotlib move plot legend python plot axis legend sympy plot legend matplotlib show all legend make legend matplotlib plt.legend position python sympy expression in matplotlib legend how to make legend with label in matplotlib howt to add something to the legend on matplotlib python howt to add something to the legend on matplotlib hwo to position legend python matplotlib legend title python adding legend to python plot change location legend matplotlib how to define a legend to plot in matplotlib how to add a legend to plot in matplotlib plot legend in matplotlib changing legend labels in matplotlib matplotlib fix legend position plt matplotlib order legend how to use legend in matplotlib legend() in matplotlib plt.plot() legend legend label matplotlib how to add legend in matplotlib plots matplotlib axes add legend matplotlib y legend matplotlib legend horizontal legend below figure matplotlib how to plot legend using matplotlib put legend for plot legend mathplotlib python python plot graph legend set legend labels matplotlib set legend axis matplotlib how to specify the position of legend in matplotlib matplotlob legend location what does matplotlib.pyplot legend axes legend python plotting legend python matplotlib add legend label move figure legend matplotlib make figure legend place matplotlib .plot python legend python legend library pyplot plot legend how to add legend in plt graphs how to put legend to the plot what is the work of plt.legend in matplotlib matplotlib fig legend plot legend python position axes set legend python locations for legend in matplotlib plot legend pyplot how to create a legend in matplotlib how to add a legend to a python graph how to pass labels in legend to matplotlib how to put legend in order pyplot legend pythhon matplotlib set legend how to set a legend for plot legend matplotlib custom matplotlib plt legend plt python legend add legend to figure matplotlib matplotlib legend ax legend for plot legend plt.plot legend plot legend below matplot legend attributes in plot matplotlib legend over plots what is plt.legend() python put legend in plt.plot matplotlib control legend position matplotlib.legend.Legend at plt.legendws how to specify the legend in plot in python pandas plot set legend plt legend legend python graph plt get legend how to change legend location in matplotlib plt.legend labels matplotlib ax legend matplotlib plot legend separately matplotlib legend python ax.set_legend() matplotlib set_legend() matplotlib set_legend matplotlib edit legend matplotlib plt-legend python legend show plot python pltmatplotlib legend add legend with percentages matplotlib insert legend python plot how to add legend to a chart in python how to set a legend in a plot python add legend to matplotlib python matplotlib plot label legend python ax legend legend matplotilib how to add legend in matplotlib plot plot legend matplotlib add legend matplotlib example adding legend plt.plot matplotlib legend labels plot legend python example matplotlib cant see legend legend title plt legend in matplotlib uses how to position legend in matplotlib python matplotlib plot legend plot legend position matplotlib what is plt.legend what is plt.legend() plot matplotlib legend ply.legend pandas plot make legend add legend to plt matplotlib python fig legend location legend = plt.legend() what it does how to show legend matplotlib plt. fig legend plt.legend parameters matplotlib legend positions plt plot legend position pyplot legend under plot legend for plt matplotlib legend documentation matplotlib labels and legend python matplot legend ax set legend matplotlib plt.plot legend plt .legend can legend be plot in matplotlib pandas plot change legend position the legend in matplotlib plt.legend python example change legend format matplotlib matplotlib matshow show legend matplotlib legend ordering position inside legend matplotlib print legend matplotlib figure matplotlib box plot legend move matplotlib legend matplotlib legend set position add legend to axis matplotlib ple.legend plot python add legend add legend pandas plot legend matplotlib.pyplot pandas plot with legend legend in matplotlib python matplotlib plot label for legend legend plot matplotlib how to add legend to plot in legend style matplotlib pandas add dataframe to the bottom of another plt.plot add legend put legend in matplotlib legend for pandas plot matplotlib change legend pyplot legend of labels how to add legend in matplotlib python from matplotlib.legend import Legend matplotlib pandas legend matplotlib insert legend matplotlib make a legend python manually add legend python manually make legend set legend when plotting pyplot legend position how to add a legend in plotly python Axes.legend() matplotlib manually create legend creating a legend in matplotlib how to add custom legend in matplotlib plt.legend attributes matplotlib legend under plot legend method matplotlib location of legend matplotlib plt figure legend legend title matplotlib legend off matplotlib legend name matplotlib plt.legend options matplotlib change legend position plt. legend() in matplot lib set legend for pandas plot graph legend python matplotlib legend figure how to create legend in matplotlib how to set legend in matplotlib figure set legend adjust legend position matplotlib add legend to plt.plot axes legend matplotlib plt legend pandas axis set legend matplotlib add custom legend matplotlib matplotlib put legend layout matplotlib put legend below graph legend location python plot plot legend location matplotlib figure legend example what does plt.legend() does? add legend pyplot matplotlib plt.legend legend matplotlib horizontal location legend matplotlib how to provide a legend for plot in python plt plot add legend how to change legend in matplotlib matplotlib legend of different axis legend subplot matplotlib matplotlib add to legend add legend in graph python how to set position of legend matplotlib matplotlib cluster legend how to use plt.legend take legend out of plot matplotlib add to legend matplotlib axis legend matplotlib legend out of plot matplotlib plt legend matplotlib set legend matplot lib matploylib legend how to give position to legend in matplotlib how to do legend in matplotlib matplotlib how to create a legend plt. legend() matplotlib legend out of plot python legend position change location of legend matplotlib legend label matplotlib python plt legend dataframe plot legend in python plotas add legend to bar plot pyplot legend matplot lib dataframe. plot legend dataframe. plot legend example matplotlib plot label legend figure legend matplotlib matplotlib legend symbols legend label in matplotlib add legend label matplotlib include legend matplotlib set position of legend matplotlib legend plot matplotlib 3d plot legend plot add legend legend pyplot how to get legend in matplotlib python set legend for axis matplotlib how to change the legend position in matplotlib matplotlib legend axis plot legend position python df.plot legend plot legend positon matplotlib matplotlib create legend legend position python inside plot how to show legend in python what does pl.legend legend python matplotlib figure legend legend matplolib location add legend within each plot python plot axis legend matplotlib plot legend axis matplotlib jupyter matplotlib legend matplotlib label legend plot.legend() how to change legend on the matplotlib graph matplotlib.pyplot legend add legend to matplotlib plot what does pl.legend how to control legend in matplotlib how to give legend to axes matplotlib matplot lib legend What is the use of legend() function in matplotlib? plotlib legend display legend matplotlib plot legend pytohn matplotlib legend for marker matplotlib legend style add legend in matplotlib position legend outside plot matplotlib position legend matplotlib plotting legend in python python plot legend change legend labels in matplotlib how to change legend labels in matplotlib plot.legend matplotlib plot legend legend location matplotlib dj.plot(legend) python syntax how to plot legend in python legend for matplotlib legend for plot matplotlib matplotlib legend example plot python legend title localization of legend in python python matplotlib legende manually add legend matplotlib legend positioning matplotlib plot legend matplotlib python show legend plt matplotlib.legend adding legend in matplotlib manually how to add a legend in python matplotlib get legend how to plot a legend in python matplot library plot with legend matplotlib legende pandas plot add legend matplotlib show legend set location of legend matplotlib plt.legend.prop legend matplotlib pandas legend pandas plotting legend in matplotlib plot plot pandas legend add legend plot python how to add legend python how to use plt.legend in python python axes legend make legend in matplotlib pandas dataframe plot legend dataframe plot legend mathplotlib plot legend plot(legend=None) plt set legend python legend listplot legend add legend to plot python' plt.legend('') matplotlib legend order how to position legend in python fig.legend matplotlib matplotlib legend locaitons df plot legend matplotlib plot dataframe with legend set legend for plot python plt.legend pandas matplotlib how to plot a legend without plotting the object legend with data matplotlib matplotliv legend PLOT LEGEND PYHTON how to add legend to charts in matplotlib matplotlib how to make legend pandas plot.legend options matplotlib plot legend example legend position matplotlib plt create legend legend handles matplotlib legend osition matlotlib what is legend in matplotlib axes.legend matplotlib ax.plot python legend legend matplotlib\ plt.plot show legend plt legend label matplotlib matplotlib legend_options matplotlib plot lib legend matplotlib legend legend graph matplotlib plot with legend show legend matplotlib plot python ax set legend plot python ax legend put legend on the top of plot matplotlib how to add legend to a table matplotlib legend() in python add legend in plot python mathplotlib legend change legend pandas plot how to put legend in the axis matplotlib python how to set position of legend in matplotlib matplot legend plt.legend() python fix legend location matplotlib how to add a legend in matplotlib legend with matplotlib pandas legend of plot matplotlib legend simple matplotlib legend sample matplotlib: add legend to plot matplotlib legend set location legend matplotlib location plot.legend matplotlib python legend plot python pyplot legend legend based on labels matplotlib pyplot show plot legend plt.legend example custom legend matplotlib what is plot.legend pyplot add legend plt legend in python legend options matplotlib how to change the position of legend in matplotlib plt.legend matplotlib df.plot() legend add legend in plot function legend matplotlib plot show legend plt axes legend matplotlib pyplot legend legende plot python legend matplotlib position matplotlib pyplot.plot legend plt. legend python plt legend position matplotlib legend location how to give legend in matplotlib python graph legend legend() matplotlib how to change position of plt.show() legend python how to plot legend in bar ploit python how to plot legend python legend pltplot how to move the legend in matplotlib legend in matplotilib function legend for python plot plt suplegend ax.legend(loc='best') plt . plot legends plt legends mathplot label pyplot show legend Adding legends to matplotlib matpotlib plot label matplotlib graph legent plt show legend pyplot legend outside plot box legend matplotlib plot label python python legend add number python label plt position of the legend plt plt legend size add legend plt.plot add a legend to matplotlib barplot legend matlab place legend matplotlib python plot add legend legend plt numpy how to add legend to python plot plt show labels python bar plot legend plt.plot legend size matplotlib lable for plot how to add legends matplotlb plt legend pyplot matplotlib give legend how to set legend position in matlab python matplotlib legend position in python legend in line plot matplotlib pyplot.legend matplotlib legend label plt set legend title plt border legend what is key plot python legend labels line graph matplotlib plt ax legend plt legen plt legend python pplt legend location what does scatterpoints mean in plt.legend label matplotlib kwargs in matplotlib legend location plt.legend size inclinate legend matplotlib matplotlib tsne plot legend matplotlib figure legend matlibplot python legend out font legend matplotlib pyplot.legend() matplotlib how create legend legend package for matplotlib python plt plot python with legend title xaxis plt plot python with legend matplotlib titles and legends matplotlib legend kwargs plt legend output plt. legend plt.legend name legend plt.plot legend_kwds matplotlib matplotlib in jupyter notebook plot legend legend function python legends python plot plt plot label matrplotlib legend matplotlib legen label matplotlib set legend title matplotlib legend lable how to make a legend in matplotlib matplotlib legend add plot legend python location legend plot 3 plt.label matplotlib plot label legend loc plt.label() python plot number in legend plt legend title plt loc plt.legend position legend in matplot lib pu tlegned in plot multple lines in plot python add labels with numbers python plot legend how to add legend to plot in matplotlib legend pyplot matplotlib set legend together line matplotlib matplotlib ligend matplot lib label add some parameters to plt legend plt fill legend Showing legeng for multiple curves in matplot labeling legends - python ax.legend size matlab add legend plot plt.legend(loc=) add legend matplotlib fig plt.legend for python3 matplotlib legend title how to scale figure to insert legend python legend labels matplotlib what can be used as a legend handler python python 3 pyplot legend set legend position matplotlib label matplotlib plot matplolab legend handles maplotlib legend handles how to add a label in python plot loc legend plt legend left matplotlib pyplot graph legend matplotlib add legend labels matplotlib how to add legend python plot legend location matplotlib legend top center matplotlib legend top pyplot legend lables matplotlib plot legenda pyplot plt.legend position pyplot plt.legend matplot lib legends legend orientation matplotlib how to add info to legend matplotlib matplotlib axes legend how to add values to matplotlib legend adding legend matplotlib python adding legendmatplotlib python matplotlib location pyplot legend title plt.legend python what i need to install specify legend label in myplot legends python no shadows matplotlib line plots with legend matplotlib legend true plot.line() legend series plot.line() line legengnd series plot line legengnd matplotlib hide legend create legend manually python how to add plot legend in matplotlib plt.legend location python matplotlib write legend ax.legend location python legend title how to plot legend in seaborn plot scatter plot legend figure legend size how to out on label matplotlib lable plot python matlab legend size of legend in matplotlib set position legend matplotlib plt.legend(loc=4) ax.legend position pyhton plot legend pytho matlibplot legend print legend plot python plotlib label plt.plot label argument how to split graph in 2 legend and figure how to name a legend in python add_legend(loc="lower center") legend at bottom matplotlib ax legend plot legend matplotlib pyplot set legend plot legens how to give legend using matplotlib fig legend position python plt.plot[.loc[:] place a legend on the axes matplotlib legend matplot legend on plot python matplot lib legend position plotly python label legend legend for xlabel in matplotlib add legend for x labels in matplotlib add legent to bar plot in matplotlib position legent pyplot plt labels for classes matplotlib loc legend plt plot plt legend line with loc legend python plt plot label python display ledgend python how to change alpha legend python .legend matplotlib legent in plt graph plot legend mtplotlib rcparams matplotlib legend plt.lgend pylab legend how to add legends in python set legend in matplotlib declaring legend in matplotlib change legend position matplotlib pylab plot lines names plot label matplotlib matplotlib position legend plot legend matlab how to label plots python plt.legend python syntax labels plot python python label plot matplotlib legend correct labels legend in matplotlib displaying legend matplotlib plot legend position plt label add legends in matplotlib plt.plot label pyplot plot label matplotlib legend position python legent legend on bar matplotlib matplot set legend location numpy pyplot legend pyplot custom legend locatin python legend location plot.legend python how to add legend to a graph in python pyplot legend text add legend to plot matplotlib Pandas plot legend python pyplot legend plt.legend scatterpoints add plot legend matplotlib matplotlib.pyplot.legend() matplotlib labelin plt.plot ax.legend legend on matplotlib python line display matlibplot legend pyplot legend location bottom right show labels on graph matplot pyplot legend locations plt.legend( plt legent label pytohn plots pyplt.plot(label) how to label the functyion in pyplot plt legend right bar plot legend matplotlib set legend matplotlib pyplot legend in corner how to display legend in matplotlib matplotlib add multiple legends matplotlib print legend matplotlib print line legend box matplotlib python legend python plot legend value over graph legend lovation matplotlib move legend position position legend plot python matplotlib function in legend matplotlib legends multiple labels matplotlib matplotlib series label pyplot label plot pyplot legend matplotlib plot labels and legend legend for a graph in python python plot legen matplotlib plot lines with legend label legend matplotlib pyplot legend labels matlplotlib.pyplot legend set wide legend bottom matplotlib how to set matplotlib legend plot legend matlab python python plot legend value python matplotlib legend scale matplotlib.pyplot actual legend give a title to legend python how to make a legend on matplot legend pyplo how to add legends in plt plt legend inside add legend in python legend plot position matplotlib legend loc python legend top right matplotlib legend box how to show legend of graph in python plot legend plotlib best location how to insert large legend in python position legend pyplot what does plt.legend do plt how to add legenf add legend plt plt figure key matplotlib add ledgend plt add legend pythoin legend matpoltlib legend matplotlib legend name plt showing lengend python matplotlib adjust legend position PyCM python legend pl.imshow label .legend python plt legend location best loc legend matplotlib python matplotlib bar plot legend legend fontsize matplotlib matplotlib legend locatio set legend location matplotlib python legend locations matploit show key show lavel matplotlib how to put legend in matplotlib plt legend offset matplotlib legend bottom plt legend example pyplot label adding legend in matplotlib how to make legend in matplotlib legend example matplotlib figure label best python create legend matplotlib pyplot add to legend matplotlib append to legend of axis add value to legend matplotlib how to add a legend in pyplot plotting a legend in python how to add legend in python plot define key in matplotlib plot lewgend location python ax.legend matplotlib matplotlib legend left matplotlib plot label plt.legend(loc=3) plt.legend loc pl.legend legend location best python matplotlib.pyplot show legend matplotlib change legend labels matlibplot how to add legend legend plt python matplotlib place legend set position legend python how to put a legend in python legend in bar plot matplotlib matplotlib figure legend size pyplot legend location how to add legend to plot in python best location for legend matplotlib ax.legend loc size of legend matplotlib legend loc matplotlib python plot label plt.gac().set(legend) how to put legend in python matplotlib legend placement line plot matplotlib legend adding legend in pyplot how to add legend in python pyplot add legend to plot plot labels python python sed legend for figure plt.label matplotlib legend number of labels add legend matplotlib how to add a key to matplotlib matplotlib legend jupyter python how to add a figure legend at the best position change legend labels matplotlib format legend in matplotlib add legend to matplotlib figure plt legend location plt.legend() in python legend location python legend elements matplotlib plt label plot matplotlib legend show matplotlib legend font size plt plot legend matplotlib add legend to plot python add legend and label legend plt how to add the legend number in matplotlib. python plot with legend line with legend series plot show legend label legend using python legend in plot python legend in python legend python plot python plt.legend how to display parameters used in legend matplotlib fig.legend(location = 'best') plt.legend(loc='upper left') bigger legend label python python plot set legend label python plot set legend fig.legend python python matplot lib legend locatio legend python line legend python size python fig.legend together add legend to plot python show legend in matplotlib plot how to put a legend on a graph in python show legend in plot python show label plot legend matplotlib python plt labels examples plt.legend python plot legend python plt.legend in python label plot python python matplotlib add legend python plt legend location legend plot python create a legend in python show label in plot python legend placement python python show legend plt.legend(loc= how to add legend in python matplotlib python plot an instance of a legend legend matplotlib example matplot legend size matplotlib lanbel bottom left how to print legend with numbers python maptplotlib legend pyplot legenf legend python adding a legend in matplotlib fig.legend add legend matplotlib plot label for matplotlib plot x y legends matplotlib plt.legend() how to move legend from the line in graph python legend in plt legend size matplotlib frameon in legend python legend position label for curve matplotlib python plt legend matplotlib add legend python scatter plot legend position plt.legend location pyplot.legend pass parameters artists pyplot.legend pass parameters plt.legend() position graph lengends python how to show a legend in matplotlib how to add a legend matplotlib matplotlib legend size how do i give value in legend python matplotlib order legend by larger mean matplotlib plot mean on legend matplotlib plot labels matplotlib add information to legend pyplot legend size matplotlib label legend in line graph python sequence of string in label matplotlib plt.legend plt legend ax legend matplotlib matplot label how to add legend in matplotlib matplotlib add legend to graph add a key matplotlib plot a 2d graph in python with legend plt.legend(loc) in python python legend legend position python python matplotlib legend add legend python legend in python plot position of legend matplotlib
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source