UNIT V
Plotting Functions in Python (Google Colab)
Introduction
Plotting is a vital skill in data visualization. In Python, the Matplotlib library is the most
widely used tool for creating static, animated, and interactive plots. Google Colab
provides an excellent environment for running and visualizing Python code directly in the
browser.
1. Getting Started with Matplotlib
Import the necessary libraries:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
- matplotlib.pyplot is used for 2D plotting.
- %matplotlib inline displays plots within the notebook.
Basic structure of a plot:
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plot Title')
plt.show()
2. Line Plot
Used to represent continuous data or trends over time.
import numpy as np
import matplotlib.pyplot as plt
x_line = [1, 2, 3, 4, 5]
y_line = [2, 4, 6, 8, 10]
plt.figure(figsize=(6, 4))
plt.plot(x_line, y_line)
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
3. Scatter Plot
Used to visualize the relationship between two numerical variables.
import numpy as np
import matplotlib.pyplot as plt
x_line = [1, 2, 3, 4, 5]
y_line = [2, 4, 6, 8, 10]
plt.figure(figsize=(6, 4))
plt.plot(x_line, y_line)
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
4. Bar Chart
Represents categorical data with rectangular bars.
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C', 'D']
values = [10, 7, 12, 5]
plt.figure(figsize=(6, 4))
plt.bar(categories, values)
plt.title('Simple Bar Plot')
plt.xlabel('Category')
plt.ylabel('Value')
plt.show()
5. Histogram
Used to show the frequency distribution of data.
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(1000)
plt.hist(data, bins=30, color='lightgreen', edgecolor='black')
plt.title('Histogram Example')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
6. Box Plot
Displays spread and identifies outliers.
import numpy as np
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C', 'D']
values = [10, 7, 12, 5]
plt.figure(figsize=(6, 4))
plt.bar(categories, values)
plt.title('Simple Bar Plot')
plt.xlabel('Category')
plt.ylabel('Value')
plt.show()
7. Pie Chart
Represents data as proportions of a whole.
import numpy as np
import matplotlib.pyplot as plt
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [20, 35, 30, 15]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart Example')
plt.show()
8. Subplots
Used for multiple plots within one figure.
import matplotlib.pyplot as plt
import numpy as np
# 1. Create a figure and a grid of subplots (1 row, 2 columns)
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
# Sample data
x_line = np.linspace(0, 10, 100)
y_line = np.sin(x_line)
x_scatter = np.random.rand(50) * 10
y_scatter = np.random.rand(50) * 10
# 2. On the first subplot, generate a simple line plot
axes[0].plot(x_line, y_line)
# 4. Add a title to the first subplot
axes[0].set_title('Line Plot')
# 5. Add labels to the x and y axes of the first subplot
axes[0].set_xlabel('X-axis')
axes[0].set_ylabel('Y-axis')
# 3. On the second subplot, generate a simple scatter plot
axes[1].scatter(x_scatter, y_scatter)
# 4. Add a title to the second subplot
axes[1].set_title('Scatter Plot')
# 5. Add labels to the x and y axes of the second subplot
axes[1].set_xlabel('X-axis')
axes[1].set_ylabel('Y-axis')
# Adjust layout to prevent labels overlapping
plt.tight_layout()
# 6. Display the figure containing the subplots
plt.show()
9. Styling and Customization
Common attributes:
- Colors: 'r', 'g', 'b', 'k', 'c', 'm', 'y'
- Line styles: '-', '--', ':', '-.'
- Markers: 'o', 's', '^', '*', 'x'
Example:
plt.plot(x, np.sin(x), 'r--o', label='sin(x)')
plt.plot(x, np.cos(x), 'b-.s', label='cos(x)')
plt.legend()
plt.show()
Customization in various graphs
import numpy as np
import matplotlib.pyplot as plt
# 1. Create sample data
x_data = np.linspace(0, 10, 100)
y_data_1 = np.sin(x_data)
y_data_2 = np.cos(x_data)
categories = ['Category A', 'Category B', 'Category C', 'Category D',
'Category E']
values = [25, 40, 30, 35, 50]
# 2. Generate a line plot and customize it
plt.figure(figsize=(8, 5))
plt.plot(x_data, y_data_1)
plt.title('Customized Line Plot', fontsize=16)
plt.xlabel('X-axis Label', fontsize=12)
plt.ylabel('Y-axis Label', fontsize=12)
plt.grid(True)
plt.show()
# 3. Generate a scatter plot and customize marker style and color
plt.figure(figsize=(8, 5))
plt.scatter(x_data[::10], y_data_1[::10], marker='X', color='red', s=100)
# Using a subset of data for scatter
plt.title('Customized Scatter Plot', fontsize=16)
plt.xlabel('X-axis Label', fontsize=12)
plt.ylabel('Y-axis Label', fontsize=12)
plt.grid(True)
plt.show()
# 4. Generate a bar plot and change the bar color
plt.figure(figsize=(8, 5))
plt.bar(categories, values, color=['skyblue', 'lightcoral', 'lightgreen' , 'gold', 'orchid'])
plt.title('Customized Bar Plot', fontsize=16)
plt.xlabel('Categories', fontsize=12)
plt.ylabel('Values', fontsize=12)
plt.show()
# 5. Create a plot with multiple lines and add a legend
plt.figure(figsize=(8, 5))
plt.plot(x_data, y_data_1, label='sin(x)', linestyle='--')
plt.plot(x_data, y_data_2, label='cos(x)', linestyle='-.')
plt.title('Plot with Multiple Lines and Legend', fontsize=16)
plt.xlabel('X-axis Label', fontsize=12)
plt.ylabel('Y-axis Label', fontsize=12)
plt.legend(loc='upper right')
plt.grid(True)
plt.show()
10. Text and Annotations
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.annotate('Max Point', xy=(np.pi/2, 1), xytext=(2, 1.2),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
11. Saving Figures
plt.savefig('plot_example.png', dpi=300, bbox_inches='tight')
12. Interactive Plotting (Optional – Plotly)
!pip install plotly
import plotly.express as px
import pandas as pd
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
fig.show()
13. Practice Exercises
1. Plot quadratic and cubic functions with proper titles and legends.
2. Create a histogram of 1000 random numbers.
3. Draw a grouped bar chart comparing two datasets.
4. Generate a 2×2 subplot (line, scatter, histogram, boxplot).
5. Add annotations to mark the maximum point in a sine curve.
Summary Table
Plot Type Function Purpose
Line Plot plt.plot() Shows trends or changes
Scatter Plot plt.scatter() Correlation between
variables
Bar Chart plt.bar() Comparison of categories
Histogram plt.hist() Frequency distribution
Box Plot plt.boxplot() Spread and outliers
Pie Chart plt.pie() Composition or percentage
Matplotlib offers flexible tools to visualize data effectively. Start with simple plots, learn
customization, and then move to subplots and interactive charts.