|
| 1 | +''' |
| 2 | +Demonstrates using ax.plot's zdir keyword to plot 2D scatterplot data on |
| 3 | +selective axes of a 3D plot. |
| 4 | +''' |
| 5 | + |
1 | 6 | from mpl_toolkits.mplot3d import Axes3D
|
2 | 7 | import numpy as np
|
3 | 8 | import matplotlib.pyplot as plt
|
4 | 9 |
|
5 | 10 | fig = plt.figure()
|
6 | 11 | ax = fig.gca(projection='3d')
|
7 | 12 |
|
| 13 | +# Plot a sin curve using the x and y axes. |
8 | 14 | x = np.linspace(0, 1, 100)
|
9 | 15 | y = np.sin(x * 2 * np.pi) / 2 + 0.5
|
10 |
| -ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z') |
| 16 | +ax.plot(x, y, zs=0, zdir='z', label='curve in (x,y)') |
11 | 17 |
|
| 18 | +# Plot scatterplot data (20 2D points per colour) on the x and z axes. |
12 | 19 | colors = ('r', 'g', 'b', 'k')
|
| 20 | +x = np.random.sample(20*len(colors)) |
| 21 | +y = np.random.sample(20*len(colors)) |
| 22 | +c_list = [] |
13 | 23 | for c in colors:
|
14 |
| - x = np.random.sample(20) |
15 |
| - y = np.random.sample(20) |
16 |
| - ax.scatter(x, y, 0, zdir='y', c=c) |
| 24 | + c_list.append([c]*20) |
| 25 | +# By using zdir='y', the y value of these points is fixed to the zs value 0 |
| 26 | +# and the (x,y) points are plotted on the x and z axes. |
| 27 | +ax.scatter(x, y, zs=0, zdir='y', c=c_list, label='points in (x,z)') |
17 | 28 |
|
| 29 | +# Make legend, set axes limits and labels |
18 | 30 | ax.legend()
|
19 | 31 | ax.set_xlim3d(0, 1)
|
20 | 32 | ax.set_ylim3d(0, 1)
|
21 | 33 | ax.set_zlim3d(0, 1)
|
| 34 | +ax.set_xlabel('X') |
| 35 | +ax.set_ylabel('Y') |
| 36 | +ax.set_zlabel('Z') |
| 37 | + |
| 38 | +# Customize the view angle so it's easier to see that the scatter points lie |
| 39 | +# on the plane y=0 |
| 40 | +ax.view_init(elev=20., azim=-35) |
22 | 41 |
|
23 | 42 | plt.show()
|
0 commit comments