Write a Python Program to Generate a Line Plot of Name vs Salary

Given the values of X and Y as input to the Python program you have to generate a line plot using the matplotlib.pyplot module.  display the graph having a line graph of name versus salary.

You have to pre install the  matplotlib and numpy modules for this program to work.

 

Python Generate Line Plot Name Salary

 

 



To install both the libraries execute as shown below

pip install matplotlib
pip install numpy




Write a Python Program to Generate a Line Plot of Name vs Salary

Import the required module for this program to get executed, that is we require matplotlib.pyplot and numpy. Instead of using numpy we can also use a simple list, but to be complete I will be using numpy.

import matplotlib.pyplot as plt
import numpy as np



Let's use pyplot  and set the title as "Line Graph : Name vs Salary",  take a variable x  and assign a numpy array that contains the list of employees as shown below.  then set the xlabel  as “Employee name”  as x axis label


plt.title("Line Graph : Name vs Salary")

x = np.array(["Ajay", "Suresh", "Monica", "Rajesh"])
plt.xlabel("Employee Name")





Not take another variable named as y and take a numpy array containing the salary of his employee number of employees for taking random values as 4 as shown below.  also label the y-axis as salary.

y = np.array([10000, 25000, 15000, 5000])
plt.ylabel("Salary")




Now using the plot()  function call plot x and y axis by providing x and y as input to plot() function.  then use the show function call to display the line graph.

plt.plot(x, y)
plt.show()  



Code
=================

import matplotlib.pyplot as plt
import numpy as np

plt.title("Line Graph : Name vs Salary")

x = np.array(["Ajay", "Suresh", "Monica", "Rajesh"])
plt.xlabel("Employee Name")

y = np.array([10000, 25000, 15000, 5000])
plt.ylabel("Salary")

plt.plot(x, y)
plt.show()  





Output full screen
=====================

Write a Python Program to Generate a Line Plot of Name vs Salary

 

 


Output : Dialogue box
=========================

 

Write a Python Program to Generate a Line Plot of Name vs Salary

 

Conclusion
=====================

Change the values of X and Y Axes and execute the above Python program,  comment down below if you have any suggestions to improve above Python program





Post a Comment

0 Comments