From 0bc4c523c0fb0f3d22c9e602286353ea7fbc7395 Mon Sep 17 00:00:00 2001 From: anykraus Date: Wed, 2 Apr 2014 15:03:21 +0200 Subject: [PATCH 1/6] Re-Generate legend, through apply_callback/Apply If there is a legend, it is re-generated when "Apply" is clicked. See Issue #2934: Might have side effects. matplotlib#2934 tacaswell: "However this will clobber any legend that is not the auto-magically generated one so it should probably also get a tick box to enable/disable the updating of the legend." Tick box not included. --- lib/matplotlib/backends/qt4_editor/figureoptions.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/backends/qt4_editor/figureoptions.py b/lib/matplotlib/backends/qt4_editor/figureoptions.py index f9dc2f838cbc..d6d1c96be7da 100644 --- a/lib/matplotlib/backends/qt4_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt4_editor/figureoptions.py @@ -122,6 +122,10 @@ def apply_callback(data): line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) + + # re-generate legend, if there is one. Stefan Kraus/tacaswell 2014-04-02 + if axes.legend_ is not None: + axes.legend() # Redraw figure = axes.get_figure() From 63894f959aad87f6b011bbc8b80450ea64d70735 Mon Sep 17 00:00:00 2001 From: anykraus Date: Tue, 22 Apr 2014 14:19:10 +0200 Subject: [PATCH 2/6] Added a checkbox for (re-)generating auto-legend Checkbox added that controls if an automatic legend is re-generated, or generated for the first time. This checkbox defaults to False, as it generates a simple legend when set to True, see matplotlib.pyplot.legend(), which clobbers any carefully handcrafted legend. --- lib/matplotlib/backends/qt4_editor/figureoptions.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/backends/qt4_editor/figureoptions.py b/lib/matplotlib/backends/qt4_editor/figureoptions.py index d6d1c96be7da..0bde044089eb 100644 --- a/lib/matplotlib/backends/qt4_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt4_editor/figureoptions.py @@ -54,6 +54,8 @@ def figure_edit(axes, parent=None): ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), ('Scale', [axes.get_yscale(), 'linear', 'log']) + sep, + ('(Re-)Generate automatic legend', False) #defaults to False, as it clobbers carefully hand crafted legends /2014-04-22 ] if has_curve: @@ -98,7 +100,7 @@ def apply_callback(data): general, = data # Set / General - title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale = general + title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, generate_legend = general #/2014-04-22 axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_title(title) @@ -123,9 +125,9 @@ def apply_callback(data): line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) - # re-generate legend, if there is one. Stefan Kraus/tacaswell 2014-04-02 - if axes.legend_ is not None: - axes.legend() + # re-generate legend, if checkbox is checked. Stefan Kraus/tacaswell 2014-04-22 + if generate_legend: + new_legend = axes.legend() # Redraw figure = axes.get_figure() From 80beedf6b3c551adad36909405ef4d618d77ca61 Mon Sep 17 00:00:00 2001 From: anykraus Date: Tue, 22 Apr 2014 14:24:38 +0200 Subject: [PATCH 3/6] re-generate legend: re-use: ncol and draggable Copy some properties from the old legend to the new (re-)generated legend. If the old legend was draggable, the new one is now also. The number of columns in the legend is passed on from to old legend to the new legend. --- lib/matplotlib/backends/qt4_editor/figureoptions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/matplotlib/backends/qt4_editor/figureoptions.py b/lib/matplotlib/backends/qt4_editor/figureoptions.py index 0bde044089eb..9e6a116e51a0 100644 --- a/lib/matplotlib/backends/qt4_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt4_editor/figureoptions.py @@ -127,7 +127,10 @@ def apply_callback(data): # re-generate legend, if checkbox is checked. Stefan Kraus/tacaswell 2014-04-22 if generate_legend: + old_legend = axes.get_legend() new_legend = axes.legend() + new_legend._ncol = old_legend._ncol + new_legend.draggable(old_legend._draggable is not None) # Redraw figure = axes.get_figure() From 07930c02dd1801f7eff0b4df6a14a587e3a74112 Mon Sep 17 00:00:00 2001 From: anykraus Date: Tue, 22 Apr 2014 16:44:15 +0200 Subject: [PATCH 4/6] re-generate legend: fixes Fixed crash if there was no previous legend. Fixed ncol inheritance. --- lib/matplotlib/backends/qt4_editor/figureoptions.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/backends/qt4_editor/figureoptions.py b/lib/matplotlib/backends/qt4_editor/figureoptions.py index 9e6a116e51a0..3c71291d3f77 100644 --- a/lib/matplotlib/backends/qt4_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt4_editor/figureoptions.py @@ -127,10 +127,13 @@ def apply_callback(data): # re-generate legend, if checkbox is checked. Stefan Kraus/tacaswell 2014-04-22 if generate_legend: - old_legend = axes.get_legend() - new_legend = axes.legend() - new_legend._ncol = old_legend._ncol - new_legend.draggable(old_legend._draggable is not None) + 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() From af30544731bbce7dc3956a43e3e2e8e74cdfbade Mon Sep 17 00:00:00 2001 From: anykraus Date: Wed, 23 Apr 2014 10:57:08 +0200 Subject: [PATCH 5/6] pep8 compliance Ran pep8.py 1.5.6 (https://github.com/jcrocholl/pep8) over the code. Fixed all complaints. --- .../backends/qt4_editor/figureoptions.py | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/backends/qt4_editor/figureoptions.py b/lib/matplotlib/backends/qt4_editor/figureoptions.py index 3c71291d3f77..2e65e4fdaa7b 100644 --- a/lib/matplotlib/backends/qt4_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt4_editor/figureoptions.py @@ -18,13 +18,13 @@ from matplotlib.backends.qt4_compat import QtGui from matplotlib import markers + def get_icon(name): import matplotlib basedir = osp.join(matplotlib.rcParams['datapath'], 'images') return QtGui.QIcon(osp.join(basedir, name)) -LINESTYLES = { - '-': 'Solid', +LINESTYLES = {'-': 'Solid', '--': 'Dashed', '-.': 'DashDot', ':': 'Dotted', @@ -34,9 +34,10 @@ def get_icon(name): MARKERS = markers.MarkerStyle.markers + def figure_edit(axes, parent=None): """Edit matplotlib figure options""" - sep = (None, None) # separator + sep = (None, None) # separator has_curve = len(axes.get_lines()) > 0 @@ -53,9 +54,9 @@ def figure_edit(axes, parent=None): (None, "Y-Axis"), ('Min', ymin), ('Max', ymax), ('Label', axes.get_ylabel()), - ('Scale', [axes.get_yscale(), 'linear', 'log']) + ('Scale', [axes.get_yscale(), 'linear', 'log']), sep, - ('(Re-)Generate automatic legend', False) #defaults to False, as it clobbers carefully hand crafted legends /2014-04-22 + ('(Re-)Generate automatic legend', False), ] if has_curve: @@ -72,8 +73,7 @@ def figure_edit(axes, parent=None): curvelabels = sorted(linedict.keys()) for label in curvelabels: line = linedict[label] - curvedata = [ - ('Label', label), + curvedata = [('Label', label), sep, (None, 'Line'), ('Style', [line.get_linestyle()] + linestyles), @@ -100,7 +100,8 @@ def apply_callback(data): general, = data # Set / General - title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, generate_legend = general #/2014-04-22 + title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale, \ + generate_legend = general axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_title(title) @@ -114,7 +115,8 @@ def apply_callback(data): for index, curve in enumerate(curves): line = linedict[curvelabels[index]] label, linestyle, linewidth, color, \ - marker, markersize, markerfacecolor, markeredgecolor = curve + marker, markersize, markerfacecolor, markeredgecolor \ + = curve line.set_label(label) line.set_linestyle(linestyle) line.set_linewidth(linewidth) @@ -124,12 +126,12 @@ def apply_callback(data): line.set_markersize(markersize) line.set_markerfacecolor(markerfacecolor) line.set_markeredgecolor(markeredgecolor) - - # re-generate legend, if checkbox is checked. Stefan Kraus/tacaswell 2014-04-22 + + # 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 = axes.legend(ncol=old_legend._ncol) new_legend.draggable(old_legend._draggable is not None) else: new_legend = axes.legend() @@ -140,6 +142,7 @@ def apply_callback(data): figure.canvas.draw() data = formlayout.fedit(datalist, title="Figure options", parent=parent, - icon=get_icon('qt4_editor_options.svg'), apply=apply_callback) + icon=get_icon('qt4_editor_options.svg'), + apply=apply_callback) if data is not None: apply_callback(data) From 9ede5bea702d68500f17fb5fbde841d781e96625 Mon Sep 17 00:00:00 2001 From: anykraus Date: Wed, 23 Apr 2014 13:57:48 +0200 Subject: [PATCH 6/6] figureoptions.py is now pep8 compliant */matplotlib/backends/qt4_editor/figureoptions.py now passes pep8 checks. --- lib/matplotlib/tests/test_coding_standards.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_coding_standards.py b/lib/matplotlib/tests/test_coding_standards.py index ac690c50effd..45d1be620b5d 100644 --- a/lib/matplotlib/tests/test_coding_standards.py +++ b/lib/matplotlib/tests/test_coding_standards.py @@ -134,7 +134,6 @@ '*/matplotlib/backends/qt4_compat.py', '*/matplotlib/backends/tkagg.py', '*/matplotlib/backends/windowing.py', - '*/matplotlib/backends/qt4_editor/figureoptions.py', '*/matplotlib/backends/qt4_editor/formlayout.py', '*/matplotlib/sphinxext/ipython_console_highlighting.py', '*/matplotlib/sphinxext/ipython_directive.py',