def test_lineanimator_figure(): np.random.seed(1) data_shape0 = (10, 20) data0 = np.random.rand(*data_shape0) plot_axis0 = 1 slider_axis0 = 0 xdata = np.tile(np.linspace(0, 100, (data_shape0[plot_axis0] + 1)), (data_shape0[slider_axis0], 1)) fig = plt.figure() ani = LineAnimator(data0, plot_axis_index=plot_axis0, axis_ranges=[None, xdata], fig=fig) ani.plot_start_image(ax=fig.gca())
def test_lineanimator_init(plot_axis_index, axis_ranges, xlabel, xlim): data = np.random.random((5, 5, 10)) LineAnimator(data=data, plot_axis_index=plot_axis_index, axis_ranges=axis_ranges, xlabel=xlabel, xlim=xlim)
def test_lineanimator_init_nans(): data = np.random.random((5, 5, 10)) data[0][0][:] = np.nan line_anim = LineAnimator(data=data, plot_axis_index=-1, axis_ranges=[None, None, XDATA], xlabel='x-axis', xlim=None, ylim=None) assert line_anim.ylim[0] is not None assert line_anim.ylim[1] is not None assert line_anim.xlim[0] is not None assert line_anim.xlim[1] is not None
def test_lineanimator_figure(): np.random.seed(1) data_shape0 = (10, 20) data0 = np.random.rand(*data_shape0) plot_axis0 = 1 slider_axis0 = 0 xdata = np.tile(np.linspace( 0, 100, (data_shape0[plot_axis0] + 1)), (data_shape0[slider_axis0], 1)) ani = LineAnimator(data0, plot_axis_index=plot_axis0, axis_ranges=[None, xdata]) return ani.fig
def _animate_cube_1D(self, plot_axis_index=-1, axes_coordinates=None, axes_units=None, data_unit=None, **kwargs): """Animates an axis of a cube as a line plot with sliders for other axes.""" if axes_coordinates is None: axes_coordinates = [None] * self.data.ndim if axes_units is None: axes_units = [None] * self.data.ndim # Get real world axis values along axis to be plotted and enter into axes_ranges kwarg. if axes_coordinates[plot_axis_index] is None: xname = self.world_axis_physical_types[plot_axis_index] xdata = self.axis_world_coords(plot_axis_index) elif isinstance(axes_coordinates[plot_axis_index], str): xname = axes_coordinates[plot_axis_index] xdata = self.extra_coords[xname]["value"] else: xname = "" xdata = axes_coordinates[plot_axis_index] # Change x data to desired units it set by user. if isinstance(xdata, u.Quantity): if axes_units[plot_axis_index] is None: unit_x_axis = xdata.unit else: unit_x_axis = axes_units[plot_axis_index] xdata = xdata.to(unit_x_axis).value else: if axes_units[plot_axis_index] is not None: raise TypeError(INVALID_UNIT_SET_MESSAGE) else: unit_x_axis = None # Put xdata back into axes_coordinates as a masked array. axes_coordinates[plot_axis_index] = xdata # Set default x label default_xlabel = "{0} [{1}]".format(xname, unit_x_axis) # Derive y axis data if data_unit is None: data = self.data data_unit = self.unit else: if self.unit is None: raise TypeError("NDCube.unit is None. Must be an astropy.units.unit or " "valid unit string in order to set data_unit.") else: data = (self.data * self.unit).to(data_unit).value # Combine data with mask #data = np.ma.masked_array(data, self.mask) # Set default y label default_ylabel = "Data [{0}]".format(unit_x_axis) # Initiate line animator object. ax = LineAnimator(data, plot_axis_index=plot_axis_index, axis_ranges=axes_coordinates, xlabel=default_xlabel, ylabel="Data [{0}]".format(data_unit), **kwargs) return ax
from sunpy.visualization.animator import LineAnimator ############################################################################### # Animate a 2D cube of random data as a line plot along an # axis where the x-axis drifts with time. # Define some random data data_shape0 = (10, 20) data0 = np.random.rand(*data_shape0) ############################################################################### # Define the axis that will make up the line plot plot_axis0 = 1 slider_axis0 = 0 ############################################################################### # Define value along x axis which drift with time. To do this, define # xdata to be the same shape as the data where each row/column # (depending on axis to be animated) represents the x-axis values for # a single frame of the animations. xdata = np.tile(np.linspace(0, 100, data_shape0[plot_axis0]), (data_shape0[slider_axis0], 1)) ############################################################################### # Generate animation object with variable x-axis data. ani = LineAnimator(data0, plot_axis_index=plot_axis0, axis_ranges=[None, xdata]) ############################################################################### # Show plot plt.show()
def _animate_cube_1D(self, plot_axis_index=-1, axes_coordinates=None, axes_units=None, data_unit=None, **kwargs): """ Animates an axis of a cube as a line plot with sliders for other axes. """ if axes_coordinates is None: axes_coordinates = [None] * self.data.ndim if axes_units is None: axes_units = [None] * self.data.ndim # Get real world axis values along axis to be plotted and enter into axes_ranges kwarg. if axes_coordinates[plot_axis_index] is None: xname = self.world_axis_physical_types[plot_axis_index] xdata = self.axis_world_coords(plot_axis_index, edges=True) elif isinstance(axes_coordinates[plot_axis_index], str): xname = axes_coordinates[plot_axis_index] xdata = _get_extra_coord_edges(self.extra_coords[xname]["value"]) else: xname = "" xdata = axes_coordinates[plot_axis_index] # Change x data to desired units it set by user. if isinstance(xdata, u.Quantity): if axes_units[plot_axis_index] is None: unit_x_axis = xdata.unit else: unit_x_axis = axes_units[plot_axis_index] xdata = xdata.to(unit_x_axis).value else: if axes_units[plot_axis_index] is not None: raise TypeError(INVALID_UNIT_SET_MESSAGE) else: unit_x_axis = None # Put xdata back into axes_coordinates as a masked array. if len(xdata.shape) > 1: # Since LineAnimator currently only accepts 1-D arrays for the x-axis, collapse xdata # to single dimension by taking mean along non-plotting axes. index = utils.wcs.get_dependent_data_axes(self.wcs, plot_axis_index, self.missing_axes) reduce_axis = np.where(index == np.array(plot_axis_index))[0] index = np.delete(index, reduce_axis) # Reduce the data by taking mean xdata = np.mean(xdata, axis=tuple(index)) axes_coordinates[plot_axis_index] = xdata # Set default x label default_xlabel = f"{xname} [{unit_x_axis}]" # Derive y axis data if data_unit is None: data = self.data data_unit = self.unit else: if self.unit is None: raise TypeError( "NDCube.unit is None. Must be an astropy.units.unit or " "valid unit string in order to set data_unit.") else: data = (self.data * self.unit).to(data_unit).value # Combine data with mask # data = np.ma.masked_array(data, self.mask) # Set default y label default_ylabel = f"Data [{unit_x_axis}]" # Initiate line animator object. ax = LineAnimator(data, plot_axis_index=plot_axis_index, axis_ranges=axes_coordinates, xlabel=default_xlabel, ylabel=f"Data [{data_unit}]", **kwargs) return ax