summaryrefslogtreecommitdiff
path: root/silx/gui/plot/backends/BackendMatplotlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'silx/gui/plot/backends/BackendMatplotlib.py')
-rw-r--r--silx/gui/plot/backends/BackendMatplotlib.py52
1 files changed, 49 insertions, 3 deletions
diff --git a/silx/gui/plot/backends/BackendMatplotlib.py b/silx/gui/plot/backends/BackendMatplotlib.py
index 726a839..7739329 100644
--- a/silx/gui/plot/backends/BackendMatplotlib.py
+++ b/silx/gui/plot/backends/BackendMatplotlib.py
@@ -54,7 +54,8 @@ from matplotlib.backend_bases import MouseEvent
from matplotlib.lines import Line2D
from matplotlib.collections import PathCollection, LineCollection
from matplotlib.ticker import Formatter, ScalarFormatter, Locator
-
+from matplotlib.tri import Triangulation
+from matplotlib.collections import TriMesh
from . import BackendBase
from .._utils import FLOAT32_MINPOS
@@ -359,9 +360,12 @@ class BackendMatplotlib(BackendBase.BackendBase):
else:
errorbarColor = color
- # On Debian 7 at least, Nx1 array yerr does not seems supported
+ # Nx1 error array deprecated in matplotlib >=3.1 (removed in 3.3)
+ if (isinstance(xerror, numpy.ndarray) and xerror.ndim == 2 and
+ xerror.shape[1] == 1):
+ xerror = numpy.ravel(xerror)
if (isinstance(yerror, numpy.ndarray) and yerror.ndim == 2 and
- yerror.shape[1] == 1 and len(x) != 1):
+ yerror.shape[1] == 1):
yerror = numpy.ravel(yerror)
errorbars = axes.errorbar(x, y, label=legend,
@@ -477,6 +481,32 @@ class BackendMatplotlib(BackendBase.BackendBase):
self.ax.add_artist(image)
return image
+ def addTriangles(self, x, y, triangles, legend,
+ color, z, selectable, alpha):
+ for parameter in (x, y, triangles, legend, color,
+ z, selectable, alpha):
+ assert parameter is not None
+
+ # 0 enables picking on filled triangle
+ picker = 0 if selectable else None
+
+ color = numpy.array(color, copy=False)
+ assert color.ndim == 2 and len(color) == len(x)
+
+ if color.dtype not in [numpy.float32, numpy.float]:
+ color = color.astype(numpy.float32) / 255.
+
+ collection = TriMesh(
+ Triangulation(x, y, triangles),
+ label=legend,
+ alpha=alpha,
+ picker=picker,
+ zorder=z)
+ collection.set_color(color)
+ self.ax.add_collection(collection)
+
+ return collection
+
def addItem(self, x, y, legend, shape, color, fill, overlay, z,
linestyle, linewidth, linebgcolor):
if (linebgcolor is not None and
@@ -1100,6 +1130,22 @@ class BackendMatplotlibQt(FigureCanvasQTAgg, BackendMatplotlib):
elif label.startswith('__IMAGE__'):
self._picked.append({'kind': 'image', 'legend': label[9:]})
+ elif isinstance(event.artist, TriMesh):
+ # Convert selected triangle to data point indices
+ triangulation = event.artist._triangulation
+ indices = triangulation.get_masked_triangles()[event.ind[0]]
+
+ # Sort picked triangle points by distance to mouse
+ # from furthest to closest to put closest point last
+ # This is to be somewhat consistent with last scatter point
+ # being the top one.
+ dists = ((triangulation.x[indices] - event.mouseevent.xdata) ** 2 +
+ (triangulation.y[indices] - event.mouseevent.ydata) ** 2)
+ indices = indices[numpy.flip(numpy.argsort(dists))]
+
+ self._picked.append({'kind': 'curve', 'legend': label,
+ 'indices': indices})
+
else: # it's a curve, item have no picker for now
if not isinstance(event.artist, (PathCollection, Line2D)):
_logger.info('Unsupported artist, ignored')