Given students details that are name and percentage as the input to the Python program your task is to draw a scatter plot from the student details.
Let's take the already declared numpy array of student name and student percentage and plot a graph out of the given details.
Write A Python Program To Generate A Scatter Plot Of Name Vs Percentage
Let's import the required libraries, that is we require numpy and matplotlib, numpy will be used to store the student names and percentage and matplotlib will be used for the drawing of the plot.
import numpy as np
import matplotlib.pyplot as plt
Set the plot title as name versus percentages then, set the X label as name and Y label as percentages as shown below.
plt.title("Name vs Percentage")
plt.xlabel("Name")
plt.ylabel("Percentage")
Let's take some random names into an numpy array and store it into a variable named as name. also take another numpy array variable named as percentage and store the random integers as percentages, you can also use float. In the below statement I am using only five names and five percentages.
name = np.array(["Ana", "James", "Joe", "Sam", "Mary"], dtype=str)
percentage = np.array([23, 80, 45, 60, 90], dtype=int)
Plot the scatter plot by passing the input values to the function i.e, X and Y values and the color type the X value will be the name, why value will be the percentage and the color will be red “r” you can also use “g” for Green or “b” for blue. Finally use the show() function call to display the plot and run the program.
plt.scatter(name, percentage, c="r")
plt.show()
Python Code
=====================
import numpy as np
import matplotlib.pyplot as plt
plt.xlabel("Name")
plt.ylabel("Percentage")
plt.title("Name vs Percentage")
name = np.array(["Ana", "James", "Joe", "Sam", "Mary"], dtype=str)
percentage = np.array([23, 80, 45, 60, 90], dtype=int)
plt.scatter(name, percentage, c="r")
plt.show()
Output
=================
Name versus percentage
Conclusion
=====================
Change the array values containing name and the percentage and agree on the above Python program.
Comment down below if you have any suggestions to improve the above Python program
0 Comments