How To Animate A Polar Plot Python
Matplotlib, Python's chief scientific plotting library, provides tools to make many elaborate plots, graphs, and diagrams. Many of these can be animated, just the process isn't always intuitive. The hardest part is learning how to animate a simple line plot (hither's my piece of cake way). Beyond that, the steps to creating most animations tend to be like.
The examples below demonstrate the particular methods needed to animate common types of plot. Hither I focus on the key components needed for updating each frame of the blitheness. The full code for the examples is hither. It includes liberal and arbitrary apply of sines and cosines and so as to produce looping gifs.
Scatter with variable colour, position, and size
Here nosotros are animating eight × 8 = 64 points. X3
and Y3
, the locations of the circles, are each arrays of size (N f, 8, eight) whereN f is the number of frames. Due south
(the sizes of the circles) is the same size as X3
, whereas C
(the colours of the circles) has size (Northward f, viii, viii, 4).
def animate(i): 11, Yi = X3[i, :, :].flatten(), Y3[i, :, :].flatten() scat.set_offsets(np.c_[11, Yi]) scat.set_sizes(S[i, :, :].flatten()) scat.set_facecolors(C[i, :, :].reshape(-1, iv))
-
set_offsets
alters the location of the points. Information technology accepts a 64 × 2 array of (10, y) coordinates. np.c_ is a handy tool to combine two 1D arrays appropriately. -
set_sizes
does what it sounds like. Information technology accepts a 1D array of length 64. -
set_facecolors
also does what it sounds similar. It accepts a 64 × 4 assortment, where each row is (R, Thousand, B, A). The A controls transparency and is optional, then 64 × three would work.
Quiver with variable direction, position, and colour
Here nosotros are animative 30 arrows. Qx
and Qy
are the locations of the base of the arrows for all frames. Their size is (N f, thirty). U
and V
, the arrows' directions, are also this size. C
, the colours of the arrows, is (Due north f, iv), just for each frame we take a subset of size (30, iv).
def breathing(i): s = np.s_[i, :] qax.set_offsets(np.c_[Qx[s].flatten(), Qy[s].flatten()]) qax.set_UVC(U[s], 5[s]) Cidx = np.modern(np.r_[i:i+U.shape[1]], Nframes) qax.set_facecolor(C[Cidx, :])
-
set_offsets
works as it does for the scatter. Since we have thirty arrows, information technology accepts an array of size (30, 2). -
set_UVC
accepts the arrow vectors as 2 1D arrays, rather than combined. -
set_facecolor
accepts an array of size (30, 4), ane RGBA colour for each arrow. Equally above, the A is optional.
The first line within the function simplifies the slicing of arrays in the following lines. The fourth line involving the modulo sectionalization (np.mod
) is only there to assist wheel through the colourmap.
Line plots in a polar projection
Because Matplotlib does all the difficult work backside the scenes with respect to graph projections, animating a polar plot is no different to animative a line plot. Here nosotros update two curves. theta1
and theta2
are 2D arrays of size (N f, 100), where 100 is the number of elements comprising a curve for a given frame.
def animate(i): curve1.set_ydata(theta1[i, :]) curve2.set_ydata(theta2[i, :])
Filled area plots with variable colour
Filled area plots are a piffling more challenging as they are patches, rather than points or lines. Matplotlib's treatment of patches makes sense, simply it isn't intuitive at the get-go which backdrop control what.
fill
is an object created by the fill_between
function. Y1
and Y2
, the upper and lower bounds of the fill up, are each arrays of size (N f,North x), where Due north x is the number of elements making upwardly each curve in a single frame.
def animate(i): path = fill.get_paths()[0] verts = path.vertices verts[1:Nx+ane, 1] = Y1[i, :] verts[Nx+2:-1, i] = Y2[i, :][::-1] fill.set_color(C[i, :])
-
get_paths()[0]
extracts the one and only path. The output ofget_paths()
is a list, even if only one chemical element long. - We rename
path.vertices
for clarity. - The elements
i:Nx+1
contain they coordinates of the upper curve. - The elements
Nx+2:-1
incorporate they coordinates of the lower curve, but correct-to-left, hence the need to opposite the array using[::-1]
. -
set_color
works as it does for examples above.
Careful inspection of the third and quaternary lines of the function indicates that the 0
, Nx+1
, and -1
elements of verts
are not being updated. It's non clear what these elements practice.
3-dimensional contour plots
Contour plots are treated unlike from the other examples. In a mode, they are simpler, considering for each frame, nosotros just delete the current contour object and and then plot a new one. No need to figure out what set_...
method to use. Although any animation could be created this way, the trade-off is oftentimes a noticeable boring downwards in the speed that the blitheness renders and unnecessary repetition in the Python lawmaking.
Here 10
and Y
are 2D arrays of size (20, 30) and Z
, the height of the contour, is a 3D assortment of size (N f, 20, 30).
def breathing(i): ax.collections = [] ax.plot_surface(Ten, Y, Z[i, :, :], **contour_opts) data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" manner="overflow:subconscious;line-height:0" ></bridge>
- Delete the existing profile past elimination the listing of
collections
, which is where Matplotlib stores information about the contour object. - Replot the contour using
plot_surface
with**contour_opts
being a dictionary of keyword arguments.
With judicious use of ax.collections
, it is possible to retain some components of a plot while removing others. Here that's unnecessary since we're plotting only a unmarried matter.
Source: https://brushingupscience.com/2019/08/01/elaborate-matplotlib-animations/
Posted by: colemangingaid.blogspot.com
0 Response to "How To Animate A Polar Plot Python"
Post a Comment