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


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

#create data frame
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

Leave Comment

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

clear formSubmit