Friday, 10 April 2020

Python Program-8

(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 line
import 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()

Output-3:Histogram:



Program Code-4:Pie-chart:

#Lab-8-Learn to plot different types of graphs using PyPlot.

#Pie-chart

import matplotlib.pyplot as plt

# defining labels

activities = ['eat', 'sleep', 'work', 'play']

# portion covered by each label

slices = [3, 7, 8, 6]

# color for each label

colors = ['r', 'y', 'g', 'b']

# plotting the pie chart

plt.pie(slices, labels = activities, colors=colors,startangle=90, shadow = True, explode = (0, 0, 0.1, 0),radius = 1.2, autopct = '%1.1f%%')

plt.title(' College Pie Chart')

plt.legend()

# showing the plot

plt.show()


Output-4:Pie-chart:



No comments:

Post a Comment