How to implement legends, titles, and labels with Matplotlib in python?

Description

To differentiate two different curves
using matplotlib legends, titles and labels using python.

   Import matplotlib package and
sub packages also.

   Load the data set.

   Use plot function to differentiate
two curves.

   Pass the corresponding variable
to the plot function.

#import libraries
import matplotlib.pyplot as plt
import pandas as pd

#sample data
data={‘salary':[100,200,300],
‘age':[90,95,600],
‘rating':[110,96,250],
‘bonus':[2500,1200,900]}

#create data frame
df=pd.DataFrame(data)
x = (df[‘salary’])
y = (df[‘bonus’])

x2 = (df[‘rating’])
y2 = (df[‘age’])

#call the plot functions
plt.plot(x, y, label=’First Line’,color=’r’)

#second curve
plt.plot(x2, y2, label=’Second Line’,color=’g’)

#define the labels and title
plt.xlabel(‘Salary and rating’)
plt.ylabel(‘age and bonus’)
plt.title(‘Legends, Tables and Titles’)

#call the legend function
plt.legend()
plt.show()

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit