def _show_form(props, title, parent): form = [] for ppty in props: if isinstance(ppty, Property): # proper property form.append(ppty.get_form_line()) elif isinstance(ppty, six.string_types): # tuple form.append((None, "<b>{}</b>".format(ppty))) else: # separator form.append((None, None)) return formlayout.fedit(form, title=title, parent=parent, icon=get_icon('edit'), apply= lambda d, p=props, par=parent: _apply_data_to_properties(p, d, par) )
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator # Get / General xmin, xmax = map(float, axes.get_xlim()) ymin, ymax = map(float, axes.get_ylim()) general = [('Title', axes.get_title()), sep, (None, "<b>X-Axis</b>"), ('Min', xmin), ('Max', xmax), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log']), sep, (None, "<b>Y-Axis</b>"), ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log']), sep, ('(Re-)Generate automatic legend', False), ] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() # Sorting for default labels (_lineXXX, _imageXXX). def cmp_key(label): match = re.match(r"(_line|_image)(\d+)", label) if match: return match.group(1), int(match.group(2)) else: return label, 0 # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] def prepare_data(d, init): """Prepare entry for FormLayout. `d` is a mapping of shorthands to style names (a single style may have multiple shorthands, in particular the shorthands `None`, `"None"`, `"none"` and `""` are synonyms); `init` is one shorthand of the initial style. This function returns an list suitable for initializing a FormLayout combobox, namely `[initial_name, (shorthand, style_name), (shorthand, style_name), ...]`. """ # Drop duplicate shorthands from dict (by overwriting them during # the dict comprehension). name2short = {name: short for short, name in d.items()} # Convert back to {shorthand: name}. short2name = {short: name for name, short in name2short.items()} # Find the kept shorthand for the style specified by init. canonical_init = name2short[d[init]] # Sort by representation and prepend the initial value. return ([canonical_init] + sorted(short2name.items(), key=lambda short_and_name: short_and_name[1])) curvelabels = sorted(linedict, key=cmp_key) for label in curvelabels: line = linedict[label] color = mcolors.to_hex( mcolors.to_rgba(line.get_color(), line.get_alpha()), keep_alpha=True) ec = mcolors.to_hex(line.get_markeredgecolor(), keep_alpha=True) fc = mcolors.to_hex(line.get_markerfacecolor(), keep_alpha=True) curvedata = [ ('Label', label), sep, (None, '<b>Line</b>'), ('Line style', prepare_data(LINESTYLES, line.get_linestyle())), ('Draw style', prepare_data(DRAWSTYLES, line.get_drawstyle())), ('Width', line.get_linewidth()), ('Color (RGBA)', color), sep, (None, '<b>Marker</b>'), ('Style', prepare_data(MARKERS, line.get_marker())), ('Size', line.get_markersize()), ('Face color (RGBA)', fc), ('Edge color (RGBA)', ec)] curves.append([curvedata, label, ""]) # Is there a curve displayed? has_curve = bool(curves) # Get / Images imagedict = {} for image in axes.get_images(): label = image.get_label() if label == '_nolegend_': continue imagedict[label] = image imagelabels = sorted(imagedict, key=cmp_key) images = [] cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())] for label in imagelabels: image = imagedict[label] cmap = image.get_cmap() if cmap not in cm.cmap_d.values(): cmaps = [(cmap, cmap.name)] + cmaps low, high = image.get_clim() imagedata = [ ('Label', label), ('Colormap', [cmap.name] + cmaps), ('Min. value', low), ('Max. value', high), ('Interpolation', [image.get_interpolation()] + [(name, name) for name in sorted(image.iterpnames)])] images.append([imagedata, label, ""]) # Is there an image displayed? has_image = bool(images) datalist = [(general, "Axes", "")] if curves: datalist.append((curves, "Curves", "")) if images: datalist.append((images, "Images", "")) def apply_callback(data): """This function will be called to apply changes""" general = data.pop(0) curves = data.pop(0) if has_curve else [] images = data.pop(0) if has_image else [] if data: raise ValueError("Unexpected field") # Set / General (title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, generate_legend) = general if axes.get_xscale() != xscale: axes.set_xscale(xscale) if axes.get_yscale() != yscale: axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] (label, linestyle, drawstyle, linewidth, color, marker, markersize, markerfacecolor, markeredgecolor) = curve line.set_label(label) line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) rgba = mcolors.to_rgba(color) line.set_alpha(None) line.set_color(rgba) if marker is not 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # Set / Images for index, image_settings in enumerate(images): image = imagedict[imagelabels[index]] label, cmap, low, high, interpolation = image_settings image.set_label(label) image.set_cmap(cm.get_cmap(cmap)) image.set_clim(*sorted([low, high])) image.set_interpolation(interpolation) # re-generate legend, if checkbox is checked if generate_legend: draggable = None ncol = 1 if axes.legend_ is not None: old_legend = axes.get_legend() draggable = old_legend._draggable is not None ncol = old_legend._ncol new_legend = axes.legend(ncol=ncol) if new_legend: new_legend.draggable(draggable) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator has_curve = len(axes.get_lines()) > 0 # Get / General xmin, xmax = axes.get_xlim() ymin, ymax = axes.get_ylim() general = [('Title', axes.get_title()), sep, (None, "<b>X-Axis</b>"), ('Min', xmin), ('Max', xmax), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log']), sep, (None, "<b>Y-Axis</b>"), ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log']), sep, ('(Re-)Generate automatic legend', False), ] if has_curve: # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] linestyles = list(six.iteritems(LINESTYLES)) markers = list(six.iteritems(MARKERS)) curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] color = rgb2hex(colorConverter.to_rgb(line.get_color())) ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor())) fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor())) curvedata = [('Label', label), sep, (None, '<b>Line</b>'), ('Style', [line.get_linestyle()] + linestyles), ('Width', line.get_linewidth()), ('Color', color), sep, (None, '<b>Marker</b>'), ('Style', [line.get_marker()] + markers), ('Size', line.get_markersize()), ('Facecolor', fc), ('Edgecolor', ec), ] curves.append([curvedata, label, ""]) # make sure that there is at least one displayed curve has_curve = bool(curves) datalist = [(general, "Axes", "")] if has_curve: datalist.append((curves, "Curves", "")) def apply_callback(data): """This function will be called to apply changes""" if has_curve: general, curves = data else: general, = data # Set / General title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, \ generate_legend = general axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) if has_curve: # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] label, linestyle, linewidth, color, \ marker, markersize, markerfacecolor, markeredgecolor \ = curve line.set_label(label) line.set_linestyle(linestyle) line.set_linewidth(linewidth) line.set_color(color) if marker is not 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # re-generate legend, if checkbox is checked if generate_legend: if axes.legend_ is not None: old_legend = axes.get_legend() new_legend = axes.legend(ncol=old_legend._ncol) new_legend.draggable(old_legend._draggable is not None) else: new_legend = axes.legend() new_legend.draggable(True) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator # Get / General xmin, xmax = map(float, axes.get_xlim()) ymin, ymax = map(float, axes.get_ylim()) general = [ ('Title', axes.get_title()), sep, (None, "<b>X-Axis</b>"), ('Min', xmin), ('Max', xmax), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log']), sep, (None, "<b>Y-Axis</b>"), ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log']), sep, ('(Re-)Generate automatic legend', False), ] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() # Sorting for default labels (_lineXXX, _imageXXX). def cmp_key(label): match = re.match(r"(_line|_image)(\d+)", label) if match: return match.group(1), int(match.group(2)) else: return label, 0 # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] def prepare_data(d, init): """Prepare entry for FormLayout. `d` is a mapping of shorthands to style names (a single style may have multiple shorthands, in particular the shorthands `None`, `"None"`, `"none"` and `""` are synonyms); `init` is one shorthand of the initial style. This function returns an list suitable for initializing a FormLayout combobox, namely `[initial_name, (shorthand, style_name), (shorthand, style_name), ...]`. """ # Drop duplicate shorthands from dict (by overwriting them during # the dict comprehension). name2short = {name: short for short, name in d.items()} # Convert back to {shorthand: name}. short2name = {short: name for name, short in name2short.items()} # Find the kept shorthand for the style specified by init. canonical_init = name2short[d[init]] # Sort by representation and prepend the initial value. return ([canonical_init] + sorted( short2name.items(), key=lambda short_and_name: short_and_name[1])) curvelabels = sorted(linedict, key=cmp_key) for label in curvelabels: line = linedict[label] color = rgb2hex(colorConverter.to_rgb(line.get_color())) ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor())) fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor())) curvedata = [ ('Label', label), sep, (None, '<b>Line</b>'), ('Line Style', prepare_data(LINESTYLES, line.get_linestyle())), ('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())), ('Width', line.get_linewidth()), ('Color', color), sep, (None, '<b>Marker</b>'), ('Style', prepare_data(MARKERS, line.get_marker())), ('Size', line.get_markersize()), ('Facecolor', fc), ('Edgecolor', ec) ] curves.append([curvedata, label, ""]) # Is there a curve displayed? has_curve = bool(curves) # Get / Images imagedict = {} for image in axes.get_images(): label = image.get_label() if label == '_nolegend_': continue imagedict[label] = image imagelabels = sorted(imagedict, key=cmp_key) images = [] cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())] for label in imagelabels: image = imagedict[label] cmap = image.get_cmap() if cmap not in cm.cmap_d.values(): cmaps = [(cmap, cmap.name)] + cmaps low, high = image.get_clim() imagedata = [('Label', label), ('Colormap', [cmap.name] + cmaps), ('Min. value', low), ('Max. value', high)] images.append([imagedata, label, ""]) # Is there an image displayed? has_image = bool(images) datalist = [(general, "Axes", "")] if curves: datalist.append((curves, "Curves", "")) if images: datalist.append((images, "Images", "")) def apply_callback(data): """This function will be called to apply changes""" general = data.pop(0) curves = data.pop(0) if has_curve else [] images = data.pop(0) if has_image else [] if data: raise ValueError("Unexpected field") # Set / General (title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, generate_legend) = general if axes.get_xscale() != xscale: axes.set_xscale(xscale) if axes.get_yscale() != yscale: axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] (label, linestyle, drawstyle, linewidth, color, marker, markersize, markerfacecolor, markeredgecolor) = curve line.set_label(label) line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) line.set_color(color) if marker is not 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # Set / Images for index, image_settings in enumerate(images): image = imagedict[imagelabels[index]] label, cmap, low, high = image_settings image.set_label(label) image.set_cmap(cm.get_cmap(cmap)) image.set_clim(*sorted([low, high])) # re-generate legend, if checkbox is checked if generate_legend: draggable = None ncol = 1 if axes.legend_ is not None: old_legend = axes.get_legend() draggable = old_legend._draggable is not None ncol = old_legend._ncol new_legend = axes.legend(ncol=ncol) if new_legend: new_legend.draggable(draggable) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator # Get / General # Cast to builtin floats as they have nicer reprs. xmin, xmax = map(float, axes.get_xlim()) ymin, ymax = map(float, axes.get_ylim()) if 'labelsize' in axes.xaxis._major_tick_kw: _ticksize = int(axes.xaxis._major_tick_kw['labelsize']) else: _ticksize = 15 general = [(None, "<b>Figure Title</b>"), ('Title', axes.get_title()), ('Font Size', int(axes.title.get_fontsize())), sep, (None, "<b>Axes settings</b>"), ('Label Size', int(axes.xaxis.label.get_fontsize())), ('Tick Size', _ticksize), sep, (None, "<b>X-Axis</b>"), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log', 'logit']), sep, (None, "<b>Y-Axis</b>"), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log', 'logit'])] if axes.legend_ is not None: old_legend = axes.get_legend() _draggable = old_legend._draggable is not None _ncol = old_legend._ncol _fontsize = int(old_legend._fontsize) _frameon = True #old_legend._drawFrame _shadow = old_legend.shadow _fancybox = type(old_legend.legendPatch.get_boxstyle() ) == matplotlib.patches.BoxStyle.Round _framealpha = old_legend.get_frame().get_alpha() else: _draggable = False _ncol = 1 _fontsize = 15 _frameon = True _shadow = True _fancybox = True _framealpha = 0.5 legend = [('Draggable', _draggable), ('columns', _ncol), ('Font Size', _fontsize), ('Frame', _frameon), ('Shadow', _shadow), ('FancyBox', _fancybox), ('Alpha', _framealpha)] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() # Sorting for default labels (_lineXXX, _imageXXX). def cmp_key(label): match = re.match(r"(_line|_image)(\d+)", label) if match: return match.group(1), int(match.group(2)) else: return label, 0 # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] def prepare_data(d, init): """Prepare entry for FormLayout. `d` is a mapping of shorthands to style names (a single style may have multiple shorthands, in particular the shorthands `None`, `"None"`, `"none"` and `""` are synonyms); `init` is one shorthand of the initial style. This function returns an list suitable for initializing a FormLayout combobox, namely `[initial_name, (shorthand, style_name), (shorthand, style_name), ...]`. """ if init not in d: d = {**d, init: str(init)} # Drop duplicate shorthands from dict (by overwriting them during # the dict comprehension). name2short = {name: short for short, name in d.items()} # Convert back to {shorthand: name}. short2name = {short: name for name, short in name2short.items()} # Find the kept shorthand for the style specified by init. canonical_init = name2short[d[init]] # Sort by representation and prepend the initial value. return ([canonical_init] + sorted( short2name.items(), key=lambda short_and_name: short_and_name[1])) curvelabels = sorted(linedict, key=cmp_key) for label in curvelabels: line = linedict[label] color = mcolors.to_hex(mcolors.to_rgba(line.get_color(), line.get_alpha()), keep_alpha=True) ec = mcolors.to_hex(mcolors.to_rgba(line.get_markeredgecolor(), line.get_alpha()), keep_alpha=True) fc = mcolors.to_hex(mcolors.to_rgba(line.get_markerfacecolor(), line.get_alpha()), keep_alpha=True) curvedata = [ ('Label', label), sep, (None, '<b>Line</b>'), ('Line style', prepare_data(LINESTYLES, line.get_linestyle())), ('Draw style', prepare_data(DRAWSTYLES, line.get_drawstyle())), ('Width', line.get_linewidth()), ('Color (RGBA)', color), sep, (None, '<b>Marker</b>'), ('Style', prepare_data(MARKERS, line.get_marker())), ('Size', line.get_markersize()), ('Face color (RGBA)', fc), ('Edge color (RGBA)', ec) ] curves.append([curvedata, label, ""]) # Is there a curve displayed? has_curve = bool(curves) # Get / Images imagedict = {} for image in axes.get_images(): label = image.get_label() if label == '_nolegend_': continue imagedict[label] = image imagelabels = sorted(imagedict, key=cmp_key) images = [] cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())] for label in imagelabels: image = imagedict[label] cmap = image.get_cmap() if cmap not in cm.cmap_d.values(): cmaps = [(cmap, cmap.name)] + cmaps low, high = image.get_clim() imagedata = [('Label', label), ('Colormap', [cmap.name] + cmaps), ('Min. value', low), ('Max. value', high), ('Interpolation', [image.get_interpolation()] + [(name, name) for name in sorted(mimage.interpolations_names)])] images.append([imagedata, label, ""]) # Is there an image displayed? has_image = bool(images) datalist = [(general, "Axes", ""), (legend, "Legend", "")] if curves: datalist.append((curves, "Curves", "")) if images: datalist.append((images, "Images", "")) def apply_callback(data): """This function will be called to apply changes""" general = data.pop(0) legend = data.pop(0) curves = data.pop(0) if has_curve else [] images = data.pop(0) if has_image else [] if data: raise ValueError("Unexpected field") # Set / General (title, titlesize, labelsize, ticksize, grid, xlabel, xscale, ylabel, yscale) = general if axes.get_xscale() != xscale: axes.set_xscale(xscale) if axes.get_yscale() != yscale: axes.set_yscale(yscale) axes.set_title(title) axes.title.set_fontsize(titlesize) axes.set_xlabel(xlabel) axes.xaxis.label.set_size(labelsize) axes.xaxis.set_tick_params(labelsize=ticksize) axes.set_ylabel(ylabel) axes.yaxis.label.set_size(labelsize) axes.yaxis.set_tick_params(labelsize=ticksize) axes.grid(grid) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() # Set / Legend ( leg_draggable, leg_ncol, leg_fontsize, leg_frameon, leg_shadow, leg_fancybox, leg_framealpha, ) = legend new_legend = axes.legend(ncol=leg_ncol, fontsize=float(leg_fontsize), frameon=leg_frameon, shadow=leg_shadow, framealpha=leg_framealpha, fancybox=leg_fancybox) new_legend.set_draggable(leg_draggable) # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] (label, linestyle, drawstyle, linewidth, color, marker, markersize, markerfacecolor, markeredgecolor) = curve line.set_label(label) line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) rgba = mcolors.to_rgba(color) line.set_alpha(None) line.set_color(rgba) if marker != 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # Set / Images for index, image_settings in enumerate(images): image = imagedict[imagelabels[index]] label, cmap, low, high, interpolation = image_settings image.set_label(label) image.set_cmap(cm.get_cmap(cmap)) image.set_clim(*sorted([low, high])) image.set_interpolation(interpolation) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator # Get / General # Cast to builtin floats as they have nicer reprs. xmin, xmax = map(float, axes.get_xlim()) ymin, ymax = map(float, axes.get_ylim()) general = [('Title', axes.get_title()), sep, (None, "<b>X-Axis</b>"), ('Left', xmin), ('Right', xmax), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log', 'logit']), sep, (None, "<b>Y-Axis</b>"), ('Bottom', ymin), ('Top', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log', 'logit']), sep, ('(Re-)Generate automatic legend', False), ] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() # Sorting for default labels (_lineXXX, _imageXXX). def cmp_key(label): match = re.match(r"(_line|_image)(\d+)", label) if match: return match.group(1), int(match.group(2)) else: return label, 0 # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == NOLEGEND: continue linedict[label] = line for idx, ebar in enumerate([cont for cont in axes.containers if outils.is_errorbar(cont)]): label = ebar.get_label() if label is None: label = "" elif label == NOLEGEND: label = "unnamed{:d}".format(idx) linedict[label] = ebar curves = [] curvelabels = sorted(linedict, key=cmp_key) for label in curvelabels: line = linedict[label] if outils.is_errorbar(line): line = line[0] color = mcolors.to_hex( mcolors.to_rgba(line.get_color(), line.get_alpha()), keep_alpha=True) ec = mcolors.to_hex( mcolors.to_rgba(line.get_markeredgecolor(), line.get_alpha()), keep_alpha=True) fc = mcolors.to_hex( mcolors.to_rgba(line.get_markerfacecolor(), line.get_alpha()), keep_alpha=True) ls_data = outils.prepare_formdata(LINESTYLES, line.get_linestyle()) ds_data = outils.prepare_formdata(DRAWSTYLES, line.get_drawstyle()) ms_data = outils.prepare_formdata(MARKERS, line.get_marker()) curvedata = [ ('Label', label), ('Visible', line.get_visible()), sep, (None, '<b>Line</b>'), ('Line style', [ls_data[0]] + ls_data[1]), ('Draw style', [ds_data[0]] + ds_data[1]), ('Width', line.get_linewidth()), ('Color (RGBA)', color), sep, (None, '<b>Marker</b>'), ('Style', [ms_data[0]] + ms_data[1]), ('Size', line.get_markersize()), ('Face color (RGBA)', fc), ('Edge color (RGBA)', ec), ] curves.append([curvedata, label, ""]) # Is there a curve displayed? has_curve = bool(curves) # Get / Images imagedict = {} for image in axes.get_images(): label = image.get_label() if label == NOLEGEND: continue imagedict[label] = image imagelabels = sorted(imagedict, key=cmp_key) images = [] cmaps = [(cmap, name) for name, cmap in sorted(cm.cmap_d.items())] for label in imagelabels: image = imagedict[label] cmap = image.get_cmap() if cmap not in cm.cmap_d.values(): cmaps = [(cmap, cmap.name)] + cmaps low, high = image.get_clim() imagedata = [ ('Label', label), ('Colormap', [cmap.name] + cmaps), ('Min. value', low), ('Max. value', high), ('Interpolation', [image.get_interpolation()] + [(name, name) for name in sorted(mimage.interpolations_names)])] images.append([imagedata, label, ""]) # Is there an image displayed? has_image = bool(images) datalist = [(general, "Axes", "")] if curves: datalist.append((curves, "Curves", "")) if images: datalist.append((images, "Images", "")) def apply_callback(data): """This function will be called to apply changes""" orig_xlim = axes.get_xlim() orig_ylim = axes.get_ylim() general = data.pop(0) curves = data.pop(0) if has_curve else [] images = data.pop(0) if has_image else [] if data: raise ValueError("Unexpected field") # Set / General (title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, generate_legend) = general if axes.get_xscale() != xscale: axes.set_xscale(xscale) if axes.get_yscale() != yscale: axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() # Set / Curves for index, curve in enumerate(curves): (label, visible, linestyle, drawstyle, linewidth, color, marker, markersize, markerfacecolor, markeredgecolor) = curve line = linedict[curvelabels[index]] hbar = None vbar = None ebar = None line.set_label(label) if outils.is_errorbar(line): ebar = line hbar = ebar[1] vbar = ebar[2] line = ebar[0] line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) rgba = mcolors.to_rgba(color) line.set_alpha(None) line.set_color(rgba) line.set_visible(visible) for bar in [hbar, vbar]: if bar is not None: for b in bar: b.set_linewidth(linewidth) b.set_color(rgba) b.set_visible(visible) if marker is not 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # Set / Images for index, image_settings in enumerate(images): image = imagedict[imagelabels[index]] label, cmap, low, high, interpolation = image_settings image.set_label(label) image.set_cmap(cm.get_cmap(cmap)) image.set_clim(*sorted([low, high])) image.set_interpolation(interpolation) # re-generate legend, if checkbox is checked if generate_legend: outils.regenerate_legend(axes) # Redraw figure = axes.get_figure() figure.canvas.draw() if not (axes.get_xlim() == orig_xlim and axes.get_ylim() == orig_ylim): figure.canvas.toolbar.push_current() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator has_curve = len(axes.get_lines()) > 0 # Get / General #xmin, xmax = axes.get_xlim() #ymin, ymax = axes.get_ylim() #general = [('Title', axes.get_title()), # sep, # (None, "<b>X-Axis</b>"), # ('Min', xmin), ('Max', xmax), # ('Label', axes.get_xlabel()), # ('Scale', [axes.get_xscale(), 'linear', 'log']), # sep, # (None, "<b>Y-Axis</b>"), # ('Min', ymin), ('Max', ymax), # ('Label', axes.get_ylabel()), # ('Scale', [axes.get_yscale(), 'linear', 'log']) # ] if not has_curve: return if has_curve: # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] linestyles = LINESTYLES.items() markers = MARKERS.items() curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] curvedata = [ ('Label', label), sep, (None, '<b>Line</b>'), ('Style', [line.get_linestyle()] + list(linestyles)), ('Width', line.get_linewidth()), ('Color', col2hex(line.get_color())), sep, (None, '<b>Marker</b>'), ('Style', [line.get_marker()] + list(markers)), ('Size', line.get_markersize()), ('Facecolor', col2hex(line.get_markerfacecolor())), ('Edgecolor', col2hex(line.get_markeredgecolor())), ] curves.append([curvedata, label, ""]) #datalist = [(general, "Axes", "")] #if has_curve: #datalist.append((curves, "Curves", "")) datalist = [(curves, "Curves", "")] def apply_callback(data): """This function will be called to apply changes""" #if has_curve: # general, curves = data #else: # general, = data curves = data[0] # Set / General #title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale = general #axes.set_xscale(xscale) #axes.set_yscale(yscale) #axes.set_title(title) #axes.set_xlim(xmin, xmax) #axes.set_xlabel(xlabel) #axes.set_ylim(ymin, ymax) #axes.set_ylabel(ylabel) if has_curve: # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] label, linestyle, linewidth, color, \ marker, markersize, markerfacecolor, markeredgecolor = curve line.set_label(label) line.set_linestyle(linestyle) line.set_linewidth(linewidth) line.set_color(color) if marker != 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # Redraw figure = axes.get_figure() figure.canvas.draw() if formlayout is not None: data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator has_curve = len(axes.get_lines()) > 0 # Get / General xmin, xmax = axes.get_xlim() ymin, ymax = axes.get_ylim() general = [('Title', axes.get_title()), sep, (None, "<b>X-Axis</b>"), ('Min', xmin), ('Max', xmax), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log']), sep, (None, "<b>Y-Axis</b>"), ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log']), sep, ('(Re-)Generate automatic legend', False), ] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() if has_curve: # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] def prepare_data(d, init): """Prepare entry for FormLayout. `d` is a mapping of shorthands to style names (a single style may have multiple shorthands, in particular the shorthands `None`, `"None"`, `"none"` and `""` are synonyms); `init` is one shorthand of the initial style. This function returns an list suitable for initializing a FormLayout combobox, namely `[initial_name, (shorthand, style_name), (shorthand, style_name), ...]`. """ # Drop duplicate shorthands from dict (by overwriting them during # the dict comprehension). name2short = {name: short for short, name in d.items()} # Convert back to {shorthand: name}. short2name = {short: name for name, short in name2short.items()} # Find the kept shorthand for the style specified by init. canonical_init = name2short[d[init]] # Sort by representation and prepend the initial value. return ([canonical_init] + sorted(short2name.items(), key=lambda short_and_name: short_and_name[1])) curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] color = rgb2hex(colorConverter.to_rgb(line.get_color())) ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor())) fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor())) curvedata = [ ('Label', label), sep, (None, '<b>Line</b>'), ('Line Style', prepare_data(LINESTYLES, line.get_linestyle())), ('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())), ('Width', line.get_linewidth()), ('Color', color), sep, (None, '<b>Marker</b>'), ('Style', prepare_data(MARKERS, line.get_marker())), ('Size', line.get_markersize()), ('Facecolor', fc), ('Edgecolor', ec)] curves.append([curvedata, label, ""]) # make sure that there is at least one displayed curve has_curve = bool(curves) datalist = [(general, "Axes", "")] if has_curve: datalist.append((curves, "Curves", "")) def apply_callback(data): """This function will be called to apply changes""" if has_curve: general, curves = data else: general, = data # Set / General title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, \ generate_legend = general axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() if has_curve: # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] label, linestyle, drawstyle, linewidth, color, \ marker, markersize, markerfacecolor, markeredgecolor \ = curve line.set_label(label) line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) line.set_color(color) if marker is not 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # re-generate legend, if checkbox is checked if generate_legend: draggable = None ncol = None if axes.legend_ is not None: old_legend = axes.get_legend() draggable = old_legend._draggable is not None ncol = old_legend._ncol new_legend = axes.legend(ncol=ncol) if new_legend: new_legend.draggable(draggable) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator has_curve = len(axes.get_lines()) > 0 # Get / General xmin, xmax = axes.get_xlim() ymin, ymax = axes.get_ylim() general = [ ("Title", axes.get_title()), sep, (None, "<b>X-Axis</b>"), ("Min", xmin), ("Max", xmax), ("Label", axes.get_xlabel()), ("Scale", [axes.get_xscale(), "linear", "log"]), sep, (None, "<b>Y-Axis</b>"), ("Min", ymin), ("Max", ymax), ("Label", axes.get_ylabel()), ("Scale", [axes.get_yscale(), "linear", "log"]), sep, ("(Re-)Generate automatic legend", False), ] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() if has_curve: # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == "_nolegend_": continue linedict[label] = line curves = [] linestyles = list(six.iteritems(LINESTYLES)) drawstyles = list(six.iteritems(DRAWSTYLES)) markers = list(six.iteritems(MARKERS)) curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] color = rgb2hex(colorConverter.to_rgb(line.get_color())) ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor())) fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor())) curvedata = [ ("Label", label), sep, (None, "<b>Line</b>"), ("Line Style", [line.get_linestyle()] + linestyles), ("Draw Style", [line.get_drawstyle()] + drawstyles), ("Width", line.get_linewidth()), ("Color", color), sep, (None, "<b>Marker</b>"), ("Style", [line.get_marker()] + markers), ("Size", line.get_markersize()), ("Facecolor", fc), ("Edgecolor", ec), ] curves.append([curvedata, label, ""]) # make sure that there is at least one displayed curve has_curve = bool(curves) datalist = [(general, "Axes", "")] if has_curve: datalist.append((curves, "Curves", "")) def apply_callback(data): """This function will be called to apply changes""" if has_curve: general, curves = data else: general, = data # Set / General title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, generate_legend = general axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() if has_curve: # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] label, linestyle, drawstyle, linewidth, color, marker, markersize, markerfacecolor, markeredgecolor = ( curve ) line.set_label(label) line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) line.set_color(color) if marker is not "none": line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # re-generate legend, if checkbox is checked if generate_legend: draggable = None ncol = 1 if axes.legend_ is not None: old_legend = axes.get_legend() draggable = old_legend._draggable is not None ncol = old_legend._ncol new_legend = axes.legend(ncol=ncol) if new_legend: new_legend.draggable(draggable) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit( datalist, title="Figure options", parent=parent, icon=get_icon("qt4_editor_options.svg"), apply=apply_callback ) if data is not None: apply_callback(data)
def figure_edit(axes, parent=None): """Edit matplotlib figure options""" sep = (None, None) # separator has_curve = len(axes.get_lines()) > 0 # Get / General xmin, xmax = map(float, axes.get_xlim()) ymin, ymax = map(float, axes.get_ylim()) general = [('Title', axes.get_title()), sep, (None, "<b>X-Axis</b>"), ('Min', xmin), ('Max', xmax), ('Label', axes.get_xlabel()), ('Scale', [axes.get_xscale(), 'linear', 'log']), sep, (None, "<b>Y-Axis</b>"), ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log']), sep, ('(Re-)Generate automatic legend', False), ] # Save the unit data xconverter = axes.xaxis.converter yconverter = axes.yaxis.converter xunits = axes.xaxis.get_units() yunits = axes.yaxis.get_units() if has_curve: # Get / Curves linedict = {} for line in axes.get_lines(): label = line.get_label() if label == '_nolegend_': continue linedict[label] = line curves = [] def prepare_data(d, init): """Prepare entry for FormLayout. `d` is a mapping of shorthands to style names (a single style may have multiple shorthands, in particular the shorthands `None`, `"None"`, `"none"` and `""` are synonyms); `init` is one shorthand of the initial style. This function returns an list suitable for initializing a FormLayout combobox, namely `[initial_name, (shorthand, style_name), (shorthand, style_name), ...]`. """ # Drop duplicate shorthands from dict (by overwriting them during # the dict comprehension). name2short = {name: short for short, name in d.items()} # Convert back to {shorthand: name}. short2name = {short: name for name, short in name2short.items()} # Find the kept shorthand for the style specified by init. canonical_init = name2short[d[init]] # Sort by representation and prepend the initial value. return ([canonical_init] + sorted(short2name.items(), key=lambda short_and_name: short_and_name[1])) curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] color = rgb2hex(colorConverter.to_rgb(line.get_color())) ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor())) fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor())) curvedata = [ ('Label', label), sep, (None, '<b>Line</b>'), ('Line Style', prepare_data(LINESTYLES, line.get_linestyle())), ('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())), ('Width', line.get_linewidth()), ('Color', color), sep, (None, '<b>Marker</b>'), ('Style', prepare_data(MARKERS, line.get_marker())), ('Size', line.get_markersize()), ('Facecolor', fc), ('Edgecolor', ec)] curves.append([curvedata, label, ""]) # make sure that there is at least one displayed curve has_curve = bool(curves) datalist = [(general, "Axes", "")] if has_curve: datalist.append((curves, "Curves", "")) def apply_callback(data): """This function will be called to apply changes""" if has_curve: general, curves = data else: general, = data # Set / General title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, \ generate_legend = general axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_title(title) axes.set_xlim(xmin, xmax) axes.set_xlabel(xlabel) axes.set_ylim(ymin, ymax) axes.set_ylabel(ylabel) # Restore the unit data axes.xaxis.converter = xconverter axes.yaxis.converter = yconverter axes.xaxis.set_units(xunits) axes.yaxis.set_units(yunits) axes.xaxis._update_axisinfo() axes.yaxis._update_axisinfo() if has_curve: # Set / Curves for index, curve in enumerate(curves): line = linedict[curvelabels[index]] label, linestyle, drawstyle, linewidth, color, \ marker, markersize, markerfacecolor, markeredgecolor \ = curve line.set_label(label) line.set_linestyle(linestyle) line.set_drawstyle(drawstyle) line.set_linewidth(linewidth) line.set_color(color) if marker is not 'none': line.set_marker(marker) line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) # re-generate legend, if checkbox is checked if generate_legend: draggable = None ncol = None if axes.legend_ is not None: old_legend = axes.get_legend() draggable = old_legend._draggable is not None ncol = old_legend._ncol new_legend = axes.legend(ncol=ncol) if new_legend: new_legend.draggable(draggable) # Redraw figure = axes.get_figure() figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) if data is not None: apply_callback(data)