Thursday 14 September 2023

How to Customize the Legend title in Plot

Using ‘title’ parameter of plot.legend() method, we can customize the legend title.

 

Example

plt.legend(title='x square vs cube')

 

legend_title.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(title='x square vs cube')
plt.show()

Output



 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment