Skip to content

New block type for 'surface' plots #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions animatplot/blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from .lineplots import Line, ParametricLine, Scatter
from .vectors import Quiver, vector_comp
from .image_like import Pcolormesh, Imshow
from .surface import Surface
from .update import Nuke, Update
from .title import Title
79 changes: 79 additions & 0 deletions animatplot/blocks/surface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from .base import Block
import matplotlib.pyplot as plt
import numpy as np


class Surface(Block):
"""Animates a surface (wrapping :meth:`mpl_toolkits.mplot3d.axes3d.plot_surface`)

Parameters
----------
X : 1D or 2D np.ndarray, optional
Y : 1D or 2D np.ndarray, optional
C : list of 2D np.ndarray or a 3D np.ndarray
ax : matplotlib.axes.Axes, optional
The matplotlib axes to attach the block to.
Must be created with 'projection="3d"'.
Defaults to matplotlib.pyplot.gca()
t_axis : int, optional
The axis of the array that represents time. Defaults to 0.
No effect if C is a list.
fixed_vscale: bool, default True
By default, set the vertical scale using the overall minimum and maximum of the
array. If set to False, scale is calculated independently for each time slice.

Attributes
----------
ax : matplotlib axis
The matplotlib axes that the block is attached to.

Notes
-----
All other keyword arguments get passed to ``ax.plot_surface``
see :meth:`mpl_toolkits.mplot3d.axes3d.plot_surface` for details.
"""
def __init__(self, *args, ax=None, t_axis=0, fixed_vscale=True, **kwargs):
self.kwargs = kwargs

if len(args) == 1:
self.C = args[0]
x1d = np.arange(self.C[0].shape[0])
y1d = np.arange(self.C[0].shape[1])
self.Y, self.X = np.meshgrid(y1d, x1d)

Check warning on line 42 in animatplot/blocks/surface.py

View check run for this annotation

Codecov / codecov/patch

animatplot/blocks/surface.py#L39-L42

Added lines #L39 - L42 were not covered by tests
elif len(args) == 3:
self.X, self.Y, self.C = args
if len(self.X.shape) not in [1, 2]:
raise TypeError('X must be a 1D or 2D arrays')

Check warning on line 46 in animatplot/blocks/surface.py

View check run for this annotation

Codecov / codecov/patch

animatplot/blocks/surface.py#L46

Added line #L46 was not covered by tests
if len(self.Y.shape) not in [1, 2]:
raise TypeError('Y must be a 1D or 2D arrays')

Check warning on line 48 in animatplot/blocks/surface.py

View check run for this annotation

Codecov / codecov/patch

animatplot/blocks/surface.py#L48

Added line #L48 was not covered by tests
else:
raise TypeError(

Check warning on line 50 in animatplot/blocks/surface.py

View check run for this annotation

Codecov / codecov/patch

animatplot/blocks/surface.py#L50

Added line #L50 was not covered by tests
'Illegal arguments to Surface; see help(ax.plot_surface)')

if self.kwargs.get("color") is None and self.kwargs.get("cmap") is None:
# No user-specified colors for plot. Need to set to a fixed value to avoid
# cycling during the animation
self.kwargs["color"] = "C0"

super().__init__(ax, t_axis)

self._is_list = isinstance(self.C, list)
self.C = np.asanyarray(self.C)

if fixed_vscale:
self.ax.set_zlim([self.C.min(), self.C.max()])

Slice = self._make_slice(0, 3)

self.poly = self.ax.plot_surface(self.X, self.Y, self.C[Slice], **self.kwargs)

def _update(self, i):
Slice = self._make_slice(i, 3)
self.ax.collections.clear()
self.poly = self.ax.plot_surface(self.X, self.Y, self.C[Slice], **self.kwargs)
return self.poly

def __len__(self):
if self._is_list:
return self.C.shape[0]

Check warning on line 78 in animatplot/blocks/surface.py

View check run for this annotation

Codecov / codecov/patch

animatplot/blocks/surface.py#L78

Added line #L78 was not covered by tests
return self.C.shape[self.t_axis]
1 change: 1 addition & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ These blocks are built to animate data.
Pcolormesh
Imshow
Scatter
Surface

Graph Label Blocks
~~~~~~~~~~~~~~~~~~
Expand Down
Binary file added docs/source/gallery/surface.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading