#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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.

Process

   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.

Sapmle Code

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

Screenshots