π¨ Mastering Graph Plotting in Python with Matplotlib
- Get link
- X
- Other Apps
Welcome to your creative journey into plotting graphs in Python! π
Today, we'll dive deep into Matplotlib, a powerful, flexible library
that makes data visualization in Python super fun. We'll cover everything from scatter
plots to Monte Carlo simulations — with interactive challenges
and fun facts along the way!
π 1. Plotting Graphs in Python: An Overview
Matplotlib is a comprehensive library for
creating static, interactive, and animated graphics in
Python.
You can use modules like pyplot and pylab to easily create a
variety of graphs.
- Pylab
mixes numpy and pyplot features into a single workspace.
- But pyplot
is generally preferred for non-interactive plotting.
π️ 1.1 Scatter Plots: Let's Paint Some Dots!
First, let's create a simple scatter plot:
Changing color, size, titles, and labels makes your
graph pop:
π 1.2 Plotting Explicit Functions
Let's plot the graph of
in the interval
π¨ 1.3 Plot Styles: Make it Fancy!
Explore different line styles:
π Did You Know?
Matplotlib was originally created by John D. Hunter in 2003 to replicate MATLAB’s plotting capabilities — but for free and open-source! ππ§© 1.4 Titles, Labels, Grids: Polishing Your Plot
Add important plot elements:
A little polish makes a big difference!
π 1.5 Plotting Multiple Graphs Together
Sometimes you need to compare different functions on
the same graph:
Or with legends:
πΌ️ 1.6 Subplots: Tiny Worlds in One Figure
Multiple graphs in a grid layout:
π Did You Know?
You can useπ 1.7 Parametric Plots: Beautiful Curves
Example: Figure-Eight Shape!
π 1.8 Random Walk: Art Made from Randomness
Create a random walk — a path where each step is random:
π² 1.9 Monte Carlo Simulation: Estimating Pi (Ο)
Estimate Ο by throwing random darts π―
inside a square and counting how many land inside the circle:
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random
def Pi_MonteCarlo(maxItr):
sqrX = [1, -1, -1,
1, 1]
sqrY = [1, 1, -1,
-1, 1]
cirX, cirY = [],
[]
for i in
range(361):
cirX.append(np.cos(np.pi * i/180))
cirY.append(np.sin(np.pi * i/180))
insideX, insideY,
outsideX, outsideY = [], [], [], []
insideCount = 0
for i in
range(maxItr):
x =
2*(random() - 0.5)
y =
2*(random() - 0.5)
if
np.sqrt(x**2 + y**2) <= 1:
insideCount += 1
insideX.append(x)
insideY.append(y)
else:
outsideX.append(x)
outsideY.append(y)
piValue = 4 *
insideCount / maxItr
plt.figure(figsize=(5, 5))
plt.scatter(insideX, insideY, color='g', marker=".", s=5)
plt.scatter(outsideX, outsideY, color='b', marker=".", s=5)
plt.plot(sqrX,
sqrY, color='y')
plt.plot(cirX,
cirY, color='r')
plt.xlabel('x')
plt.ylabel('y')
plt.title(f'Approximate Ο ≈ {piValue}')
plt.show()
print(f'Approximate value of pi is {piValue}')
Pi_MonteCarlo(100000)
π ️ 1.10 Other Cool Plots to Explore
Matplotlib has even more plot types:
Plot Type |
Function |
Polar plot |
|
Bar plot |
|
Histogram plot |
|
Pie chart |
|
Box plot |
|
π Did You Know?
Matplotlib also has a 3D plotting toolkit called mpl_toolkits.mplot3d for stunning 3D graphs! π₯π Coming Up Next: Fun Challenges and Creative Plotting Projects!
Get ready to elevate your plotting skills with hands-on practice challenges:- We'll
create a stunning parametric curve, diving into the magic of trigonometric
and periodic functions.
- Discover
the beauty of calculus as we plot a function and its derivative side by
side.
- Explore
the power of visualizing real-world data with bar plots and pie charts,
focusing on meaningful insights from COVID data.
- Dive into statistics by generating normally distributed random numbers, complete with a histogram to visualize their spread.
- Get link
- X
- Other Apps
Comments
Post a Comment
If you have any queries, do not hesitate to reach out.
Unsure about something? Ask away—I’m here for you!