0% found this document useful (0 votes)
4 views

02 practicle bar graphs

The document contains a Jupyter Notebook with various Python code snippets that utilize NumPy and Matplotlib to create different types of plots. It includes 3D surface plots for mathematical functions such as log, sine, and cosine, as well as 2D plots for functions and bar graphs representing data on subjects, expenditures, and student participation in games. Each section of code generates a corresponding visual representation of the data or mathematical function.

Uploaded by

lelenib296
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

02 practicle bar graphs

The document contains a Jupyter Notebook with various Python code snippets that utilize NumPy and Matplotlib to create different types of plots. It includes 3D surface plots for mathematical functions such as log, sine, and cosine, as well as 2D plots for functions and bar graphs representing data on subjects, expenditures, and student participation in games. Each section of code generates a corresponding visual representation of the data or mathematical function.

Uploaded by

lelenib296
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 1/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [1]: import numpy as np


import matplotlib.pyplot as plt

# Define the function f(x, y)
def f(x, y):
return np.log(x**2 + y**2)

# Generate x and y values
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)

# Calculate the corresponding z values using the function f(x, y)
Z = f(X, Y)

# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot surface
surf = ax.plot_surface(X, Y, Z, cmap='Greens')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('f(X, Y)')
ax.set_title('3D Surface Plot for f(X, Y) = log(X^2 + Y^2)')

# Add color bar
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)

# Show plot
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 2/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 3/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [2]: import numpy as np


import matplotlib.pyplot as plt

# Define the function f(x, y)
def f(x, y):
return np.sin(x**2 + y**2)

# Generate x and y values
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)

# Calculate the corresponding z values using the function f(x, y)
Z = f(X, Y)

# Create a new figure
fig = plt.figure()

# Create a 3D subplot
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
surf = ax.plot_surface(X, Y, Z, cmap='viridis')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('f(X, Y)')
ax.set_title('3D Surface Plot for f(X, Y) = sin(X^2 + Y^2)')

# Add color bar
fig.colorbar(surf, shrink=0.5, aspect=5)

# Show the plot
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 4/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 5/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [2]: import numpy as np


import matplotlib.pyplot as plt

# Define the function f(x)
def f(x):
return np.exp(-x**2)

# Generate x values
x = np.linspace(-5, 5, 100)

# Calculate the corresponding y values using the function f(x)
y = f(x)

# Create a new figure
fig = plt.figure()

# Add subplot
ax = fig.add_subplot(111, projection='3d')

# Plot the line with green dashed lines and upward pointing triangle markers
ax.plot(x, np.zeros_like(x), y, linestyle='--', marker='^', color='green')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Plot of f(x) = e^(-x^2)')

# Show the plot
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 6/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 7/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [4]: import numpy as np


import matplotlib.pyplot as plt

# Define the function z = cos(x^2) + y^2 - 0.5
def f(x, y):
return np.cos(x**2) + y**2 - 0.5

# Generate x and y values
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x, y)

# Calculate the corresponding z values using the function f(x, y)
Z = f(X, Y)

# Create a new figure
fig = plt.figure()

# Add subplot
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
surf = ax.plot_surface(X, Y, Z, cmap='viridis')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Surface Plot of z = cos(x^2) + y^2 - 0.5')

# Add color bar
fig.colorbar(surf, shrink=0.5, aspect=5)

# Show the plot
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 8/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 9/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [5]: import numpy as np


import matplotlib.pyplot as plt

# Define the function f(x, y)
def f(x, y):
return np.sin(x**2) + y**2

# Generate x and y values
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)

# Calculate the corresponding z values using the function f(x, y)
Z = f(X, Y)

# Create the 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
surf = ax.plot_surface(X, Y, Z, cmap='viridis')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Surface Plot for f(x, y) = sin(x^2) + y^2')

# Add color bar
fig.colorbar(surf, shrink=0.5, aspect=5)

# Show the plot
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 10/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 11/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [6]: import matplotlib.pyplot as plt



# Data
subjects = ['Maths', 'Science', 'English', 'Marathi', 'Hindi']
percentages = [68, 90, 70, 85, 91]

# Plotting the bar graph
plt.figure(figsize=(8, 6))
plt.bar(subjects, percentages, color='green')

# Adding labels and title
plt.xlabel('Subjects')
plt.ylabel('Percentage of Passing')
plt.title('Percentage of Passing in Different Subjects')

# Displaying the graph
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 12/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 13/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [7]: import matplotlib.pyplot as plt



# Data
items = ['Clothing', 'Food', 'Rent', 'Petrol', 'Misc. Expenditure']
expenditure = [600, 4000, 2000, 1500, 700]
colors = ['blue', 'green', 'red', 'purple', 'orange']

# Plotting the bar graph
plt.figure(figsize=(8, 6))
plt.bar(items, expenditure, color=colors)

# Adding labels and title
plt.xlabel('Items')
plt.ylabel('Expenditure (Rs)')
plt.title('Expenditure on Different Items')

# Displaying the graph
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 14/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 15/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [11]: import matplotlib.pyplot as plt



# Data
games = ['Cricket', 'Football', 'Hockey', 'Chess', 'Tennis']
students = [65, 30, 54, 10, 20]

# Plotting the bar graph
plt.figure(figsize=(6, 6))
plt.bar(games, students, width=0.7)

# Adding labels and title
plt.xlabel('Games')
plt.ylabel('Number of Students')
plt.title('Number of Students Participating in Various Games')

# Displaying the graph
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 16/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [12]: import numpy as np


import matplotlib.pyplot as plt

# Define the function f(x)
def f(x):
return np.cos(x)

# Generate x values in the interval [0, 2*pi]
x = np.linspace(0, 2*np.pi, 100)

# Calculate the corresponding y values using the function f(x)
y = f(x)

# Plot the graph
plt.plot(x, y)

# Set labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = cos(x)')

# Show the plot
plt.grid(True)
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 17/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

localhost:8890/notebooks/Untitled1.ipynb 18/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [14]: import numpy as np


import matplotlib.pyplot as plt

# Define the function z = sin(x) + cos(y)
def f(x, y):
return np.sin(x) + np.cos(y)

# Generate x and y values
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(x, y)

# Calculate the corresponding z values using the function f(x, y)
Z = f(X, Y)

# Create a new figure
fig = plt.figure()

# Create a 3D subplot
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
surf = ax.plot_surface(X, Y, Z, cmap='viridis')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Plot for z = sin(x) + cos(y)')

# Add color bar
fig.colorbar(surf, shrink=0.5, aspect=5)

# Show the plot
plt.show()

localhost:8890/notebooks/Untitled1.ipynb 19/20
4/8/24, 11:19 PM Untitled1 - Jupyter Notebook

In [ ]: ​

localhost:8890/notebooks/Untitled1.ipynb 20/20

You might also like