Plotting a three-core cable | View | Download | |
An eigenvector animation | View | Download | |
Plotting a torus | View | Download |
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib widget
%matplotlib widget
, you could also try
%matplotlib ipympl
(after using pip
to
install ipympl
) or %matplotlib qt
(after using pip
to install pyqt6
).
With the qt
version, your plot will appear in a
new window, which may be hidden behind the VS Code window
when it is first created.
numpy
array vertices
of
shape $(3,20)$ whose columns are these points.
edge_length
.
edges
of shape
(30, 2)
, whose rows are the pairs
[i,j]
with $i < j$ such that $\|v_i-v_j\|$
is equal to edge_length
. To avoid trouble
from numerical inaccuracies, you should actually check whether
the distance is less than edge_length
plus
some small tolerance. You will probably want to construct
edges
as a list of lists first, and then use
np.array(edges,dtype=int)
to convert it to an
array of integers.
make_dodecahedron()
that returns the pair (vertices, edges)
.
Enter this in the online test system.
ax = plt.figure().add_subplot(projection='3d')
to get an object ax
that can be used for 3D plotting.
Do ax.axis('off')
to turn off the axes. Use
ax.scatter()
to draw all the vertices as blue
blobs. Use ax.plot()
to draw all the edges as
red lines. (You will probably want to run a loop and call
ax.plot()
once for each edge.)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
%matplotlib widget
ipympl
or qt
instead of
widget
.)
phi(t, x)
that calculates the vector
$(\phi_0(t,x),\dotsc,\phi_4(t,x))$. Your code should closely mirror
the above definitions; do not try to collapse everything into a single
formula. Make sure that your code is vectorized, so that it
works correctly when t
and x
are arrays
of the same shape. (It will be even better if your code handles
broadcasting correctly when t
and x
have
different shapes, but that is left as an additional challenge for
enthusiasts.) Enter your definition in the online test system.
ax.plot(...)
, do line = ax.plot(...)[0]
instead. This will give you an object called line
which you can use to update the plot. (You should arrange
the details so that you get a figure object fig
as well as the axis object ax
, and you
should call ax.plot()
instead of
plt.plot()
.)
repaint(i)
. This should calculate
the array of values $\phi_4(t_i,x)$, and then call
line.set_data(x, y)
to update the plot with the
new $y$ values. The repaint()
function should
always return a tuple of objects that have been updated, so
in this case it should return the one-element tuple
(line,)
.
anim0 = FuncAnimation(fig, repaint,
frames=len(t),
interval=20,
blit=True)
ax.set_ylim()
to set the limits
manually. You should label the four plots, using LaTeX to write the labels.
(Remember that when a string contains backslashes, it is best to enter it with
the prefix r
, as in r'$\sin(\theta)$'
.) In the
initial setup you will need to create four different line objects, which
you could assemble into a list called lines
. The
repaint()
function should then update each of these objects and
return tuple(lines)
. The result of the call to
FuncAnimation()
should be assigned to a new variable, not the
same as the one used for the first animation.
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib widget
ax.set_xlim()
, ax.set_ylim()
and
ax.set_zlim()
(or do something equivalent), because
matplotlib will not always choose the limits correctly.