def __init__(self, axes, model, interpolation="nearest", aspect="auto"):
super(SliceView, self).__init__()
data = np.zeros((1, 1))
self._image = axes.imshow(data, interpolation=interpolation, aspect=aspect, origin='upper')
""" :type: AxesImage """
self._model = model
""" :type: SliceModel """
style = {"fill": False,
"alpha": 1,
"color": 'black',
"linestyle": 'dotted',
"linewidth": 0.75
}
self._vertical_indicator = patches.Rectangle((-0.5, -0.5), 1, model.height, **style)
self._horizontal_indicator = patches.Rectangle((-0.5, -0.5), model.width, 1, **style)
self._zoom_factor = 1.0
self._view_limits = None
self._min_xlim = 0
self._max_xlim = model.width
self._min_ylim = 0
self._max_ylim = model.height
python类AxesImage()的实例源码
def update(self, data):
"""
:param data: A (left, bottom, right, top) bounding box.
"""
if self._image_handle is None:
self._image_handle = next(c for c in plt.gca().get_children() if isinstance(c, AxesImage))
data_shape = self._image_handle.get_array().shape # Hopefully this isn't copying
if data_shape != self._last_data_shape:
extent =(-.5, data_shape[1]-.5, data_shape[0]-.5, -.5)
self._image_handle.set_extent(extent)
plt.gca().set_xlim(extent[:2])
plt.gca().set_ylim(extent[2:])
self._last_data_shape = data_shape
l, b, r, t = data
# x = np.array([l+.5, l+.5, r+.5, r+.5, l+.5]) # Note: should we be adding .5? The extend already subtracts .5
# y = np.array([t+.5, b+.5, b+.5, t+.5, t+.5])
x = np.array([l, l, r, r, l]) # Note: should we be adding .5? The extend already subtracts .5
y = np.array([t, b, b, t, t])
LinePlot.update(self, (x, y))
def _(artist, event):
if type(artist) != AxesImage:
# Skip and warn on subclasses (`NonUniformImage`, `PcolorImage`) as
# they do not implement `contains` correctly. Even if they did, they
# would not support moving as we do not know where a given index maps
# back physically.
return compute_pick.dispatch(object)(artist, event)
contains, _ = artist.contains(event)
if not contains:
return
ns = np.asarray(artist.get_array().shape)[::-1] # (y, x) -> (x, y)
xy = np.array([event.xdata, event.ydata])
xmin, xmax, ymin, ymax = artist.get_extent()
# Handling of "upper" origin copied from AxesImage.get_cursor_data.
if artist.origin == "upper":
ymin, ymax = ymax, ymin
low, high = np.array([[xmin, ymin], [xmax, ymax]])
idxs = ((xy - low) / (high - low) * ns).astype(int)[::-1]
target = with_attrs(xy, index=tuple(idxs))
return Selection(artist, target, 0, None, None)
def setCanvas(self, **kwargs):
"""Set canvas to plot in
:param figure: Matplotlib figure to plot in
:type figure: :py:class:`matplotlib.Figure`
:param axes: Matplotlib axes to plot in
:type axes: :py:class:`matplotlib.Axes`
:raises: TypeError
"""
axes = kwargs.get('axes', None)
figure = kwargs.get('figure', None)
if isinstance(axes, plt.Axes):
self.fig, self.ax = axes.get_figure(), axes
self._show_plt = False
elif isinstance(figure, plt.Figure):
self.fig, self.ax = figure, figure.gca()
self._show_plt = False
elif axes is None and figure is None and self.fig is None:
self.fig, self.ax = plt.subplots(1, 1)
self._show_plt = True
else:
raise TypeError('axes has to be of type matplotlib.Axes. '
'figure has to be of type matplotlib.Figure')
self.image = AxesImage(self.ax)
self.ax.add_artist(self.image)
def data(self):
""" Data passed to matplotlib.image.AxesImage """
return self._data
def plot(self, component='displacement', **kwargs):
"""Plots any component fom Scene
The following components are recognizes
- 'cartesian.dE'
- 'cartesian.dN'
- 'cartesian.dU'
- 'displacement'
- 'phi'
- 'theta'
:param **kwargs: Keyword args forwarded to `matplotlib.plt.imshow()`
:type **kwargs: {dict}
:param component: Component to plot
['cartesian.dE', 'cartesian.dN', 'cartesian.dU',
'displacement', 'phi', 'theta']
:type component: {string}, optional
:param axes: Axes instance to plot in, defaults to None
:type axes: :py:class:`matplotlib.Axes`, optional
:param figure: Figure instance to plot in, defaults to None
:type figure: :py:class:`matplotlib.Figure`, optional
:param **kwargs: kwargs are passed into `plt.imshow`
:type **kwargs: dict
:returns: Imshow instance
:rtype: :py:class:`matplotlib.image.AxesImage`
:raises: AttributeError
"""
self._initImagePlot(**kwargs)
self.component = component
self.title = self.components_available[component]
if self._show_plt:
plt.show()