Plotting - Ipynb in
Plotting - Ipynb in
ipynb - Colab
# pylab is a module that combines matplotlib.pyplot with numpy into a single namespace for convenience.
# it is generally discouraged because it imports many functions and variables globally, which may cause confusion or naming c
# While pylab is convenient for simple tasks, it's better to use matplotlib.pyplot directly for more complex projects
plot(x, y,'o') #
# omitting marker=, we can plot only points
# there should be no line connecting the points.
show()
avg_percentage = [50,55,49,60,59,68,70,79,81,75,85,90,95]
print(len(avg_percentage))
plot(avg_percentage)
show()
years=range(2010,2023)
plot(avg_percentage,years)
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 1/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
13
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 2/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
axis(ymin=0)
show()
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 3/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# Download the saved plot
files.download("pylab_plot.png")
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 4/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 5/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# NumPy
import numpy as np
a=np.array([2,3,5,7,11])
print(a)
type(a)
a
list1=[2,3,5,7,11]
print(list1)
# NumPy array it looks a lot like a Python list except that the entries are
# separated by spaces whereas entries in a Python list are separated by commas.
[ 2 3 5 7 11]
[2, 3, 5, 7, 11]
# Matplotlib and NumPy are essential libraries in Python for data visualization and numerical computing.
# NumPy: Used for creating and handling large, multi-dimensional arrays and matrices of data. It provides mathematical funct
# Matplotlib: Used for plotting graphs and visualizing data. Matplotlib works well with NumPy arrays for data organization a
# how to import
import numpy as np
import matplotlib.pyplot as plt
y1=np.sin(x)
y2=np.cos(x)
plt.plot(x, y1, label='Sine Wave', color='r')
plt.plot(x, y2, label='Cos Wave', color='b')
plt.legend()
plt.show
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 6/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
x=
# Bar Chart
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 5, 9]
plt.bar(categories, values)
plt.title("Bar Chart Example")
plt.xlabel('categories')
plt.ylabel('values')
plt.show()
Parameters
colors=['red','green','blue','purple']
----------
plt.bar(categories, values,color=colors)
block : bool, optional
plt.show()
Whether to wait for all figures to be closed before returning.
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 7/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# Contour plots show three-dimensional data in two dimensions by plotting constant values (contours).
# Use contour() for 2D contour plots.
X, Y = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-5, 5, 100))
Z = np.sin(np.sqrt(X**2 + Y**2))
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 8/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
#Fields
# Field plots, such as vector fields, use quiver() to visualize vector data.
X, Y = np.meshgrid(np.linspace(-5, 5, 10), np.linspace(-5, 5, 10))
U = -Y
V = X
plt.subplot(2, 2, 1)
plt.plot(x, np.sin(x),label='sin(x)',color='r') # First subplot (Sine wave)
plt.subplot(2, 2, 2)
plt.plot(x, np.cos(x),label='cos(x)',color='b') # Second subplot (Cosine wave)
plt.subplot(2, 2, 3)
plt.plot(x, np.sin(2*x),label='sin(2x)',color= 'r') # First subplot (Sine wave)
plt.subplot(2, 2, 4)
plt.plot(x, np.cos(2*x),label='cos(2x)',color='b') # Second subplot (Cosine wave)
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 9/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# subplots()
# This function returns a figure object and an array of axes objects for more advanced subplot management.
# fig, axs = plt.subplots(nrows, ncols, figsize, sharex, sharey)
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 10/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# nrows: Number of rows of subplots.
# ncols: Number of columns of subplots.
# fig: The figure object, which contains all subplots.
# fig: Represents the entire figure, which contains all subplots.
# axs: An array of axes objects, one for each subplot.
# axs: This is an array of axes objects (a 2D array in this case). Each element of axs corresponds to one subplot.#
plt.show()
# Histogram
import matplotlib.pyplot as plt
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 11/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# Pie Chart
plt.pie(sizes, labels=labels_1, autopct='%1.1f%%')
plt.title("Pie Chart Example")
plt.show()
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 12/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
import numpy as np
import matplotlib.pyplot as plt
# Example usage
Q0 = 100 # Initial quantity
lam = 0.1 # Decay constant
t = np.linspace(0, 10, 100) # Time range
plt.plot(t, Q_t)
plt.title('Exponential Decay')
plt.xlabel('Time')
plt.ylabel('Quantity')
plt.show()
# Animation of decay
# N = N_0 exp(-lamda *t) where N_0 is initial quantity, lamda is a rate and t is time
# Import required libraries:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML # For displaying the animation in Colab
# Decay function
def decay(t, N0, decay_rate):
result = N0 * np.exp(-decay_rate * t)
return result
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 13/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# fig, axs = plt.subplots(nrows, ncols, figsize, sharex, sharey)
# nrows: Number of rows of subplots.
# ncols: Number of columns of subplots.
# fig: The figure object, which contains all subplots.
# fig: Represents the entire figure, which contains all subplots.
# axs: An array of axes objects, one for each subplot.
# axs: This is an array of axes objects (a 2D array in this case). Each element of axs corresponds to one subplot.
# Set up figure and axis
fig, ax = plt.subplots()
ax.set_xlim(0, 10) # Time range from 0 to 10
ax.set_ylim(0, 1.0) # Decay value from 0 to 1
line, = ax.plot([], [], lw=2)
# Initialization function
def init():
line.set_data([], [])
return line,
# Update function
def update(frame):
t = np.linspace(0, frame, 100) # Generate time values
N = decay(t, N0=1, decay_rate=0.5) # Calculate decay values
line.set_data(t, N)
return line,
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 14/15
11/12/24, 10:54 PM Plotting.ipynb - Colab
# Bayes Theorem
# To create a set in Python, we can use the FiniteSet class from the sympy package,
from sympy import FiniteSet
s = FiniteSet(2, 4, 6)
print(s)
{2, 4, 6}
# Empty Set
s1=FiniteSet()
print(s1)
{1/5, 1, 1.5}
3
False
EmptySet
Once Loop Reflect
https://colab.research.google.com/drive/1sbeHOEYdNkpSSXSoX8Jn0OVEjL4xYsAe#printMode=true 15/15