|
| 1 | +""" |
| 2 | +Demonstrate the mixing of 2d and 3d subplots |
| 3 | +""" |
| 4 | +from mpl_toolkits.mplot3d import Axes3D |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +def f(t): |
| 9 | + s1 = np.cos(2*np.pi*t) |
| 10 | + e1 = np.exp(-t) |
| 11 | + return np.multiply(s1,e1) |
| 12 | + |
| 13 | + |
| 14 | +################ |
| 15 | +# First subplot |
| 16 | +################ |
| 17 | +t1 = np.arange(0.0, 5.0, 0.1) |
| 18 | +t2 = np.arange(0.0, 5.0, 0.02) |
| 19 | +t3 = np.arange(0.0, 2.0, 0.01) |
| 20 | + |
| 21 | +fig = plt.figure() |
| 22 | +fig.suptitle('A tale of 2 subplots') |
| 23 | +ax = fig.add_subplot(2, 1, 1) |
| 24 | +l = ax.plot(t1, f(t1), 'bo', |
| 25 | + t2, f(t2), 'k--', markerfacecolor='green') |
| 26 | +ax.grid(True) |
| 27 | +ax.set_ylabel('Damped oscillation') |
| 28 | + |
| 29 | + |
| 30 | +################# |
| 31 | +# Second subplot |
| 32 | +################# |
| 33 | +ax = fig.add_subplot(2, 1, 2, projection='3d') |
| 34 | +X = np.arange(-5, 5, 0.25) |
| 35 | +xlen = len(X) |
| 36 | +Y = np.arange(-5, 5, 0.25) |
| 37 | +ylen = len(Y) |
| 38 | +X, Y = np.meshgrid(X, Y) |
| 39 | +R = np.sqrt(X**2 + Y**2) |
| 40 | +Z = np.sin(R) |
| 41 | + |
| 42 | +surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, |
| 43 | + linewidth=0, antialiased=False) |
| 44 | + |
| 45 | +ax.set_zlim3d(-1, 1) |
| 46 | + |
| 47 | +plt.show() |
| 48 | + |
0 commit comments