Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2208665

Browse files
authored
Merge pull request #13534 from anntzer/more-mplot3d
More followup to autoregistering 3d axes.
2 parents cd3ed7a + 73cb465 commit 2208665

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

examples/animation/random_walk.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,29 @@
77

88
import numpy as np
99
import matplotlib.pyplot as plt
10-
import mpl_toolkits.mplot3d.axes3d as p3
1110
import matplotlib.animation as animation
1211

1312
# Fixing random state for reproducibility
1413
np.random.seed(19680801)
1514

1615

17-
def Gen_RandLine(length, dims=2):
16+
def gen_rand_line(length, dims=2):
1817
"""
1918
Create a line using a random walk algorithm
2019
2120
length is the number of points for the line.
2221
dims is the number of dimensions the line has.
2322
"""
24-
lineData = np.empty((dims, length))
25-
lineData[:, 0] = np.random.rand(dims)
23+
line_data = np.empty((dims, length))
24+
line_data[:, 0] = np.random.rand(dims)
2625
for index in range(1, length):
2726
# scaling the random numbers by 0.1 so
2827
# movement is small compared to position.
2928
# subtraction by 0.5 is to change the range to [-0.5, 0.5]
3029
# to allow a line to move backwards.
31-
step = ((np.random.rand(dims) - 0.5) * 0.1)
32-
lineData[:, index] = lineData[:, index - 1] + step
33-
34-
return lineData
30+
step = (np.random.rand(dims) - 0.5) * 0.1
31+
line_data[:, index] = line_data[:, index - 1] + step
32+
return line_data
3533

3634

3735
def update_lines(num, dataLines, lines):
@@ -41,12 +39,13 @@ def update_lines(num, dataLines, lines):
4139
line.set_3d_properties(data[2, :num])
4240
return lines
4341

42+
4443
# Attaching 3D axis to the figure
4544
fig = plt.figure()
46-
ax = p3.Axes3D(fig)
45+
ax = fig.add_subplot(projection="3d")
4746

4847
# Fifty lines of random 3-D lines
49-
data = [Gen_RandLine(25, 3) for index in range(50)]
48+
data = [gen_rand_line(25, 3) for index in range(50)]
5049

5150
# Creating fifty line objects.
5251
# NOTE: Can't pass empty arrays into 3d version of plot()
@@ -65,7 +64,7 @@ def update_lines(num, dataLines, lines):
6564
ax.set_title('3D Test')
6665

6766
# Creating the Animation object
68-
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines),
69-
interval=50, blit=False)
67+
line_ani = animation.FuncAnimation(
68+
fig, update_lines, 25, fargs=(data, lines), interval=50)
7069

7170
plt.show()

tutorials/toolkits/mplot3d.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@
1414
1515
Getting started
1616
---------------
17-
An Axes3D object is created just like any other axes using
18-
the projection='3d' keyword.
19-
Create a new :class:`matplotlib.figure.Figure` and
20-
add a new axes to it of type :class:`~mpl_toolkits.mplot3d.Axes3D`::
17+
3D Axes (of class `Axes3D`) are created by passing the ``projection="3d"``
18+
keyword argument to `Figure.add_subplot`::
2119
2220
import matplotlib.pyplot as plt
23-
from mpl_toolkits.mplot3d import Axes3D
2421
fig = plt.figure()
2522
ax = fig.add_subplot(111, projection='3d')
2623
27-
.. versionadded:: 1.0.0
28-
This approach is the preferred method of creating a 3D axes.
24+
.. versionchanged:: 1.0.0
25+
Prior to Matplotlib 1.0.0, `Axes3D` needed to be directly instantiated with
26+
``from mpl_toolkits.mplot3d import Axes3D; ax = Axes3D(fig)``.
2927
30-
.. note::
31-
Prior to version 1.0.0, the method of creating a 3D axes was
32-
different. For those using older versions of matplotlib, change
33-
``ax = fig.add_subplot(111, projection='3d')``
34-
to ``ax = Axes3D(fig)``.
28+
.. versionchanged:: 3.2.0
29+
Prior to Matplotlib 3.2.0, it was necessary to explicitly import the
30+
:mod:`mpl_toolkits.mplot3d` module to make the '3d' projection to
31+
`Figure.add_subplot`.
3532
3633
See the :ref:`toolkit_mplot3d-faq` for more information about the mplot3d
3734
toolkit.

0 commit comments

Comments
 (0)