[plotting] gradient plot added

This commit is contained in:
mzwiessele 2015-10-05 18:47:54 +01:00
parent 5869ece323
commit 2b02082015
12 changed files with 193 additions and 125 deletions

View file

@ -129,13 +129,7 @@ class BufferedAxisChangedController(AxisChangedController):
return numpy.hstack((x.flatten()[:, None], y.flatten()[:, None]))
def recompute_X(self, buffered=True):
X = self.plot_function(self.get_grid(buffered))
if isinstance(X, (tuple, list)):
for x in X:
x.shape = [self.resolution, self.resolution]
x[:, :] = x.T[::-1, :]
return X
return X.reshape(self.resolution, self.resolution).T[::-1, :]
return self.plot_function(self.get_grid(buffered))
def _compute_buffered(self, mi, ma):
buffersize = self._buffersize()

View file

@ -33,7 +33,7 @@ class ImshowController(BufferedAxisChangedController):
view.set_extent((xmin, xmax, ymin, ymax))
class ImAnnotateController(ImshowController):
def __init__(self, ax, plot_function, plot_limits, resolution=20, update_lim=.99, **kwargs):
def __init__(self, ax, plot_function, plot_limits, resolution=20, update_lim=.99, imshow_kwargs=None, **kwargs):
"""
:param plot_function:
function to use for creating image for plotting (return ndarray-like)
@ -44,15 +44,16 @@ class ImAnnotateController(ImshowController):
:param text_props: kwargs for pyplot.text(**text_props)
:param kwargs: additional kwargs are for pyplot.imshow(**kwargs)
"""
self.imshow_kwargs = imshow_kwargs or {}
super(ImAnnotateController, self).__init__(ax, plot_function, plot_limits, resolution, update_lim, **kwargs)
def _init_view(self, ax, X, xmin, xmax, ymin, ymax, text_props={}, **kwargs):
view = [super(ImAnnotateController, self)._init_view(ax, X[0], xmin, xmax, ymin, ymax, **kwargs)]
def _init_view(self, ax, X, xmin, xmax, ymin, ymax, **kwargs):
view = [super(ImAnnotateController, self)._init_view(ax, X[0], xmin, xmax, ymin, ymax, **self.imshow_kwargs)]
xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False)
ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False)
for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin[::-1])):
view.append(ax.text(x + xoffset, y + yoffset, "{}".format(X[1][j, i]), ha='center', va='center', **text_props))
view.append(ax.text(x + xoffset, y + yoffset, "{}".format(X[1][j, i]), ha='center', va='center', **kwargs))
return view
def update_view(self, view, X, xmin, xmax, ymin, ymax):

View file

@ -68,6 +68,8 @@ data_y_1d_plot = dict(color='k', linewidth=1.5)
ard = dict(edgecolor='k', linewidth=1.2)
# Input plots:
latent = dict(aspect='auto', cmap='Greys', interpolation='bilinear')
magnification = dict(aspect='auto', cmap='Greys', interpolation='bilinear')
latent_scatter = dict(s=40, linewidth=.2, edgecolor='k', alpha=.9)
latent = dict(aspect='auto', cmap='Greys', interpolation='bicubic')
gradient = dict(aspect='auto', cmap='RdBu', interpolation='nearest')
magnification = dict(aspect='auto', cmap='Greys', interpolation='bicubic')
latent_scatter = dict(s=40, linewidth=.2, edgecolor='k', alpha=.9)
annotation = dict(fontdict=dict(family='sans-serif', weight='light', fontsize=9), zorder=.3)

View file

@ -33,7 +33,8 @@ from ..abstract_plotting_library import AbstractPlottingLibrary
from .. import Tango
from . import defaults
from matplotlib.colors import LinearSegmentedColormap
from .controllers import ImshowController
from .controllers import ImshowController, ImAnnotateController
import itertools
class MatplotlibPlots(AbstractPlottingLibrary):
def __init__(self):
@ -120,10 +121,26 @@ class MatplotlibPlots(AbstractPlottingLibrary):
def imshow(self, ax, X, extent=None, label=None, plot_function=None, resolution=None, vmin=None, vmax=None, **kwargs):
if plot_function is not None:
self.controller = ImshowController(ax, plot_function, extent, resolution=resolution, vmin=vmin, vmax=vmax, **kwargs)
return self.controller
return ImshowController(ax, plot_function, extent, resolution=resolution, vmin=vmin, vmax=vmax, **kwargs)
return ax.imshow(X, label=label, extent=extent, vmin=vmin, vmax=vmax, **kwargs)
def annotation_heatmap(self, ax, X, annotation, extent, label=None, plot_function=None, resolution=None, imshow_kwargs=None, **annotation_kwargs):
if plot_function is not None:
return ImAnnotateController(ax, plot_function, extent, resolution=resolution, imshow_kwargs=imshow_kwargs or {}, **annotation_kwargs)
if ('ha' not in annotation_kwargs) and ('horizontalalignment' not in annotation_kwargs):
annotation_kwargs['ha'] = 'center'
if ('va' not in annotation_kwargs) and ('verticalalignment' not in annotation_kwargs):
annotation_kwargs['va'] = 'center'
xmin, xmax, ymin, ymax = extent
self.imshow(X, extent, label, None, resolution, **imshow_kwargs or {})
xoffset, yoffset = (xmax - xmin) / (2 * self.resolution), (ymax - ymin) / (2 * self.resolution)
xlin = np.linspace(xmin, xmax, self.resolution, endpoint=False)
ylin = np.linspace(ymin, ymax, self.resolution, endpoint=False)
annotations = []
for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin[::-1])):
annotations.append(ax.text(x + xoffset, y + yoffset, "{}".format(annotation[j, i]), **annotation_kwargs))
return annotations
def contour(self, ax, X, Y, C, levels=20, label=None, **kwargs):
return ax.contour(X, Y, C, levels=np.linspace(C.min(), C.max(), levels), label=label, **kwargs)