Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

position of legend matplotlib

plt.legend(loc='upper right')
Comment

place legend on location matplotlib

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 6, 3
fig, axes = plt.subplots(ncols=3)
locs = ["upper left", "lower left", "center right"]
for l, ax in zip(locs, axes.flatten()):
    ax.set_title(l)
    ax.plot([1,2,3],[2,3,1], "b-", label="blue")
    ax.plot([1,2,3],[1,2,1], "r-", label="red")
    ax.legend(loc=l, bbox_to_anchor=(0.6,0.5)) # This is the key statement. 
    ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes)

plt.tight_layout()    
plt.show()
Comment

matplotlib position legend

plt.legend(loc='best')
plt.legend(loc='upper right')
plt.legend(loc='Center left')
plt.legend(loc='Upper center')
# etc.
Comment

legend matplotlib


import matplotlib.pyplot as plt

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

matplotlib legend location manual

answer=this is how we are defining the location of legend
Comment

PREVIOUS NEXT
Code Example
Python :: create dictionary from keys and values python 
Python :: how to extract integers from string python 
Python :: python summary() 
Python :: basic tkinter window 
Python :: python how to count number of true 
Python :: python package version 
Python :: make the program title a name python 
Python :: error command errored out with exit status 1 face_recognition 
Python :: django reverse queryset 
Python :: how to create numpy array using two vectors 
Python :: run linux command using python 
Python :: python ftplib get list of directories 
Python :: plot background color matplotlib 
Python :: read binary image python 
Python :: pandas divide one column by another 
Python :: contains duplicate in python 
Python :: python plotting moving average 
Python :: plt.tick_params 
Python :: apply same shuffle to two arrays numpy 
Python :: lower upper in pytho 
Python :: how to create model in tensorflow 
Python :: extract zip file in python zipfile 
Python :: Access the Response Methods and Attributes in python Get the HTML of the page 
Python :: snakeCase 
Python :: Get files from S3 bucket Python 
Python :: print statement in python 
Python :: what is from_records in DataFrame() pandas in python? 
Python :: how to write the character from its ascii value in python 
Python :: counter python 
Python :: python break for loop 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =