150 likes | 168 Vues
Learn how to create 2D and 3D graphs using Python with different styles and markers for effective visualization. Includes line graphs, markers, multiple subplots, and 3D wireframe and surface plots.
E N D
PYTHON Graphs Prof. Muhammad Saeed
Graph 2D (Simple Line Graphs) Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D • import matplotlib.pyplot as plt • from numpy import arange, sin, exp, pi • LineGraph(Colors ): • x= arange(-12*pi, 12*pi, 0.1) • y=exp(-0.05*x)*sin(x) • plt.plot(x, y, ‘ob’) • plt.plot(x, y, ‘-r’) • plt.plot(x, y, ‘-sm’) • plt.plot(x,y, color=‘#ff0000’) • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D Color, Marker & Line Style Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D • import matplotlib.pyplot as plt • from numpy import arange, sin, exp, pi • LineGraph(Title and Labels ): • x= arange(-12*pi, 12*pi, 0.1) • y=exp(-0.05*x)*sin(x) • plt.plot(x, y, ‘b’) • plt.title(‘Graph Damped Oscillator’) • plt.xlabel(‘Angle’) • plt.ylabel(‘Oscillations’) • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D • Graph With Marker and Line Styles: • x= arange(-12*pi, 12*pi, 0.1) • y=exp(-0.05*x)*sin(x) • plt.plot(x, y, alpha = 0.5, color = ‘#FF7F00’, • label = ’Line Label’, linestyle= ‘-.’, linewidth = 3, • marker = ‘o’, markeredgecolor = ‘#000000’, • markeredgewidth= 2, markerfacecolor = ‘#FF7F00’, • markersize=10) • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D • import matplotlib.pyplot as plt • from numpy import arange, sin, cos, pi • MultipleGraphs in One Figure: • x= arange(-12*pi, 12*pi, 0.1) • y1=sin(x) • y2=cos(x) • y3=sin(x)/x • plt.plot(x, y1, x, y2, x, y3) • plt.legend(‘Sine’,’Cosine’,’Sine/Angle’).draggable(True) • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D • import matplotlib.pyplot as plt • from numpy import arange, sin, cos, pi • MultipleGraphs in One Figure: • x= arange(-12*pi, 12*pi, 0.1) • y1=sin(x) • y2=cos(x) • y3=sin(x)/x • plt.plot(x, y1) • plt.plot(x,y2) • plt.plot(x,y3) • plt.legend(‘Sine’,’Cosine’,’Sine/Angle’).draggable(True) • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 2D Multiple Subplots in a Window: • import matplotlib.pyplot as plt • Import numpy as np • fig, ax=plt.subplots(2,2, sharex='all' ) #, sharey=‘all’) • x= np.linspace(-12*pi, 12*pi, 600) • y1=np.sin(x) ; • y2=np.tan(x); • y3=np.sin(x)/x • y4=np.exp(-0.05*x)*np.sin(x) • ax=np.ravel(ax) • ax[0].plot(x,y1,'r') • ax[1].plot(x,y2,'g') • ax[2].plot(x,y3,'b') • ax[3].plot(x,y4,'m') Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 3D (Wireframe and surface) Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 3D Curves: • import matplotlib.pyplot as plt • Import numpy as np • from mpl_toolkits.mplot3d import Axes3D • x = linspace(-8*pi,8*pi,400) • y = x*sin(x) • x = 2*cos(x) • z = x*y • fig = plt.figure() • ax=fig.gca(projection='3d') • ax.plot(x,y,z) • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 3D Wireframe: • import matplotlib.pyplot as plt • Import numpy as np • from mpl_toolkits.mplot3d import Axes3D • fig = plt.figure() • ax = fig.add_subplot(111, projection='3d') • x=np.linspace(-7,7,200) • y=np.linspace(-7,7,200) • xx,yy=np.meshgrid(x,y) • zz=np.sin(np.sqrt(xx**2+yy**2))/np.sqrt(xx**2+yy**2) • ax.plot_wireframe(xx, yy, zz, rstride=5, cstride=5) Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 3D Surface: • import matplotlib.pyplot as plt • Import numpy as np • from mpl_toolkits.mplot3d import Axes3D • fig = plt.figure() • ax = fig.add_subplot(111, projection='3d') • x=np.linspace(-7,7,200) • y=np.linspace(-7,7,200) • xx,yy=np.meshgrid(x,y) • zz=np.sin(np.sqrt(xx**2+yy**2))/np.sqrt(xx**2+yy**2) • ax.plot_surface(xx, yy, zz, rstride=5, cstride=5) Python Dept. Of Comp. Sc. & IT, FUUAST
Graph 3D Surface: • import matplotlib.pyplot as plt • Import numpy as np • from mpl_toolkits.mplot3d import Axes3D • fig = plt.figure() • ax = fig.add_subplot(111, projection='3d') • u = np.linspace(0, 2 * np.pi, 100) • v = np.linspace(0, np.pi, 100) • x = 10 * np.outer(np.cos(u), np.sin(v)) • y = 10 * np.outer(np.sin(u), np.sin(v)) • z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) • ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b',cmap='jet') • ax.axis('square') • plt.show() Python Dept. Of Comp. Sc. & IT, FUUAST
END Python Dept. Of Comp. Sc. & IT, FUUAST