#! /usr/bin/env python # -*- coding: utf-8 -*- """ Demos and examples of the pylab environment. Used for my lectures on Scientific plotting, April 2015. More documentation online at https://github.com/rougier/matplotlib-tutorial. @date: Wed Jan 21 16:01:45 2015. @author: Lilian Besson for CS101 course at Mahindra Ecole Centrale 2015. @licence: GNU Public License version 3. """ # We import the pylab environment. # It is like calling %pylab in IPython from pylab import * # Default number of points for the plots n = 1500 # %% Test 1 (functions of x) # Creates a new figure figure(1) # Sample some points from -10 to 10 x = linspace(-10, 10, n) # And finally, plot cos and sin plot(x, cos(x), 'b-', x, sin(x), 'r-') title("Cosinus and sinus on the same plot.") # savefig("DemoPylab__figure1.svg", dpi=120) savefig("Demo_Pylab__Figure_1__Function_of_x.png", dpi=120) # %% Test 2 (param. curve) figure(2) axis('equal') t = linspace(0, 7*pi, n) plot(t - sin(t), 1 - cos(t)) title("Plot of a cycloid (t between $0$ and $7\pi$)") # savefig("DemoPylab__figure2.svg", dpi=120) savefig("Demo_Pylab__Figure_2__Parametric_curve.png", dpi=120) # %% Test 3 (param. curve 2) figure(3) axis('equal') n = 2000 t = linspace(-100, 100, n) plot(4.0*t/(3+t**4), 4.0*(t**3)/(sqrt(3)*(3+t**4))) title("Example of a parametric curve (from MA102 Tutorial sheet)") # savefig("DemoPylab__figure3.svg", dpi=120) savefig("Demo_Pylab__Figure_3__Parametric_curve_2.png", dpi=120) # %% Test 4 (polar curve 1) figure(4) axis('equal') theta = linspace(0, 2*pi, n) polar(theta, cos(theta)-cos(2*theta), label=r"$r(\theta) = \cos(\theta) - \cos(2\theta)$") legend(loc="right upper") title("Example of one polar curve") # savefig("DemoPylab__figure4.svg", dpi=120) savefig("Demo_Pylab__Figure_4__Polar_curve_1.png", dpi=120) # %% Test 5 (param. curve 3) figure(5) axis('equal') t = linspace(0, 2*pi, n) for i in [1, 2, 3, 5]: plot(cos(t)**i, sin(t)**i, label=("$i=%i$" % i)) legend() title(r"Curves $\Gamma_i = (\cos(t)^i, \sin(t)^i)$ for some values of $i$.") # savefig("DemoPylab__figure5.svg", dpi=120) savefig("Demo_Pylab__Figure_5__Polar_curve_2.png", dpi=120) # %% Test 6 (3D plotting) # This is the 3D plotting toolkit (Ref. https://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut5.html) from mpl_toolkits.mplot3d import Axes3D z = linspace(0, 1, 150) x = z * sin(20 * z) y = z * cos(20 * z) figure(6) ax = axes(projection='3d') ax.scatter(x, y, z, c=x+y) title("Scatter plot in 3D (color is depending on the position)") # savefig("DemoPylab__figure6.svg", dpi=120) savefig("Demo_Pylab__Figure_6__3D_scatter_plot.png", dpi=120) figure(7) ax = plt.axes(projection='3d') ax.plot(x, y, z, '-b') title("Line plot in 3D") # savefig("DemoPylab__figure7.svg", dpi=120) savefig("Demo_Pylab__Figure_7__3D_line_plot.png", dpi=120) # %% Test 7 (3D plotting, surface) figure(8) N = 30 x = outer(linspace(-pi/2, pi/2, N), ones(N)) y = x.copy().T # z = cos(x ** 2 + y ** 2) z = x ** 2 + y ** 2 ax = axes(projection='3d') ax.plot_surface(x, y, z, cmap='jet', rstride=1, cstride=1, linewidth=0) title("Surface in 3D (an hyperboloid $z = x^2 + y^2$)") # savefig("DemoPylab__figure8.svg", dpi=120) savefig("Demo_Pylab__Figure_8__3D_plotting_surface.png", dpi=120) # %% Test 8 (More options for plotting) figure(9) # x axis n = 10000 x = linspace(-2*pi, 2*pi, n) xlim(x.min()*1.1, x.max()*1.1) xticks([-2*pi, -pi, -pi/2, 0, pi/2, pi, 2*pi], [r"$-2\pi$", r"$-\pi$", r"$-\pi/2$", r"$0$", r"$\pi/2$", r"$\pi$", r"$2\pi$"]) # y axis C, S = cos(x), sin(x) ylim(C.min()*1.1, C.max()*1.1) yticks([-1, -0.5, 0.5, 1], [r"$-1$", r"$-1/2$", r"$1/2$", r"$1$"]) # Move spines ax = gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data', 0)) ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data', 0)) # Plotting plot(x, C, color='blue', linewidth=1, linestyle='-', label=r'$\cos$') plot(x, S, color='red', linewidth=4, linestyle=':', label=r'$\sin$') # Legend legend(loc='lower left') title("Plot of two functions, showing plenty of options") # Make the tick labels appear nicer for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(16) label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65)) # Save the figure # savefig("DemoPylab__figure9.svg") savefig("Demo_Pylab__Figure_9__Lot_of_options.png", dpi=120) # %% Test 10 (Scatter 2D points) figure(10) N = 500 # Random points X = np.random.normal(-2, 2, N) xlim(X.min(), X.max()) Y = np.random.normal(-2, 2, N) ylim(Y.min(), Y.max()) # Colors l = linspace(0, 1, N) scatter(X, Y, alpha=0.8, c=arctan2(Y, X), cmap='jet', linewidths=0.1, s=90) title("2D scatter plot of %i random points \n(colour depends on the polar angle)." % N) # savefig("DemoPylab__figure10.svg") savefig("Demo_Pylab__Figure_10__2D_Scatter_points.png", dpi=120) # %% Test 11 (Bar plots) figure(11) n = 12 X = np.arange(n) # Random Y data Y1 = (1-X/float(n)) * np.random.uniform(0.5, 1.0, n) Y2 = (1-X/float(n)) * np.random.uniform(0.5, 1.0, n) bar(X, +Y1, facecolor='#9999ff', edgecolor='white') bar(X, -Y2, facecolor='#ff9999', edgecolor='white') # Write values of each bar for x, y in zip(X, Y1): text(x+0.4, y+0.05, "%.2f" % y, ha='center', va='bottom') for x, y in zip(X, -Y2): text(x+0.4, y-0.05, "%.2f" % y, ha='center', va='top') ylim(-1.25, +1.25) title("Random data displayed as two bar plots") # savefig("DemoPylab__figure11.svg") savefig("Demo_Pylab__Figure_11__Bar_plot.png", dpi=120) # %% Test 12 (Contour Plots) figure(12) f = lambda x, y: (1-x/2+x**5+y**3)*exp(-x**2-y**2) n = 512 k = 8 # axis('equal') x = linspace(-3, 3, n) y = linspace(-3, 3, n) X, Y = meshgrid(x, y) title("2D contour plot based on a function $f(x, y)$") contourf(X, Y, f(X, Y), k, alpha=.75, cmap='hot') C = contour(X, Y, f(X, Y), k, colors='black', linewidth=.5) clabel(C, inline=1, fontsize=10) xticks([]) yticks([]) # savefig("DemoPylab__figure12.svg") savefig("Demo_Pylab__Figure_12__Contour_plot_3D_surface.png", dpi=120) # %% Test 13 (One more 3D plot) fig = figure(13) from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(fig) k = 4 n = 64 X = linspace(-k, k, n) Y = linspace(-k, k, n) X, Y = meshgrid(X, Y) R = sqrt(X**2 + Y**2) Z = sin(R) title(r"3D surface ($z = \sin(\sqrt{x^2 + y^2})$ and its projection on $xOy$ plane") ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot') ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap='winter') ax.set_zlim(-2, 2) # savefig("DemoPylab__figure_13.svg") savefig("Demo_Pylab__Figure_13__3D_surface_and_projection.png", dpi=120)