1.
Simple Line Plot
import [Link] as plt # Importing the plotting library
x = [1, 2, 3, 4] # X-axis data points
y = [10, 20, 25, 30] # Y-axis data points
[Link](x, y) # Create a line plot with x and y values
[Link]("Simple Line Plot") # Add a title to the plot
[Link]("X-axis") # Label for x-axis
[Link]("Y-axis") # Label for y-axis
[Link]() # Display the plot
🔹 Explanation: This program plots a simple line graph connecting points
(1,10), (2,20), etc. [Link]() must be called to render the plot window.
✅ 2. Simple Scatter Plot
[Link]([1, 2, 3, 4], [10, 20, 25, 30]) # Scatter plot of (x, y) points
[Link]("Simple Scatter Plot") # Title for the graph
[Link]() # Display the plot
🔹 Explanation: Each pair (x, y) is shown as a separate dot. Scatter plots
are used to analyze the relationship between two variables.
✅ 3. Pie Chart
labels = ['A', 'B', 'C', 'D'] # Category labels
sizes = [15, 30, 45, 10] # Size of each slice
[Link](sizes, labels=labels, autopct='%1.1f%%') # Plot pie chart with
percentage display
[Link]("Pie Chart")
[Link]()
🔹 Explanation: This creates a pie chart with 4 slices. autopct='%1.1f%%'
shows percentages on the chart (like 15.0%).
✅ 4. Box Plot
data = [7, 8, 5, 6, 9, 7, 8, 10, 15, 20]
[Link](data) # Create a boxplot to visualize data distribution
[Link]("Box Plot") # Title
[Link]()
🔹 Explanation: The boxplot shows median, quartiles, and outliers in the
dataset. It's useful for spotting spread and anomalies.
✅ 5. Matplotlib Grid
[Link](x, y) # Line plot
[Link](True) # Add gridlines
[Link]()
🔹 Explanation: [Link](True) makes it easier to read values from the plot.
✅ 6. Display Images
import numpy as np # Used to generate numerical array
img = [Link](10,10) # 10x10 matrix of random grayscale
values
[Link](img, cmap='gray') # Display the image with grayscale color
map
[Link]("Random Image")
[Link]() # Adds a scale bar
[Link]()
🔹 Explanation: This creates and displays a random grayscale image.
Each pixel gets a random brightness.
✅ 7. Tables
data = [[66386, 174296, 193263, 28924, 19670],
[115046, 57667, 47785, 79854, 20650]]
columns = ('Jan', 'Feb', 'Mar', 'Apr', 'May') # Column headers
rows = ['Tokyo', 'New York'] # Row labels
[Link](cellText=data, rowLabels=rows, colLabels=columns,
loc='center')
[Link]('off') # Hide axes
[Link]()
🔹 Explanation: This shows a table inside the plot area with city-wise
monthly data.
✅ 8. Date Handling
import [Link] as mdates
import datetime
dates = [[Link](2025, 7, i) for i in range(1, 6)]
values = [1, 3, 2, 5, 4]
[Link](dates, values)
[Link]().autofmt_xdate() # Format date labels nicely
[Link]()
🔹 Explanation: Plots values against calendar dates. autofmt_xdate() tilts
the date labels so they don’t overlap.
✅ 9. Log Plot
[Link]([1, 10, 100, 1000], [1, 2, 3, 4])
[Link]('log') # Set x-axis to log scale
[Link]("Logarithmic Plot")
[Link]()
🔹 Explanation: This is used when data grows exponentially. It compresses
wide-range x values.
✅ 10. Polar Plot
theta = [Link](0, 2*[Link], 100) # Angle values
r = [Link]([Link](5*theta)) # Radius values
[Link](theta, r) # Create a polar plot
[Link]("Polar Plot")
[Link]()
🔹 Explanation: This plots data in circular format. Useful for periodic data
like angles, waves, etc.
✅ 11. Visualizing Errors
x = [1, 2, 3, 4]
y = [2, 3, 5, 7]
errors = [0.2, 0.3, 0.2, 0.4]
[Link](x, y, yerr=errors, fmt='o') # 'o' = circle marker
[Link]("Error Bars")
[Link]()
🔹 Explanation: Shows uncertainty in each data point with error bars
above/below it.
✅ 12. Contour Plot
x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
Z = [Link](-X**2 - Y**2)
[Link](X, Y, Z) # Contour plot for 3D data
[Link]("Contour Plot")
[Link]()
🔹 Explanation: Plots lines of constant value (like elevation on a map).
✅ 13. Histogram
data = [Link](1000) # Random normal data
[Link](data, bins=30, density=True) # Histogram with 30 bins
[Link]("Histogram")
[Link]()
🔹 Explanation: Shows how often each range of values occurs.
density=True shows probability density.
✅ 14. Customizing Plot Legends
[Link](x, y, label='Line 1')
[Link](x, [i*2 for i in y], label='Line 2')
[Link](loc='upper left') # Add legend at upper-left
[Link]()
🔹 Explanation: Legends help identify different data series in the same
plot.
✅ 15. Customizing Colorbars
Editimg = [Link](10,10)
[Link](img, cmap='viridis')
[Link]() # Adds scale reference for color intensity
[Link]()
🔹 Explanation: Useful in heatmaps or image data to show the value
range.
✅ 16. Multiple Subplots
[Link](1, 2, 1) # 1 row, 2 columns, 1st plot
[Link](x, y)
[Link]("Plot 1")
[Link](1, 2, 2) # 2nd plot
[Link](x, y)
[Link]("Plot 2")
plt.tight_layout() # Avoid overlapping titles
[Link]()
🔹 Explanation: Two plots are shown side-by-side in the same figure
window.
✅ 17. Text and Annotation
[Link](x, y)
[Link](2, 20, "Sample Text", fontsize=12) # Add text
[Link]('Max', xy=(4, 30), xytext=(3, 25),
arrowprops=dict(facecolor='black', shrink=0.05)) # Annotate
with arrow
[Link]()
🔹 Explanation: Text helps label key points; annotate() can point to them
with arrows.
✅ 18. Customizing Ticks
[Link](x, y)
[Link]([1, 2, 3, 4], ['A', 'B', 'C', 'D']) # Rename x-tick labels
[Link]()
🔹 Explanation: Ticks are modified to show custom labels instead of
numbers.
✅ 19. Customizing Matplotlib Styles
[Link]('ggplot') # Apply pre-defined ggplot style
[Link](x, y)
[Link]()
🔹 Explanation: Styles like 'ggplot', 'seaborn' improve plot appearance
with a single line.