0% found this document useful (0 votes)
8 views12 pages

2D & 3D Graphs

The document contains Python code snippets for plotting various mathematical functions using NumPy and Matplotlib, including logarithmic, trigonometric, algebraic, exponential, and inverse trigonometric functions. It also demonstrates how to create different types of graphs such as line graphs, bar graphs, histograms, pie charts, scatter plots, and 3D plots. Each section includes code for generating and displaying the respective plots.

Uploaded by

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

2D & 3D Graphs

The document contains Python code snippets for plotting various mathematical functions using NumPy and Matplotlib, including logarithmic, trigonometric, algebraic, exponential, and inverse trigonometric functions. It also demonstrates how to create different types of graphs such as line graphs, bar graphs, histograms, pie charts, scatter plots, and 3D plots. Each section includes code for generating and displaying the respective plots.

Uploaded by

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

# Logarithmic function #

import numpy as np

import matplotlib.pyplot as plt

x=np.linspace(0,10) # np.linspace() : initializes the interval on which this function is defined #

y=np.log10(x) # Compute values of logarithm to base 10 #

plt.plot(x,y) # Plot the points on the canvas #

plt.show() # Display the plot #

# Trigonometric function #

import numpy as np

import matplotlib.pyplot as plt

x=np.arange(0,2*(np.pi),0.01) # np.arange() : used to plot points in the specified interval with specified distance #

y=np.cos(x) # Compute values of cosine function #

plt.plot(x,y) # Plot the points on the canvas #

plt.show() # Display the plot #

# Sine function #

import numpy as np

import matplotlib.pyplot as plt

x=np.arange(0,(np.pi/2),0.1) # np.arange() : used to plot points in the specified interval with specified
distance #

y=np.tan(x) # Compute values of cosine function #

plt.plot(x,y) # Plot the points on the canvas #

plt.show()
# Inverse trigonometric functions #

import numpy as np

import matplotlib.pyplot as plt

x=np.arange(-1,1,0.1) # np.arange() : used to plot points in the specified interval with specified distance #

y=np.arcsin(x) # Compute values of cosine function #

plt.plot(x,y) # Plot the points on the canvas #

plt.show()

# Algebraic function #

import numpy as np

import matplotlib.pyplot as plt

x=np.linspace(-10,10) # np.linspace() : initializes the interval on which this function is defined #

y=x**2 # Compute values of logarithm to base 10 #

plt.plot(x ,y) # Plot the points on the canvas #

plt.show() # Display the plot #

# Exponential function #

import numpy as np

import matplotlib.pyplot as plt

x=np.linspace(-1,2) # np.linspace() : initializes the interval on which this function is defined #

y=np.exp(x) # Compute values of logarithm to base 10 #

plt.plot(x,y) # Plot the points on the canvas #

plt.show() # Display the plot #


# LIATE function#

import numpy as np

import matplotlib.pyplot as plt

x=np.arange(0,2*(np.pi),0.1) # np.linspace() : initializes the interval on which this function is defined #

y=np.sin(x)-np.exp(x)+3*x**2+np.log10(x)

plt.plot(x,y) # Plot the points on the canvas #

plt.show() # Display the plot #

# Line Graph #

import matplotlib.pyplot as plt

x1=[1,2,3]

y1=[3,6,2]

plt.plot(x1,y1,label= "line 1")

x2=[1,2,3]

y2=[7,2,5]

plt.plot(x2,y2,label= "line 2")

plt.xlabel("x-axis")

plt.ylabel("y-axis")

plt.title("Two lines on the same graph")

plt.legend()

plt.show()

# Bar Graph #

import matplotlib.pyplot as plt

left=[1,2,3,4,5]
height = [5,24,45,32,15]

tick_label=['Pune','Mumbai','Nagpur','Nasik','Satara']

plt.bar(left, height, tick_label = tick_label, width=0.8,color=['red', 'green',])

plt.xlabel('cities')

plt.ylabel('No of covid patients')

plt.title('covid-19 data')

plt.show()

# Histogram #

import matplotlib.pyplot as plt

ages=[2,5,70,40,30,45,50,45,43,40,44,60,7,13,57,18,90,77,32,21,20,40,45,32,38]

range=(0,100)

bins=5 # Number of bars #

plt.hist(ages,bins,range,color='blue',histtype='bar',rwidth=0.9)

