Lab: Matplotlib
Import the required libraries:
import [Link] as plt
Ex 1: Simple Line Plot
subjects = [‘Math’, ‘Physics’, ‘Chemistry’, ‘English’, ‘CS’]
marks = [78, 85, 72, 80, 90]
[Link](subjects, marks)
[Link]('Marks vs Subjects')
[Link]('Subjects')
[Link]('Marks')
[Link]()
Ex 2: Bar Chart
months = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’]
sales = [120, 150, 100, 170, 160, 180]
[Link](months, sales)
[Link]('Monthly Sales')
[Link]()
Ex 3: Scatter Plot
study_hours = [1,2,3,4,5,6,7]
scores = [40,45,55,65,70,78,85]
[Link](study_hours, scores)
[Link]('Study Hours')
[Link]('Scores')
[Link](True)
[Link]()
Ex 4: Multiple Lines
days = [‘Mon’,‘Tue’,‘Wed’,‘Thu’,‘Fri’,‘Sat’,‘Sun’]
city1 = [30,32,31,33,34,35,36]
city2 = [25,26,27,26,28,29,30]
[Link](days, city1, label='City A')
[Link](days, city2, label='City B')
[Link]()
[Link]()
Ex 5: Subplots
[Link](2,1,1)
[Link](subjects, marks)
[Link](2,1,2)
[Link](months, sales)
[Link]()
Ex 6: Pie Chart
obtained = [78,85,72,80,90]
[Link](obtained, labels=subjects, autopct='%1.1f%%')
# use explode=(0,0,0,0.1,0) to show coming out from chart
[Link]('Marks Distribution')
[Link]()
Ex 7: Histogram
marks = [Link](40,100,50)
[Link](marks, bins=10)
[Link]('Marks Histogram')
[Link]()
Ex 8: Box Plot
marks=[0,1,2,3,4,5,6]
[Link](marks)
[Link]('Marks Boxplot')
[Link]()
[Link](2)
x=[Link](5,5,20) # (5,5,200)
[Link](x)
Ex 9: Horizontal Bar Chart
countries = [‘India’,‘USA’,‘UK’,‘China’]
population = [1400,330,67,1440]
[Link](countries, population)
[Link]()
Ex 10: Area Plot
usage = [120,130,125,140,150,160]
plt.fill_between(months, usage)
[Link]('Electricity Usage')
[Link]()
Ex 11: Step Plot
[Link](study_hours, scores)
[Link]()
Ex 12: Stem Plot
[Link](study_hours, scores)
[Link]()
Ex 13: Log Scale Plot
[Link](study_hours, scores)
[Link]('log')
[Link]()
Ex 14: Scatter with Size
sizes = [20,40,60,80,100,120,140]
[Link](study_hours, scores, s=sizes)
[Link]()
Ex 15: Annotated Plot
[Link](study_hours, scores)
[Link]('Top Score', (7,85))
[Link]()
Ex 16: Grid Customization
[Link](subjects, marks)
[Link](True)
[Link]()
Ex 17: Line Styles and Markers
[Link](subjects, marks, linestyle='--', marker='o')
[Link]()
Ex 18: 2x2 Subplots
[Link](2,2,1); [Link](subjects, marks)
[Link](2,2,2); [Link](months, sales)
[Link](2,2,3); [Link](study_hours, scores)
[Link](2,2,4); [Link](marks)
[Link]()
Ex 19: CSV Data Plot
CSV file with columns: Year, Sales
import pandas as pd
df = pd.read_csv('[Link]')
[Link](df['Year'], df['Sales'])
[Link]()
Ex 20: Saving Figure
[Link](subjects, marks)
[Link]('[Link]')
[Link]()
Ex: 21:
months = ["Jan", "Feb", "Mar", "Apr"]
visitors = [1200, 1800, 2100, 1600]
[Link](months, visitors, color="blue", marker="o", linestyle="--")
[Link]("Monthly Website Visitors")
[Link]("Month")
[Link]("Visitors")
[Link](True)
Ex: 22:
products = ["Laptops", "Phones", "Tablets"] # Categories
sales = [200, 350, 150] # Values
[Link](products, sales, color=["#4CAF50", "#2196F3", "#FF5722"], width=0.6)
[Link]("Q1 Product Sales")
[Link]("Units Sold (Thousands)")
[Link](0, 400)
# to add text on each
for i, v in enumerate(sales):
[Link](i, v, str(v), ha='center')
Ex: 23:
t = [Link](0., 5., 0.2)
[Link](t, t, 'g--', t, t**2, 'bs', t, t**3, 'r^')