def _get_cmap(cmap): # matplotlib 2.0 deprecated 'spectral' colormap, renamed to nipy_spectral. from matplotlib import __version__ version = tuple(map(int, __version__.split('.'))) if cmap == 'spectral' and version >= (2, ): cmap = 'nipy_spectral' return cmap
class MyToolbar(NavigationToolbar2GTKAgg): #idea borrowed from: http://dalelane.co.uk/blog/?p=778 # but redone a bit differently. # we inhereit the navigation toolbar, but redefine the toolitems # list of toolitems to add to the toolbar, format is: # text, tooltip_text, image_file, callback(str) vers = matplotlib_version.split('.') if int(vers[0]) <= 1 and int(vers[1]) < 2: toolitems = ( #icon names are different in older versions ('Home', 'Reset original view', 'home.png', 'home'), ('Back', 'Back to previous view', 'back.png', 'back'), ('Forward', 'Forward to next view', 'forward.png', 'forward'), ('Pan', 'Pan axes with left mouse, zoom with right', 'move.png', 'pan'), ('Zoom', 'Zoom to rectangle', 'zoom_to_rect.png', 'zoom'), (None, None, None, None), # ('Subplots', 'Configure subplots','subplots', 'configure_subplots'), ('Save', 'Save the figure', 'filesave.png', 'save_figure'), ) else: toolitems = ( #first home used to be home.png etc ('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous view', 'back', 'back'), ('Forward', 'Forward to next view', 'forward', 'forward'), ('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'), ('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'), (None, None, None, None), # ('Subplots', 'Configure subplots','subplots', 'configure_subplots'), ('Save', 'Save the figure', 'filesave', 'save_figure'), )
def hist(self,hold=False,xlabel=None,ylabel='$ \\mathrm{probability\\:density} $', title=None,**kwds): """ Generates a histogram from the simulated data. Parameters ---------- hold: `bool`, optional If this is `False`, pyplot.show() will be called before the method exits. The default is `False` xlabel: `str` or `None`, optional a label for the histogram horizontal axis, the default is `None` ylabel: `str` or `None`, optional a label for the histogram vertical axis, the default is 'probability density' title: `str` or `None` a title for the histogram, the default is `None` kwds: additional key words that will be passed to the `pyplot.hist` method that actually created the histogram Raises ------ `NoSimulatedDataError`: if no simulated data is available from a call to `Distribution.simulate`. """ import matplotlib.pyplot as plt if self.simdata is None: raise NoSimulatedDataError('simulate must be called to generate some data') if not 'bins' in kwds: kwds['bins'] = 100 if not ('normed' in kwds or 'density' in kwds): try: from matplotlib import __version__ as version version = [int(v) for v in version.split('.')[:2]] usedensity = (version[0] > 2 or (version[0] == 2 and version[1] > 0)) except: usedensity = True if usedensity: kwds['density'] = True else: kwds['normed'] = True if not 'histtype' in kwds: kwds['histtype'] ='stepfilled' plt.hist(self.simsorted[np.abs(self.simsorted) != np.inf],**kwds) if ylabel is not None: plt.ylabel(ylabel) if xlabel is not None: plt.xlabel(xlabel) if title is not None: plt.title(title) if not hold: plt.show()
def clicked(self, event): """ Call if an element of this plottype is clicked. Implement in sub class. """ group = event.artist._mt_group indices = event.ind # double click only supported on 1.2 or later major, minor, _ = mpl_version.split('.') if (int(major), int(minor)) < (1, 2) or not event.mouseevent.dblclick: for i in indices: print(self.groups[group][i].line_str) else: # toggle durline first = indices[0] logevent = self.groups[group][first] try: # remove triangle for this event idx = map(itemgetter(0), self.durlines).index(logevent) _, poly = self.durlines[idx] poly.remove() plt.gcf().canvas.draw() del self.durlines[idx] except ValueError: # construct triangle and add to list of durlines if self.args['optime_start']: pts = [[date2num(logevent.datetime), 0], [date2num(logevent.datetime), logevent.duration], [ date2num(logevent.datetime + timedelta( milliseconds=logevent.duration)), 0 ]] else: pts = [[date2num(logevent.datetime), 0], [date2num(logevent.datetime), logevent.duration], [ date2num(logevent.datetime - timedelta( milliseconds=logevent.duration)), 0 ]] poly = Polygon(pts, closed=True, alpha=0.2, linewidth=0, facecolor=event.artist.get_markerfacecolor(), edgecolor=None, zorder=-10000) ax = plt.gca() ax.add_patch(poly) plt.gcf().canvas.draw() self.durlines.append((logevent, poly))
def get_children(self): from matplotlib import __version__ as mversion children = super().get_children() major, minor, patch = [int(_) for _ in mversion.split('.')] if (major, minor) <= (3, 0): children.remove(self.xaxis) children.remove(self.yaxis) children.extend(self._get_axis_list()) return children
def clicked(self, event): """ Call if an element of this plottype is clicked. Implement in sub class. """ group = event.artist._mt_group indices = event.ind # double click only supported on 1.2 or later major, minor, _ = mpl_version.split('.') if (int(major), int(minor)) < (1, 2) or not event.mouseevent.dblclick: for i in indices: print(self.groups[group][i].line_str) else: # toggle durline first = indices[0] logevent = self.groups[group][first] try: # remove triangle for this event idx = map(itemgetter(0), self.durlines).index(logevent) _, poly = self.durlines[idx] poly.remove() plt.gcf().canvas.draw() del self.durlines[idx] except ValueError: # construct triangle and add to list of durlines if self.args['optime_start']: pts = [[date2num(logevent.datetime), 0], [date2num(logevent.datetime), logevent.duration], [date2num(logevent.datetime + timedelta(milliseconds=logevent.duration) ), 0]] else: pts = [[date2num(logevent.datetime), 0], [date2num(logevent.datetime), logevent.duration], [date2num(logevent.datetime - timedelta(milliseconds=logevent.duration) ), 0]] poly = Polygon(pts, closed=True, alpha=0.2, linewidth=0, facecolor=event.artist.get_markerfacecolor(), edgecolor=None, zorder=-10000) ax = plt.gca() ax.add_patch(poly) plt.gcf().canvas.draw() self.durlines.append((logevent, poly))
def check_maplotlib_max_version(ver_maj, ver_min): """ return True if check_maplotlib_version is lower than specified version """ parts = matplotlib_version.split('.') if not parts: return False if parts[0] < str(ver_maj): return True if len(parts) > 1 and parts[1] < str(ver_min): return True return False
def env_check(args): # check mpl_version t = mpl_version.split('.') if int(t[0]) < 2: print("matplotlib >= 2.0.0 needed. Now version " + mpl_version + ".") exit() # check tex environment if args.mode[0] == 'normal': try: subprocess.check_output('tex --version') except subprocess.CalledProcessError: print("Tex not found. Install tex or run in draft mode: `python run.py <json_file> --mode=draft`") exit()
def test_matplotlib_suported_version(self): """matplotlib version is supported """ min_acceptable_version = (1, 1, 0) max_acceptable_version = (1, 3, 1) try: from matplotlib import __version__ as matplotlib_lib_version version = tuple(map(int, matplotlib_lib_version.split('.'))) pass_test = (version >= min_acceptable_version and version <= max_acceptable_version) version_string = str(matplotlib_lib_version) except ImportError: pass_test = False version_string = "Not installed" self.assertTrue(pass_test, "Unsupported matplotlib version. Must be >= %s and <= %s , but running %s." % ('.'.join(map(str, min_acceptable_version)), '.'.join(map(str, max_acceptable_version)), version_string))
def test_matplotlib_suported_version(self): """matplotlib version is supported """ min_acceptable_version = (1, 1, 0) max_acceptable_version = (1, 3, 1) try: from matplotlib import __version__ as matplotlib_lib_version version = tuple(map(int, matplotlib_lib_version.split('.'))) pass_test = (version >= min_acceptable_version and version <= max_acceptable_version) version_string = str(matplotlib_lib_version) except ImportError: pass_test = False version_string = "Not installed" self.assertTrue(pass_test,\ "Unsupported matplotlib version. Must be >= %s and <= %s , but running %s." \ % ('.'.join(map(str,min_acceptable_version)), '.'.join(map(str,max_acceptable_version)), version_string))
def plot_attribute(cur, planners, attribute, typename): """Create a box plot for a particular attribute. It will include data for all planners that have data for this attribute.""" plt.clf() ax = plt.gca() labels = [] measurements = [] nan_counts = [] is_bool = True for planner in planners: cur.execute('SELECT * FROM `%s`' % planner) attributes = [ t[0] for t in cur.description] if attribute in attributes: cur.execute('SELECT `%s` FROM `%s` WHERE `%s` IS NOT NULL' % (attribute, planner, attribute)) measurement = [ t[0] for t in cur.fetchall() ] cur.execute('SELECT count(*) FROM `%s` WHERE `%s` IS NULL' % (planner, attribute)) nan_counts.append(cur.fetchone()[0]) cur.execute('SELECT DISTINCT `%s` FROM `%s`' % (attribute, planner)) is_bool = is_bool and set([t[0] for t in cur.fetchall() if not t[0]==None]).issubset(set([0,1])) measurements.append(measurement) labels.append(planner.replace('planner_geometric_','').replace('planner_control_','')) if is_bool: width = .5 measurements_percentage = [sum(m)*100./len(m) for m in measurements] ind = range(len(measurements)) plt.bar(ind, measurements_percentage, width) xtickNames = plt.xticks([x+width/2. for x in ind], labels, rotation=30) ax.set_ylabel(attribute.replace('_',' ') + ' (%)') else: if int(matplotlibversion.split('.')[0])<1: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5) else: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5, bootstrap=1000) ax.set_ylabel(attribute.replace('_',' ')) xtickNames = plt.setp(ax,xticklabels=labels) plt.setp(xtickNames, rotation=25) ax.set_xlabel('Motion planning algorithm') ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if max(nan_counts)>0: maxy = max([max(y) for y in measurements]) for i in range(len(labels)): x = i+width/2 if is_bool else i+1 ax.text(x, .95*maxy, str(nan_counts[i]), horizontalalignment='center', size='small') plt.show()
def plot_measurements(self, measurements, total_per_planner, planners, attribute, typename, labels, layout): plt.clf() width = 5 height = 4 dpi = 100 fig = Figure(figsize=(width, height), dpi=dpi, facecolor=(1.0, 1.0, 1.0, 1.0)) ax = fig.add_subplot(111) figcanvas = FigureCanvas(fig) figcanvas.setParent(self._widget) FigureCanvas.setSizePolicy(figcanvas, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(figcanvas) if typename == 'BOOLEAN': width = .5 measurementsPercentage = [sum(m) * 100. / total_per_planner[counter] for counter, m in enumerate(measurements)] ind = range(len(measurements)) ax.bar(ind, measurementsPercentage, width) plt.setp(ax, xticks=[x + width / 2. for x in ind]) ax.set_ylim([0, 100]) ax.set_xlim([0, len(planners)]) else: if int(matplotlibversion.split('.')[0]) < 1: ax.boxplot(measurements, notch=0, sym='r+', vert=1, whis=1.5) else: ax.boxplot(measurements, notch=0, sym='r+', vert=1, whis=1.5, bootstrap=1000) xtickNames = plt.setp(ax, xticklabels=labels) plt.setp(xtickNames, rotation=90) for tick in ax.xaxis.get_major_ticks(): # shrink the font size of the x tick labels tick.label.set_fontsize(7) for tick in ax.yaxis.get_major_ticks(): # shrink the font size of the y tick labels tick.label.set_fontsize(7) fig.subplots_adjust(bottom=0.32, top=0.90, left=0.08, right=0.98) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if "clearance" in attribute: ax.ticklabel_format(style='sci', axis='y', scilimits=(-3, 4), useLocale=True) ax.yaxis.offsetText.set_fontsize(7) self.clearLayout(layout) layout.addWidget(figcanvas)
def checkVersions(): """Helper function to determine whether the versions of yt, pyqt and mpl the user has installed are sufficient for a smooth run.""" errorString = "It seems that you do not have the required modules \ installed.\n" ytNums = yt.__version__.split(".") ytCheck = float(ytNums[0]) >= 3 and float(ytNums[1]) >= 5 if not ytCheck: errorString += (f"Your version of the yt-module is {yt.__version__}, " "but you need v. 3.5 or later\n") PyQtNums = PyQtVersion.split(".") PyQtCheck = float(PyQtNums[0]) >= 5 and float(PyQtNums[1]) >= 9 if not PyQtCheck: errorString += (f"Your version of the PyQt-module is {PyQtVersion}, " "but you need v. 5.9 or later\n") mplNums = mplVersion.split(".") mplCheck = float(mplNums[0]) >= 3 if not mplCheck: errorString += (f"Your version of the matplotlib-module is " f"{mplVersion}, but you need v. 3.0 or later\n") if not (ytCheck and PyQtCheck and mplCheck): raise RuntimeError(errorString)
def test_matplotlib_suported_version(self): """maptplotlib version is supported """ # min_acceptable_version = (1,1,0) # min_unacceptable_version = (1,1,0) matplotlib_acceptable_version = (1, 1, 0) try: from matplotlib import __version__ as matplotlib_lib_version version = tuple(map(int, matplotlib_lib_version.split("."))) pass_test = version == matplotlib_acceptable_version version_string = str(matplotlib_lib_version) except ImportError: pass_test = False version_string = "Not installed" self.assertTrue( pass_test, "Unsupported matplotlib version. Must be >= %s and <= %s , but running %s." % ( ".".join(map(str, matplotlib_acceptable_version)), ".".join(map(str, matplotlib_acceptable_version)), version_string, ), )
########################################################### # We check what extensions are available ########################################################### __extensions__ = {'matplotlib': False, 'mplot3d': False, 'pygmo': False, 'scikit-learn': False, 'scipy': False} # 1 - matplotlib try: from matplotlib import __version__ as matplotlib_ver __extensions__['matplotlib'] = True # We detect the version and if more than 1.1.0 mplot3d is there mver = matplotlib_ver.split('.') mver = int(mver[0]) * 100 + int(mver[1]) * 10 + int(mver[2]) if mver >= 110: __extensions__['mplot3d'] = True del mver except ImportError: pass # 2 - PyGMO try: # Here we cannot try to import __version__ as PyGMO also imports PyKEP and would then always fail # to import anything from PyGMO __init__.py from PyGMO.problem._base import base __extensions__['pygmo'] = True except ImportError: pass # 3 - scikit-learn is installed try: from sklearn import __version__ as sklearn_ver __extensions__['scikit-learn'] = True
def plot(self): # check if there is anything to plot if len(self.plot_instances) == 0: raise SystemExit('no data to plot.') self.artists = [] plt.figure(figsize=(12,8), dpi=100, facecolor='w', edgecolor='w') axis = plt.subplot(111) # set xlim from min to max of logfile ranges xlim_min = min([pi.date_range[0] for pi in self.plot_instances]) xlim_max = max([pi.date_range[1] for pi in self.plot_instances]) if xlim_max < xlim_min: raise SystemExit('no data to plot.') xlabel = 'time' ylabel = '' for i, plot_inst in enumerate(sorted(self.plot_instances, key=lambda pi: pi.sort_order)): self.artists.extend(plot_inst.plot(axis, i, len(self.plot_instances), (xlim_min, xlim_max) )) if hasattr(plot_inst, 'xlabel'): xlabel = plot_inst.xlabel if hasattr(plot_inst, 'ylabel'): ylabel = plot_inst.ylabel self.print_shortcuts() axis.set_xlabel(xlabel) axis.set_xticklabels(axis.get_xticks(), rotation=90, fontsize=10) axis.xaxis.set_major_formatter(DateFormatter('%b %d\n%H:%M:%S')) for label in axis.get_xticklabels(): # make the xtick labels pickable label.set_picker(True) axis.set_xlim(date2num([xlim_min, xlim_max])) # ylabel for y axis if self.args['logscale']: ylabel += ' (log scale)' axis.set_ylabel(ylabel) # title and mtools link axis.set_title(self.args['title'] or ', '.join([l.name for l in self.logfiles if l.name != '<stdin>'])) plt.subplots_adjust(bottom=0.15, left=0.1, right=0.95, top=0.95) self.footnote = plt.annotate('created with mtools v%s: https://github.com/rueckstiess/mtools' % __version__, (10, 10), xycoords='figure pixels', va='bottom', fontsize=8) handles, labels = axis.get_legend_handles_labels() if len(labels) > 0: # only change fontsize if supported major, minor, _ = mpl_version.split('.') if (int(major), int(minor)) >= (1, 3): self.legend = axis.legend(loc='upper left', frameon=False, numpoints=1, fontsize=9) else: self.legend = axis.legend(loc='upper left', frameon=False, numpoints=1) if self.args['type'] == 'scatter': # enable legend picking for scatter plots for i, legend_line in enumerate(self.legend.get_lines()): legend_line.set_picker(10) legend_line._mt_legend_item = i plt.gcf().canvas.mpl_connect('pick_event', self.onpick) plt.gcf().canvas.mpl_connect('key_press_event', self.onpress) plt.show()
def plotAttribute(cur, planners, attribute, typename): """Create a plot for a particular attribute. It will include data for all planners that have data for this attribute.""" labels = [] measurements = [] nanCounts = [] if typename == 'ENUM': cur.execute('SELECT description FROM enums where name IS "%s"' % attribute) descriptions = [ t[0] for t in cur.fetchall() ] numValues = len(descriptions) for planner in planners: cur.execute('SELECT %s FROM runs WHERE plannerid = %s AND %s IS NOT NULL' \ % (attribute, planner[0], attribute)) measurement = [ t[0] for t in cur.fetchall() if t[0] != None ] if len(measurement) > 0: cur.execute('SELECT count(*) FROM runs WHERE plannerid = %s AND %s IS NULL' \ % (planner[0], attribute)) nanCounts.append(cur.fetchone()[0]) labels.append(planner[1]) if typename == 'ENUM': scale = 100. / len(measurement) measurements.append([measurement.count(i)*scale for i in range(numValues)]) else: measurements.append(measurement) if len(measurements)==0: print('Skipping "%s": no available measurements' % attribute) return plt.clf() ax = plt.gca() if typename == 'ENUM': width = .5 measurements = np.transpose(np.vstack(measurements)) colsum = np.sum(measurements, axis=1) rows = np.where(colsum != 0)[0] heights = np.zeros((1,measurements.shape[1])) ind = range(measurements.shape[1]) legend_labels = [] for i in rows: plt.bar(ind, measurements[i], width, bottom=heights[0], color=matplotlib.cm.hot(int(floor(i*256/numValues))), label=descriptions[i]) heights = heights + measurements[i] xtickNames = plt.xticks([x+width/2. for x in ind], labels, rotation=30) ax.set_ylabel(attribute.replace('_',' ') + ' (%)') box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) props = matplotlib.font_manager.FontProperties() props.set_size('small') ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop = props) elif typename == 'BOOLEAN': width = .5 measurementsPercentage = [sum(m) * 100. / len(m) for m in measurements] ind = range(len(measurements)) plt.bar(ind, measurementsPercentage, width) xtickNames = plt.xticks([x + width / 2. for x in ind], labels, rotation=30) ax.set_ylabel(attribute.replace('_',' ') + ' (%)') else: if int(matplotlibversion.split('.')[0])<1: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5) else: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5, bootstrap=1000) ax.set_ylabel(attribute.replace('_',' ')) xtickNames = plt.setp(ax,xticklabels=labels) plt.setp(xtickNames, rotation=25) ax.set_xlabel('Motion planning algorithm') ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if max(nanCounts)>0: maxy = max([max(y) for y in measurements]) for i in range(len(labels)): x = i+width/2 if typename=='BOOLEAN' else i+1 ax.text(x, .95*maxy, str(nanCounts[i]), horizontalalignment='center', size='small') plt.show()
def plot(self): # check if there is anything to plot if len(self.plot_instances) == 0: raise SystemExit('no data to plot.') if self.args['output_file'] is not None: # --output-file means don't depend on X, # so switch to a pure-image backend before doing any plotting. plt.switch_backend('agg') self.artists = [] plt.figure(figsize=(12, 8), dpi=100, facecolor='w', edgecolor='w') axis = plt.subplot(111) # set xlim from min to max of logfile ranges xlim_min = min([pi.date_range[0] for pi in self.plot_instances]) xlim_max = max([pi.date_range[1] for pi in self.plot_instances]) if xlim_max < xlim_min: raise SystemExit('no data to plot.') xlabel = 'time' ylabel = '' # use timezone of first log file (may not always be what user wants # but must make a choice) tz = self.logfiles[0].timezone # tzformat='%b %d\n%H:%M:%S' if tz == tzutc() else '%b %d\n%H:%M:%S%z' locator = AutoDateLocator(tz=tz, minticks=5, maxticks=10) formatter = AutoDateFormatter(locator, tz=tz) formatter.scaled = { 365.0: '%Y', 30.: '%b %Y', 1.0: '%b %d %Y', 1. / 24.: '%b %d %Y\n%H:%M:%S', 1. / (24. * 60.): '%b %d %Y\n%H:%M:%S', } # add timezone to format if not UTC if tz != tzutc(): formatter.scaled[1. / 24.] = '%b %d %Y\n%H:%M:%S%z' formatter.scaled[1. / (24. * 60.)] = '%b %d %Y\n%H:%M:%S%z' for i, plot_inst in enumerate( sorted(self.plot_instances, key=lambda pi: pi.sort_order)): self.artists.extend( plot_inst.plot(axis, i, len(self.plot_instances), (xlim_min, xlim_max))) if hasattr(plot_inst, 'xlabel'): xlabel = plot_inst.xlabel if hasattr(plot_inst, 'ylabel'): ylabel = plot_inst.ylabel if self.args['output_file'] is None: self.print_shortcuts(scatter=isinstance(self.plot_instance, plottypes.ScatterPlotType)) axis.set_xlabel(xlabel) axis.set_xticklabels(axis.get_xticks(), rotation=90, fontsize=9) axis.xaxis.set_major_locator(locator) axis.xaxis.set_major_formatter(formatter) axis.set_xlim(date2num([xlim_min, xlim_max])) # ylabel for y axis if self.args['logscale']: ylabel += ' (log scale)' axis.set_ylabel(ylabel) # enable grid axis.grid(True) # title and mtools link axis.set_title(self.args['title'] or ', '.join( [l.name for l in self.logfiles if l.name != '<stdin>'])) plt.subplots_adjust(bottom=0.15, left=0.1, right=0.95, top=0.95) self.footnote = plt.annotate("created with mtools v%s: " "https://github.com/rueckstiess/mtools" % __version__, (10, 10), xycoords='figure pixels', va='bottom', fontsize=8) handles, labels = axis.get_legend_handles_labels() if len(labels) > 0: # only change fontsize if supported major, minor, _ = mpl_version.split('.') if (int(major), int(minor)) >= (1, 3): self.legend = axis.legend(loc='upper left', frameon=False, numpoints=1, fontsize=9) else: self.legend = axis.legend(loc='upper left', frameon=False, numpoints=1) if self.args['type'] == 'scatter': # enable legend picking for scatter plots for i, legend_line in enumerate(self.legend.get_lines()): legend_line.set_picker(10) legend_line._mt_legend_item = i # overwrite y-axis limits if set if self.args['ylimits'] is not None: print(self.args['ylimits']) axis.set_ylim(self.args['ylimits']) plt.gcf().canvas.mpl_connect('pick_event', self.onpick) plt.gcf().canvas.mpl_connect('key_press_event', self.onpress) if self.args['output_file'] is not None: plt.savefig(self.args['output_file']) else: plt.show()
from contextlib import contextmanager from enum import Enum # 3rd party imports from matplotlib.legend import Legend from matplotlib import cm, __version__ as mpl_version_str from matplotlib.container import ErrorbarContainer # ----------------------------------------------------------------------------- # Constants # ----------------------------------------------------------------------------- # matplotlib version information MPLVersionInfo = collections.namedtuple("MPLVersionInfo", ("major", "minor", "patch")) MATPLOTLIB_VERSION_INFO = MPLVersionInfo._make( map(int, mpl_version_str.split("."))) # Use the correct draggable method based on the matplotlib version if hasattr(Legend, "set_draggable"): SET_DRAGGABLE_METHOD = "set_draggable" else: SET_DRAGGABLE_METHOD = "draggable" # Any changes here must be reflected in the definition in # the C++ MplCpp/Plot.h header. See the comment in that file # for the reason for duplication. class MantidAxType(Enum): """Define an list of possible axis types for plotting""" BIN = 0
def plot_attribute(cur, planners, attribute, typename): """Create a plot for a particular attribute. It will include data for all planners that have data for this attribute.""" plt.clf() ax = plt.gca() labels = [] measurements = [] nan_counts = [] cur.execute('SELECT count(*) FROM enums where name IS "%s"' % attribute) num_vals = cur.fetchone()[0] is_enum = False if num_vals==0 else True is_bool = not is_enum for planner in planners: cur.execute('SELECT * FROM `%s`' % planner) attributes = [ t[0] for t in cur.description] if attribute in attributes: cur.execute('SELECT `%s` FROM `%s` WHERE `%s` IS NOT NULL' % (attribute, planner, attribute)) measurement = [ 0 if np.isinf(t[0]) else t[0] for t in cur.fetchall() ] cur.execute('SELECT count(*) FROM `%s` WHERE `%s` IS NULL' % (planner, attribute)) nan_counts.append(cur.fetchone()[0]) cur.execute('SELECT DISTINCT `%s` FROM `%s`' % (attribute, planner)) is_bool = is_bool and set([t[0] for t in cur.fetchall() if not t[0]==None]).issubset(set([0,1])) if is_enum: scale = 100. / len(measurement) measurements.append([measurement.count(i)*scale for i in range(num_vals)]) else: measurements.append(measurement) labels.append(planner.replace('planner_geometric_','').replace('planner_control_','')) if is_enum: width = .5 measurements = np.transpose(np.vstack(measurements)) colsum = np.sum(measurements, axis=1) rows = np.where(colsum != 0)[0] heights = np.zeros((1,measurements.shape[1])) ind = range(measurements.shape[1]) legend_labels = [] for i in rows: cur.execute('SELECT `description` FROM enums WHERE name IS "%s" AND value IS "%d"' % (attribute,i)) plt.bar(ind, measurements[i], width, bottom=heights[0], color=matplotlib.cm.hot(int(floor(i*256/num_vals))), label=cur.fetchone()[0]) heights = heights + measurements[i] xtickNames = plt.xticks([x+width/2. for x in ind], labels, rotation=30) ax.set_ylabel(attribute.replace('_',' ') + ' (%)') box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) props = matplotlib.font_manager.FontProperties() props.set_size('small') ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop = props) elif is_bool: width = .5 measurements_percentage = [sum(m)*100./len(m) for m in measurements] ind = range(len(measurements)) plt.bar(ind, measurements_percentage, width) xtickNames = plt.xticks([x+width/2. for x in ind], labels, rotation=30) ax.set_ylabel(attribute.replace('_',' ') + ' (%)') else: if int(matplotlibversion.split('.')[0])<1: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5) else: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5, bootstrap=1000) ax.set_ylabel(attribute.replace('_',' ')) xtickNames = plt.setp(ax,xticklabels=labels) plt.setp(xtickNames, rotation=25) ax.set_xlabel('Motion planning algorithm') ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if max(nan_counts)>0: maxy = max([max(y) for y in measurements]) for i in range(len(labels)): x = i+width/2 if is_bool else i+1 ax.text(x, .95*maxy, str(nan_counts[i]), horizontalalignment='center', size='small') plt.show()
def setmask(self, im): """ input an image (for now an HDU) and set self.mask to an array the size of the image with the phot region =1 and expanded background annulus =2 for now we also create a mask the size of the image, so I recommend to extract a subimage and call this method with that input this method well trim the polyon to fit in the image """ imshape = im.shape mask = pl.zeros(imshape) if self.type == "circle": x, y, r = self.imcoords(im) x0 = int(x) y0 = int(y) dx = x - x0 dy = y - y0 # grr pixel centers again - is this right? # dx=dx-0.5; dy=dy-0.5 bg0_r = self.imcoords(im, reg="bg0")[2] #-0.2 # fudge bg1_r = self.imcoords(im, reg="bg1")[2] #+0.2 # fudge bg1_r0 = int(pl.ceil(bg1_r)) r2 = r**2 bg0_r2 = bg0_r**2 bg1_r2 = bg1_r**2 for i in pl.array(range(2 * bg1_r0 + 1)) - bg1_r0: for j in pl.array(range(2 * bg1_r0 + 1)) - bg1_r0: if y0 + j >= 0 and x0 + i >= 0 and y0 + j < ( imshape[0] - 1) and x0 + i < (imshape[1] - 1): d2 = (1. * i - dx)**2 + (1. * j - dy)**2 # d2 = (i-x)**2 + (j-y)**2 -> (i-x0-(x-x0))**2 + ... if d2 <= r2: mask[y0 + j, x0 + i] = 1 # remember indices inverted if d2 >= bg0_r2 and d2 <= bg1_r2: mask[y0 + j, x0 + i] = 2 # remember indices inverted # if x0+i==6: # print i,j,x0+i,y0+j,dx,dy,d2,bg0_r2,bg1_r2 elif self.type == "polygon": # turn annulus back into mask, will trim at edges of image from matplotlib.path import Path from matplotlib import __version__ as mpver v = mpver.split('.') if v[0] < 1: raise Exception( "need matplotlib >=1.3.1, or tell remy to add fallback nxutils option for Path.contains_points" ) elif v[1] < 3: raise Exception( "need matplotlib >=1.3.1, or tell remy to add fallback nxutils option for Path.contains_points" ) elif v[2] < 1: raise Exception( "need matplotlib >=1.3.1, or tell remy to add fallback nxutils option for Path.contains_points" ) # Create vertex coordinates for each grid cell x, y = pl.meshgrid(pl.arange(imshape[1]), pl.arange(imshape[0])) x, y = x.flatten(), y.flatten() points = pl.vstack((x, y)).T mask1 = Path(self.imcoords(im, reg="bg1")).contains_points(points) mask1 = mask1.reshape((imshape[0], imshape[1])) mask0 = Path(self.imcoords(im, reg="bg0")).contains_points(points) #,radius=1) mask0 = mask0.reshape((imshape[0], imshape[1])) mask = Path(self.imcoords(im, reg="ap")).contains_points(points) mask = mask.reshape((imshape[0], imshape[1])) mask = mask + (1 * mask1 - 1 * mask0) * 2 else: raise Exception("unknown region type %s" % self.type) self.mask = mask return mask
def plotAttribute(cur, planners, attribute, typename): """Create a plot for a particular attribute. It will include data for all planners that have data for this attribute.""" labels = [] measurements = [] nanCounts = [] if typename == 'ENUM': cur.execute('SELECT description FROM enums where name IS "%s"' % attribute) descriptions = [t[0] for t in cur.fetchall()] numValues = len(descriptions) for planner in planners: cur.execute('SELECT %s FROM runs WHERE plannerid = %s AND %s IS NOT NULL' \ % (attribute, planner[0], attribute)) measurement = [t[0] for t in cur.fetchall() if t[0] != None] if len(measurement) > 0: cur.execute('SELECT count(*) FROM runs WHERE plannerid = %s AND %s IS NULL' \ % (planner[0], attribute)) nanCounts.append(cur.fetchone()[0]) labels.append(planner[1]) if typename == 'ENUM': scale = 100. / len(measurement) measurements.append( [measurement.count(i) * scale for i in range(numValues)]) else: measurements.append(measurement) if len(measurements) == 0: print('Skipping "%s": no available measurements' % attribute) return plt.clf() ax = plt.gca() if typename == 'ENUM': width = .5 measurements = np.transpose(np.vstack(measurements)) colsum = np.sum(measurements, axis=1) rows = np.where(colsum != 0)[0] heights = np.zeros((1, measurements.shape[1])) ind = range(measurements.shape[1]) legend_labels = [] for i in rows: plt.bar(ind, measurements[i], width, bottom=heights[0], color=matplotlib.cm.hot(int(floor(i * 256 / numValues))), label=descriptions[i]) heights = heights + measurements[i] xtickNames = plt.xticks([x + width / 2. for x in ind], labels, rotation=30, fontsize=8, ha='right') ax.set_ylabel(attribute.replace('_', ' ') + ' (%)') box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) props = matplotlib.font_manager.FontProperties() props.set_size('small') ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop=props) elif typename == 'BOOLEAN': width = .5 measurementsPercentage = [sum(m) * 100. / len(m) for m in measurements] ind = range(len(measurements)) plt.bar(ind, measurementsPercentage, width) ### uncommenting this line will remove the term 'kConfigDefault' from the labels for OMPL Solvers. ### Fits situations where you need more control in the plot, such as in an academic publication for example #labels = [l.replace('kConfigDefault', '') for l in labels] xtickNames = plt.xticks([x + width / 2. for x in ind], labels, rotation=30, fontsize=8, ha='right') ax.set_ylabel(attribute.replace('_', ' ') + ' (%)') plt.subplots_adjust( bottom=0.3 ) # Squish the plot into the upper 2/3 of the page. Leave room for labels else: if int(matplotlibversion.split('.')[0]) < 1: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5) else: plt.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5, bootstrap=1000) ax.set_ylabel(attribute.replace('_', ' ')) #xtickNames = plt.xticks(labels, rotation=30, fontsize=10) #plt.subplots_adjust(bottom=0.3) # Squish the plot into the upper 2/3 of the page. Leave room for labels ### uncommenting this line will remove the term 'kConfigDefault' from the labels for OMPL Solvers. ### Fits situations where you need more control in the plot, such as in an academic publication for example #labels = [l.replace('kConfigDefault', '') for l in labels] xtickNames = plt.setp(ax, xticklabels=labels) plt.setp(xtickNames, rotation=30, fontsize=8, ha='right') for tick in ax.xaxis.get_major_ticks( ): # shrink the font size of the x tick labels tick.label.set_fontsize(8) plt.subplots_adjust( bottom=0.3 ) # Squish the plot into the upper 2/3 of the page. Leave room for labels ax.set_xlabel('Motion planning algorithm', fontsize=12) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if max(nanCounts) > 0: maxy = max([max(y) for y in measurements]) for i in range(len(labels)): x = i + width / 2 if typename == 'BOOLEAN' else i + 1 ### uncommenting the next line, the number of failed planning attempts will be added to each bar # ax.text(x, .95*maxy, str(nanCounts[i]), horizontalalignment='center', size='small') plt.show()
command = 'python --version' proc = Popen(command,shell=True,universal_newlines=True, \ stdout=PIPE,stderr=STDOUT) @@ -285,7 +285,7 @@ def test_numpy_suported_version(self): """numpy version is supported """ min_acceptable_version = (1,5,1) - max_acceptable_version = (1,7,1) + max_acceptable_version = (1,8,1) try: from numpy import __version__ as numpy_lib_version version = tuple(map(int,numpy_lib_version.split('.'))) @@ -304,7 +304,7 @@ def test_matplotlib_suported_version(self): """matplotlib version is supported """ min_acceptable_version = (1,1,0) - max_acceptable_version = (1,3,1) + max_acceptable_version = (1,4,0) try: from matplotlib import __version__ as matplotlib_lib_version version = tuple(map(int,matplotlib_lib_version.split('.'))) @@ -345,7 +345,7 @@ def test_FastTree_supported_version(self): """FastTree is in path and version is supported """ - acceptable_version = (2,1,3) + acceptable_version = (2,1,7) self.assertTrue(app_path('FastTree'), "FastTree not found. This may or may not be a problem depending on "+\ "which components of QIIME you plan to use.")
def plot(self): # check if there is anything to plot if len(self.plot_instances) == 0: raise SystemExit('no data to plot.') if self.args['output_file'] is not None: # --output-file means don't depend on X, # so switch to a pure-image backend before doing any plotting. plt.switch_backend('agg') self.artists = [] plt.figure(figsize=(12,8), dpi=100, facecolor='w', edgecolor='w') axis = plt.subplot(111) # set xlim from min to max of logfile ranges xlim_min = min([pi.date_range[0] for pi in self.plot_instances]) xlim_max = max([pi.date_range[1] for pi in self.plot_instances]) if xlim_max < xlim_min: raise SystemExit('no data to plot.') xlabel = 'time' ylabel = '' # use timezone of first log file (may not always be what user wants but must make a choice) tz = self.logfiles[0].timezone tzformat = '%b %d\n%H:%M:%S' if tz == tzutc() else '%b %d\n%H:%M:%S%z' locator = AutoDateLocator(tz=tz, minticks=5, maxticks=10) formatter = AutoDateFormatter(locator, tz=tz) formatter.scaled = { 365.0 : '%Y', 30. : '%b %Y', 1.0 : '%b %d %Y', 1./24. : '%b %d %Y\n%H:%M:%S', 1./(24.*60.): '%b %d %Y\n%H:%M:%S', } # add timezone to format if not UTC if tz != tzutc(): formatter.scaled[1./24.] = '%b %d %Y\n%H:%M:%S%z' formatter.scaled[1./(24.*60.)] = '%b %d %Y\n%H:%M:%S%z' for i, plot_inst in enumerate(sorted(self.plot_instances, key=lambda pi: pi.sort_order)): self.artists.extend(plot_inst.plot(axis, i, len(self.plot_instances), (xlim_min, xlim_max) )) if hasattr(plot_inst, 'xlabel'): xlabel = plot_inst.xlabel if hasattr(plot_inst, 'ylabel'): ylabel = plot_inst.ylabel if self.args['output_file'] is None: self.print_shortcuts(scatter=isinstance(self.plot_instance, plottypes.ScatterPlotType)) axis.set_xlabel(xlabel) axis.set_xticklabels(axis.get_xticks(), rotation=90, fontsize=9) axis.xaxis.set_major_locator(locator) axis.xaxis.set_major_formatter(formatter) axis.set_xlim(date2num([xlim_min, xlim_max])) # ylabel for y axis if self.args['logscale']: ylabel += ' (log scale)' axis.set_ylabel(ylabel) # title and mtools link axis.set_title(self.args['title'] or ', '.join([l.name for l in self.logfiles if l.name != '<stdin>'])) plt.subplots_adjust(bottom=0.15, left=0.1, right=0.95, top=0.95) self.footnote = plt.annotate('created with mtools v%s: https://github.com/rueckstiess/mtools' % __version__, (10, 10), xycoords='figure pixels', va='bottom', fontsize=8) handles, labels = axis.get_legend_handles_labels() if len(labels) > 0: # only change fontsize if supported major, minor, _ = mpl_version.split('.') if (int(major), int(minor)) >= (1, 3): self.legend = axis.legend(loc='upper left', frameon=False, numpoints=1, fontsize=9) else: self.legend = axis.legend(loc='upper left', frameon=False, numpoints=1) if self.args['type'] == 'scatter': # enable legend picking for scatter plots for i, legend_line in enumerate(self.legend.get_lines()): legend_line.set_picker(10) legend_line._mt_legend_item = i # overwrite y-axis limits if set if self.args['ylimits'] != None: print self.args['ylimits'] axis.set_ylim( self.args['ylimits']) plt.gcf().canvas.mpl_connect('pick_event', self.onpick) plt.gcf().canvas.mpl_connect('key_press_event', self.onpress) if self.args['output_file'] is not None: plt.savefig(self.args['output_file']) else: plt.show()
def plot_attribute_per_query(self, cur, planner, attribute, typename, layout): measurements = [] if typename == 'ENUM': # TODO: Implement enum support return cur.execute('SELECT %s FROM runs WHERE plannerid = %s' % (attribute, planner[0])) measurement_including_nan = [t[0] for t in cur.fetchall()] if 0 == len(measurement_including_nan): rospy.logwarn( "No measurements for {} available!".format(attribute)) self.clearLayout(layout) return # Find the number of runs cur.execute('SELECT runcount FROM experiments WHERE id = 1') runcount = cur.fetchall()[0][0] # Find names and number of queries cur.execute('SELECT id FROM experiments') queries = [q[0] for q in cur.fetchall()] num_queries = len(queries) cur.execute('SELECT experimentid FROM runs WHERE plannerid = %s' % (planner[0])) queryid_to_run_mapping = [t[0] for t in cur.fetchall()] per_query_runcount = [] for query_id in queries: per_query_runcount.append(queryid_to_run_mapping.count(query_id)) matrix_measurements_with_nans = [] for runcount in per_query_runcount: matrix_measurements_with_nans.append( measurement_including_nan[0:runcount]) del measurement_including_nan[0:runcount] # Remove NaNs from the measurement array matrix_measurements = [] for per_query_result in matrix_measurements_with_nans: matrix_measurements.append( [x for x in per_query_result if x is not None]) # Add the unit if the attribute is known attribute = attribute.replace('_', ' ') if "time" in attribute: attribute += ' (s)' elif "clearance" in attribute: attribute += ' (m)' elif "cartesian" in attribute: attribute += '' elif "length" in attribute or "quality" in attribute or "smoothness" in attribute: attribute += ' (rad)' elif typename == "BOOLEAN": attribute += ' (%)' plt.clf() # GUI width = 5 height = 4 dpi = 100 fig = Figure(figsize=(width, height), dpi=dpi, facecolor=(1.0, 1.0, 1.0, 1.0)) ax = fig.add_subplot(111) figcanvas = FigureCanvas(fig) figcanvas.setParent(self._widget) FigureCanvas.setSizePolicy(figcanvas, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(figcanvas) if typename == 'BOOLEAN': width = .5 measurements_percentage = [] missing_measurements = [] missing_measurements_index = [] for i, m in enumerate(matrix_measurements): if 0 == len(m): measurements_percentage.append(0) missing_measurements.append(50) missing_measurements_index.append(i + width / 2) else: measurements_percentage.append(sum(m) * 100 / len(m)) idx = range(len(measurements_percentage)) ax.bar(idx, measurements_percentage, width) ax.scatter(missing_measurements_index, missing_measurements, color='r', marker='x') plt.setp(ax, xticks=[x + width / 2 for x in idx], xticklabels=[x + 1 for x in idx]) ax.set_ylim([0, 100]) ax.set_xlim([0, len(matrix_measurements)]) else: if int(matplotlibversion.split('.')[0]) < 1: ax.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5) else: ax.boxplot(matrix_measurements, notch=0, sym='k+', vert=1, whis=1.5, bootstrap=1000) for tick in ax.xaxis.get_major_ticks( ): # shrink the font size of the x tick labels tick.label.set_fontsize(8) for tick in ax.yaxis.get_major_ticks( ): # shrink the font size of the x tick labels tick.label.set_fontsize(8) self.clearLayout(layout) layout.addWidget(figcanvas)
def plot_attribute_per_query(self, cur, planner, attribute, typename, layout): # Plotting for the selected planner results for each query measurements = [] cur.execute('SELECT %s FROM runs WHERE plannerid = %s' % (attribute, planner[0])) measurement_including_nan = [t[0] for t in cur.fetchall()] if 0 == len(measurement_including_nan): rospy.logwarn("No measurements for {} available!".format(attribute)) self.clearLayout(layout) return cur.execute('SELECT experimentid FROM runs WHERE plannerid = %s' % (planner[0])) queryid_to_run_mapping = [t[0] for t in cur.fetchall()] per_query_runcount = [] for query in self.queries: per_query_runcount.append(queryid_to_run_mapping.count(query[0])) matrix_measurements_with_nans = [] for runcount in per_query_runcount: matrix_measurements_with_nans.append(measurement_including_nan[0: runcount]) del measurement_including_nan[0: runcount] # Remove NaNs from the measurement array matrix_measurements = [] for per_query_result in matrix_measurements_with_nans: matrix_measurements.append([x for x in per_query_result if x is not None]) plt.clf() # GUI width = 5 height = 4 dpi = 100 fig = Figure(figsize=(width, height), dpi=dpi, facecolor=(1.0, 1.0, 1.0, 1.0)) ax = fig.add_subplot(111) figcanvas = FigureCanvas(fig) figcanvas.setParent(self._widget) FigureCanvas.setSizePolicy(figcanvas, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(figcanvas) if typename == 'BOOLEAN': width = .5 measurements_percentage = [] missing_measurements = [] missing_measurements_index = [] for i, m in enumerate(matrix_measurements): if 0 == len(m): measurements_percentage.append(0) missing_measurements.append(50) missing_measurements_index.append(i + width / 2) else: measurements_percentage.append(sum(m) * 100 / per_query_runcount[i]) idx = range(len(measurements_percentage)) ax.bar(idx, measurements_percentage, width) ax.scatter(missing_measurements_index, missing_measurements, color='r', marker='x') plt.setp(ax, xticks=[x + width / 2 for x in idx], xticklabels=[x + 1 for x in idx]) ax.set_ylim([0, 100]) ax.set_xlim([0, len(matrix_measurements)]) else: if int(matplotlibversion.split('.')[0]) < 1: ax.boxplot(measurements, notch=0, sym='r+', vert=1, whis=1.5) else: ax.boxplot(matrix_measurements, notch=0, sym='r+', vert=1, whis=1.5, bootstrap=1000) for tick in ax.xaxis.get_major_ticks(): # shrink the font size of the x tick labels tick.label.set_fontsize(7) for tick in ax.yaxis.get_major_ticks(): # shrink the font size of the x tick labels tick.label.set_fontsize(7) fig.subplots_adjust(bottom=0.1, top=0.90, left=0.08, right=0.98) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if "clearance" in attribute: ax.ticklabel_format(style='sci', axis='y', scilimits=(-3, 4), useLocale=True) ax.yaxis.offsetText.set_fontsize(7) self.clearLayout(layout) layout.addWidget(figcanvas)
# We check what extensions are available ########################################################### __extensions__ = { 'matplotlib': False, 'mplot3d': False, 'pygmo': False, 'scikit-learn': False, 'scipy': False } # 1 - matplotlib try: from matplotlib import __version__ as matplotlib_ver __extensions__['matplotlib'] = True # We detect the version and if more than 1.1.0 mplot3d is there mver = matplotlib_ver.split('.') mver = int(mver[0]) * 100 + int(mver[1]) * 10 if mver >= 110: __extensions__['mplot3d'] = True del mver except ImportError: pass # 2 - pygmo2 try: from pygmo import __version__ as pygmo_ver __extensions__['pygmo'] = True except ImportError: pass # 3 - scikit-learn is installed
def plot_attribute(self, cur, planners, attribute, typename, layout): labels = [] measurements = [] nanCounts = [] if typename == 'ENUM': cur.execute('SELECT description FROM enums where name IS "%s"' % attribute) descriptions = [t[0] for t in cur.fetchall()] numValues = len(descriptions) for planner in planners: cur.execute( 'SELECT %s FROM runs WHERE plannerid = %s AND %s IS NOT NULL' % (attribute, planner[0], attribute)) measurement = [t[0] for t in cur.fetchall() if t[0] != None] if len(measurement) > 0: cur.execute( 'SELECT count(*) FROM runs WHERE plannerid = %s AND %s IS NULL' % (planner[0], attribute)) nanCounts.append(cur.fetchone()[0]) labels.append(planner[1]) if typename == 'ENUM': scale = 100. / len(measurement) measurements.append([ measurement.count(i) * scale for i in range(numValues) ]) else: measurements.append(measurement) if len(measurements) == 0: print('Skipping "%s": no available measurements' % attribute) return # Add the unit if the attribute is known attribute = attribute.replace('_', ' ') if "time" in attribute: attribute += ' (s)' elif "clearance" in attribute: attribute += ' (m)' elif "cartesian" in attribute: attribute += '' elif "length" in attribute or "quality" in attribute or "smoothness" in attribute: attribute += ' (rad)' elif typename == "BOOLEAN": attribute += ' (%)' plt.clf() # GUI width = 5 height = 4 dpi = 100 fig = Figure(figsize=(width, height), dpi=dpi, facecolor=(1.0, 1.0, 1.0, 1.0)) ax = fig.add_subplot(111) figcanvas = FigureCanvas(fig) figcanvas.setParent(self._widget) FigureCanvas.setSizePolicy(figcanvas, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(figcanvas) # ax = plt.gca() if typename == 'ENUM': width = .5 measurements = np.transpose(np.vstack(measurements)) colsum = np.sum(measurements, axis=1) rows = np.where(colsum != 0)[0] heights = np.zeros((1, measurements.shape[1])) ind = range(measurements.shape[1]) for i in rows: ax.bar(ind, measurements[i], width, bottom=heights[0], color=matplotlib.cm.hot(int(floor(i * 256 / numValues))), label=descriptions[i]) heights = heights + measurements[i] plt.setp(ax, xticks=[x + width / 2. for x in ind]) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) props = matplotlib.font_manager.FontProperties() props.set_size('small') ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop=props) elif typename == 'BOOLEAN': width = .5 measurementsPercentage = [ sum(m) * 100. / len(m) for m in measurements ] ind = range(len(measurements)) ax.bar(ind, measurementsPercentage, width) plt.setp(ax, xticks=[x + width / 2. for x in ind]) else: if int(matplotlibversion.split('.')[0]) < 1: ax.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5) else: ax.boxplot(measurements, notch=0, sym='k+', vert=1, whis=1.5, bootstrap=1000) xtickNames = plt.setp(ax, xticklabels=labels) plt.setp(xtickNames, rotation=90) for tick in ax.xaxis.get_major_ticks( ): # shrink the font size of the x tick labels tick.label.set_fontsize(8) for tick in ax.yaxis.get_major_ticks( ): # shrink the font size of the x tick labels tick.label.set_fontsize(8) fig.subplots_adjust(bottom=0.3, top=0.95, left=0.1, right=0.98) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) if max(nanCounts) > 0: maxy = max([max(y) for y in measurements]) for i in range(len(labels)): x = i + width / 2 if typename == 'BOOLEAN' else i + 1 ax.text(x, .95 * maxy, str(nanCounts[i]), horizontalalignment='center', size='small') self.clearLayout(layout) layout.addWidget(figcanvas)