plt.xlabel('ages')

plt.ylabel('Number of people')

plt.title('Histogram plot')

plt.show()

# Pie Chart #

import matplotlib.pyplot as plt

left=[1,2,3,4,5]

height=[5,24,45,32,15]

tick_label= ['Pune','Mumbai','Nagpur','Nasik','Satara']

fig= plt.figure(figsize=(7,7)) # Size of figure either circle or ellipse #

plt.pie(height,lab els=tick_label)

plt.show()
# Scatter plot #

import matplotlib.pyplot as plt

girls_scores=[81,90,70,89,100,80,90,100,80,54]

boys_scores=[30,29,49,48,100,48,34,45,20,30]

grades_range=[10,20,30,40,50,60,70,80,90,100]

fig=plt.figure()

ax=fig.add_axes([0,0,1,1]) # Size of scatter plot #

ax.scatter(grades_range,girls_scores,color='red')

ax.scatter(grades_range,boys_scores,color='blue')

ax.set_xlabel('grades range')

ax.set_ylabel('grades scored')

ax.set_title('scatter plot')

plt.show()

legend()

show()
# 3- Dimensional frame #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

ax=plt.axes(projection='3d')

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

ax=plt.axes(projection='3d')

zvalue=np.linspace(0,15,10)

xvalue=np.sin(zvalue)

yvalue=np.cos(zvalue)

ax.plot3D(xvalue,yvalue,zvalue,'red')

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

ax=plt.axes(projection='3d')
zvalue=np.linspace(0,15,50)

xvalue=np.sin(zvalue)

yvalue=np.cos(zvalue)

ax.scatter3D(xvalue,yvalue,zvalue,'red')

# 3D graph of exp(-x**2-y**2) #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

def f(x,y):

return np.exp(-x**2-y**2)

x=np.linspace(-2,2,100)

y=np.linspace(-2,2,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')

ax.contour3D(X,Y,Z,50,cmap='RdBu')

ax.set_xlabel('x')

ax.set_xlabel('y')

ax.set_xlabel('z')

# Wireframe#

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np
fig=plt.figure()

def f(x,y):

return np.exp(-x**2-y**2)

x=np.linspace(-2,2,100)

y=np.linspace(-2,2,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')

ax.plot_wireframe(X,Y,Z,50,color='red')

ax.set_xlabel('x')

ax.set_xlabel('y')

ax.set_xlabel('z')

# sin(x**2+y**2) #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

def f(x,y):

return np.sin(x**2+y**2)

x=np.linspace(-2,2,100)

y=np.linspace(-2,2,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')
ax.contour3D(X,Y,Z,50,cmap='RdBu')

ax.set_xlabel('x')

ax.set_xlabel('y')

ax.set_xlabel('z')

# exp(-x**2) #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

def f(x,y):

return np.exp(-x**2)

x=np.linspace(-2,2,100)

y=np.linspace(-2,2,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')

ax.contour3D(X,Y,Z,50,cmap='RdBu')

ax.set_xlabel('x')

ax.set_xlabel('y')

ax.set_xlabel('z')

# sinx+ cosy #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()
def f(x,y):

return np.sin(x)+np.cos(y)

x=np.linspace(-10,10,100)

y=np.linspace(-10,10,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')

ax.contour3D(X,Y,Z,50,cmap='RdBu')

ax.set_xlabel('x')

ax.set_xlabel('y')

ax.set_xlabel('z')

# x**2+y**2 #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

def f(x,y):

return (x**2+y**2)

x=np.linspace(-10,10,100)

y=np.linspace(-10,10,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')

ax.contour3D(X,Y,Z,50,cmap='RdBu')

ax.set_xlabel('x')
ax.set_xlabel('y')

ax.set_xlabel('z')

# sin(x**2+y**2) #

from mpl_toolkits import mplot3d

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

def f(x,y):

return np.sin(x**2+y**2)

x=np.linspace(-10,10,100)

y=np.linspace(-10,10,100)

X,Y=np.meshgrid(x,y)

Z=f(X,Y)

ax=plt.axes(projection='3d')

ax.contour3D(X,Y,Z,50,cmap='RdBu')

ax.set_xlabel('x')

ax.set_xlabel('y')

ax.set_xlabel('z')

You might also like