Thursday, 7 September 2023

How to Customize Legend Location in Pyplot?

Using 'loc' parameter of legend method, we can customize the legend location.

 

Example 1: Legend location is at 'upper right'.

plt.legend(loc='upper right')



Example 2: Legend location is at 'upper left'.

plt.legend(loc='upper left')


 

Example 3: Legend location is at 'lower left'.

plt.legend(loc='lower left')


Example 4: Legend location is at 'lower right'.

plt.legend(loc='lower right')


Find the below working application.

 

legend_location_customization.py

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1, 10)
y1 = x**2
y2 = x**3

plt.plot(x, y1, label="y = x^2", color='r')
plt.plot(x, y2, label="y = x^3", color='g')

plt.legend(loc='upper left')
plt.show()


Previous                                                    Next                                                    Home

No comments:

Post a Comment