1.
Basic Plotting Functions
Function Description
[Link](x, y) Line plot
[Link](x, y) Scatter plot
[Link](x, height) Vertical bar chart
[Link](y, width) Horizontal bar chart
[Link](data) Histogram
[Link](sizes) Pie chart
[Link](data) Box and whisker plot
[Link](x, y) Stack plot
2. Labels and Titles
Function Description
[Link]("Title") Add title to graph
[Link]("X Label") Label for X-axis
[Link]("Y Label") Label for Y-axis
[Link]() Show legend for labels in plot
[Link](True) Show grid
[Link](x, y, "text") Place custom text on plot
3. Styling and Customization
Function Description
[Link](x, y, 'r--') Red dashed line (r-- = red + dashed)
[Link]('ggplot') Use predefined plot styles
[Link](figsize=(w,h)) Set figure size
[Link]([xmin, xmax, ymin, ymax]) Set axis limits
[Link]() / [Link]() Set X or Y limits
[Link]() / [Link]() Customize tick marks
4. Subplots and Layouts
Function Description
[Link](nrows, ncols, index) Add subplot (e.g. [Link](2,2,1))
[Link]() Create figure and axes (object-oriented)
plt.tight_layout() Prevent overlapping elements
Basic Plotting Functions
# 1. Line Plot
x=[2, 4, 6, 12, 9]
y = [10, 15, 13, 18, 16]
[Link](figsize=(6,4))
[Link](x, y, marker='o')
[Link]("Line Plot: Stock Price Over Days")
[Link]("Day")
[Link]("Stock Price")
[Link](True)
[Link]()
x---------------------------------------------------------------------x
# 2. Scatter Plot
study_hours = [1, 2, 3, 4, 5]
scores = [50, 55, 65, 70, 85]
[Link](figsize=(6,4))
[Link](study_hours, scores, color='red')
[Link]("Scatter Plot: Study Hours vs Exam Score")
[Link]("Study Hours")
[Link]("Exam Score")
[Link](True)
[Link]()
x---------------------------------------------------------------------x
# 3. Bar Chart (Vertical)
products = ['A', 'B', 'C', 'D']
sales = [150, 200, 300, 250]
[Link](figsize=(6,4))
[Link](products, sales, color='green')
[Link]("Bar Chart: Product Sales")
[Link]("Product")
[Link]("Sales")
[Link]()
x---------------------------------------------------------------------x
# 4. Horizontal Bar Chart
countries = ['USA', 'India', 'China', 'Brazil']
population = [331, 1391, 1441, 213]
[Link](figsize=(6,4))
[Link](countries, population, color='orange')
[Link]("Horizontal Bar Chart: Country Population (Millions)")
[Link]("Population")
[Link]("Country")
[Link]()
x---------------------------------------------------------------------x
# 5. Histogram
ages = [22, 25, 27, 25, 30, 35, 40, 23, 33, 25, 29, 40, 50]
[Link](figsize=(6,4))
[Link](ages, bins=5, color='purple', edgecolor='black')
[Link]("Histogram: Age Distribution")
[Link]("Age")
[Link]("Frequency")
[Link]()
x---------------------------------------------------------------------x
# 6. Pie Chart
departments = ['HR', 'R&D', 'Marketing', 'IT']
budget = [20, 40, 25, 15]
[Link](figsize=(6,6))
[Link](budget, labels=departments, autopct='%1.1f%%', startangle=140)
[Link]("Pie Chart: Budget Allocation")
[Link]()
x---------------------------------------------------------------------x
# 7. Box and Whisker Plot
scores = [65, 70, 72, 68, 75, 80, 85, 90, 88, 95, 100]
[Link](figsize=(6,4))
[Link](scores)
[Link]("Boxplot: Test Scores")
[Link]("Score")
[Link]()
x---------------------------------------------------------------------x
# 8. Stack Plot
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
food = [200, 220, 250, 270, 300]
rent = [800, 800, 800, 800, 800]
utilities = [150, 160, 170, 180, 190]
[Link](figsize=(6,4))
[Link](months, food, rent, utilities, labels=['Food', 'Rent', 'Utilities'])
[Link]("Stack Plot: Monthly Expenses")
[Link]("Month")
[Link]("Cost")
[Link](loc='upper left')
[Link]()
Styling and Customization
import [Link] as plt
import numpy as np
days = [Link](1, 6)
actual = [10, 15, 13, 18, 16]
predicted = [11, 14, 14, 17, 17]
[Link](figsize=(6, 4))
[Link](days, actual, 'b-', label='Actual') # Solid blue line
[Link](days, predicted, 'r--', label='Predicted') # Red dashed line
[Link]("Stock Price: Actual vs Predicted")
[Link]("Day")
[Link]("Price")
[Link]()
[Link](True)
[Link]()
x---------------------------------------------------------------------x
[Link]('ggplot') # Apply global styling
study_hours = [1, 2, 3, 4, 5]
scores = [50, 55, 65, 70, 85]
[Link](figsize=(6, 4))
[Link](study_hours, scores, color='purple')
[Link]("Study Hours vs Exam Score")
[Link]("Study Hours")
[Link]("Score")
[Link](True)
[Link]()
x------------------------------------------------------------------------x
countries = ['USA', 'India', 'China', 'Brazil']
population = [331, 1391, 1441, 213]
[Link](figsize=(10, 6)) # Increase width and height
[Link](countries, population, color='orange')
[Link]("Population by Country")
[Link]("Population (Millions)")
[Link]("Country")
[Link]()
Numpy
arr = [Link](0, 11, 2)
print("Array with step 2:", arr)
x----------------------------------------------------------------x
arr = [Link](0, 1, 5)
print("Linearly spaced array:", arr)
x-----------------------------------------------------------------x
data = [Link]([10, 20, 30, 40])
print("Sum:", [Link](data))
print("Min:", [Link](data))
print("Max:", [Link](data))
Bar Chart Example
import [Link] as plt
# Data
categories = ['Math', 'Science', 'English', 'History']
scores = [88, 92, 79, 85]
# Plot
[Link](categories, scores, color='skyblue')
[Link]('Student Scores')
[Link]('Subjects')
[Link]('Marks')
[Link]()
Pie Chart Example
import [Link] as plt
#Data
activities = ['Sleep', 'Work', 'Exercise', 'Leisure']
hours = [8, 9, 2, 5]
# Plot
[Link](hours, labels=activities, autopct='%1.1f%%', startangle=90)
[Link]('Daily Activity Breakdown')
[Link]('equal') # Makes pie a circle
[Link]()
Scatter Plot Example
python
CopyEdit
import [Link] as plt
# Data
x = [5, 7, 8, 7, 2, 17, 2, 9]
y = [99, 86, 87, 88, 100, 86, 103, 87]
# Plot
[Link](x, y, color='red', marker='o')
[Link]('Age vs Score')
[Link]('Age')
[Link]('Score')
[Link](True)
[Link]()
Multiple Lines on One Graph
import [Link] as plt
# Data
days = [1, 2, 3, 4, 5]
temperature = [30, 32, 34, 33, 31]
humidity = [60, 65, 63, 62, 64]
# Plot multiple lines
[Link](days, temperature, label='Temperature (°C)', color='orange', marker='o')
[Link](days, humidity, label='Humidity (%)', color='blue', marker='s')
[Link]('Weather Report')
[Link]('Day')
[Link]('Measurement')
[Link]()
[Link](True)
[Link]()