add tag-based plotting to mpl engine
@@ -3,6 +3,8 @@ from .engine import PlottingEngine, PlottingFigure, PlottingLayout, filterWithSe
import matplotlib.pyplot as plt
from matplotlib import gridspec
from tempfile import mkstemp
+import itertools, numpy as np
+from functools import reduce
class MatplotlibFigure(PlottingFigure):
def __init__(self, title=None, layout=PlottingLayout, use_legend=True, figsize=(9,5), save_to_pdf=False):
@@ -11,6 +13,26 @@ class MatplotlibFigure(PlottingFigure):
self.figsize = figsize
self.save_to_pdf = save_to_pdf
+ def getMergedTaggedDatasets(self):
+ for datasets_for_tag in self.tagged_data.values():
+ x = reduce(lambda u,v: np.concatenate((u,[np.nan],v)), (dataset['x'] for dataset in datasets_for_tag))
+ y = reduce(lambda u,v: np.concatenate((u,[np.nan],v)), (dataset['y'] for dataset in datasets_for_tag))
+ # merge all datasets
+ result_dataset = datasets_for_tag[0] if datasets_for_tag else None
+ for dataset in datasets_for_tag:
+ result_dataset.update(dataset)
+ # use the concatenated values for x and y
+ result_dataset['x'] = x
+ result_dataset['y'] = y
+ if result_dataset is not None:
+ yield result_dataset
+
+ def getDatasets(self):
+ """ Get an iterable of all datasets."""
+ return itertools.chain(
+ self.getMergedTaggedDatasets(),
+ (dataset for dataset in self.xy_datasets if not 'tag' in dataset))
+
def plot(self):
""" Plot the figure. Call this last."""
fig = plt.figure(num=None, figsize=self.figsize, dpi=80, facecolor='w', edgecolor='k')
@@ -18,7 +40,7 @@ class MatplotlibFigure(PlottingFigure):
plt.subplot(__gs[0])
have_labels = False
- for dataset in self.xy_datasets:
+ for dataset in self.getDatasets():
kwargs = {}
if 'name' in dataset:
kwargs['label'] = dataset['name']
