(8) Learn to plot different types of graphs using PyPlot.
1) Plotting a line
2) Bar Chart
3) Histogram
4) Pie-chart
#Lab-8-Learn to plot different types of graphs using PyPlot.
#""Matplotlib and its dependency packages are available in the form of wheel packages
#on the standard Python package repositories and can be installed on Windows,
#Linux as well as MacOS systems using the pip package manager.
#follow this instruction to install Matplotlib Package
#Matplotlib can also be installed using the Python package manager, pip.#To install Matplotlib with pip, open a terminal window and type:
#pip install matplotlib
#This command installs Matplotlib in the current working Python environment.
#command to update python PIP on a windows
#python -m pip install -U pip
Program Code-1:Plotting a Line:
#Plotting a lineimport matplotlib.pyplot as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis') # naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title(' College First graph!')
# function to show the plot
plt.show()
Output-1:Plotting a Line:
Program Code-2:Bar Chart:
#Lab-8-Learn to plot different types of graphs using PyPlot.
#Bar Chart
import matplotlib.pyplot as plt
# x-coordinates of left sides of bars
left = [1, 2, 3, 4, 5]
# heights of bars
height = [10, 24, 36, 40, 5]
# labels for bars
tick_label = ['one', 'two', 'three', 'four', 'five']
# plotting a bar chart
plt.bar(left, height, tick_label = tick_label,width = 0.8, color = ['red', 'green'])
# naming the x-axis
plt.xlabel('x - axis') # naming the y-axis
plt.ylabel('y - axis') # plot title
plt.title(' College bar chart!')
# function to show the plot
plt.show()
Output-2:Bar Chart:
Program Code-3:Histogram:
#Lab-8-Learn to plot different types of graphs using PyPlot.
#Histogram
import matplotlib.pyplot as plt
# frequencies
ages = [2,5,70,40,30,45,50,45,43,40,44,
60,7,13,57,18,90,77,32,21,20,40]
# setting the ranges and no. of intervals
range = (0, 100)
bins = 10
# plotting a histogram
plt.hist(ages, bins, range, color = 'green',histtype = 'bar', rwidth = 0.8)
# x-axis label plt.xlabel('age')
# frequency label plt.ylabel('No. of people') # plot title
plt.title(' College histogram')
# function to show the plot
plt.show()




No comments:
Post a Comment