def on_button_press(self, event): if not event.dblclick: # shortcut return # We assume this is used for editing axis information e.g. labels # which are outside of the axes so event.inaxes is no use. canvas = self.canvas figure = canvas.figure axes = figure.get_axes() def move_and_show(editor): editor.move(event.x, figure.bbox.height - event.y + self.window.y()) editor.exec_() for ax in axes: if ax.title.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.title)) elif ax.xaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.xaxis.label)) elif ax.yaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.yaxis.label)) elif ax.xaxis.contains(event)[0]: move_and_show(XAxisEditor(canvas, ax)) elif ax.yaxis.contains(event)[0]: move_and_show(YAxisEditor(canvas, ax))
def _show_axis_editor(self, event): # We assume this is used for editing axis information e.g. labels # which are outside of the axes so event.inaxes is no use. canvas = self.canvas figure = canvas.figure axes = figure.get_axes() def move_and_show(editor): editor.move(QCursor.pos()) editor.exec_() for ax in axes: if ax.title.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.title)) elif ax.xaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.xaxis.label)) elif ax.yaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.yaxis.label)) elif (ax.xaxis.contains(event)[0] or any( tick.contains(event)[0] for tick in ax.get_xticklabels())): move_and_show(XAxisEditor(canvas, ax)) elif (ax.yaxis.contains(event)[0] or any( tick.contains(event)[0] for tick in ax.get_yticklabels())): if type(ax) == Axes: move_and_show(ColorbarAxisEditor(canvas, ax)) else: move_and_show(YAxisEditor(canvas, ax)) elif hasattr(ax, 'zaxis'): if ax.zaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.zaxis.label)) elif (ax.zaxis.contains(event)[0] or any( tick.contains(event)[0] for tick in ax.get_zticklabels())): move_and_show(ZAxisEditor(canvas, ax))
def test_axis_editor_initialised_with_correct_values_3d(self): a = np.array([[1]]) fig, ax = plt.subplots(subplot_kw={'projection': 'mantid3d'}) ax.plot_surface(a, a, a) # Set properties that can be accessed via the axes menu ax.set_xlim(1, 2) ax.set_ylim(3, 4) ax.set_zlim(5, 6) ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=True)) ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=True)) ax.zaxis.set_major_formatter(LogFormatterSciNotation()) # Create axis editors for each axis x_editor = XAxisEditor(fig.canvas, ax) y_editor = YAxisEditor(fig.canvas, ax) z_editor = ZAxisEditor(fig.canvas, ax) # Check that the correct axis is assigned self.assertEqual(x_editor.axis, ax.xaxis) self.assertEqual(y_editor.axis, ax.yaxis) self.assertEqual(z_editor.axis, ax.zaxis) # Test tick formats self.assertEqual(x_editor._memento.formatter, DECIMAL_FORMAT) self.assertEqual(y_editor._memento.formatter, DECIMAL_FORMAT) self.assertEqual(z_editor._memento.formatter, SCIENTIFIC_FORMAT) # Test limits self.assertEqual(x_editor._memento.min, ax.get_xlim()[0]) self.assertEqual(x_editor._memento.max, ax.get_xlim()[1]) self.assertEqual(y_editor._memento.min, ax.get_ylim()[0]) self.assertEqual(y_editor._memento.max, ax.get_ylim()[1]) self.assertEqual(z_editor._memento.min, ax.get_zlim()[0]) self.assertEqual(z_editor._memento.max, ax.get_zlim()[1])
def test_axis_editor_initialised_with_correct_values(self): # make figure fig, ax = plt.subplots(1, 1) ax.plot([1, 2, 3], [1, 10, 100], 'o') # set properties that can be accessed via the axes menu ax.xaxis.grid(True) ax.set(xlim=[0, 4], ylim=[1e-3, 1e3], yscale='log') # get an AxisEditor object for x/y axes xEditor = XAxisEditor(fig.canvas, ax) yEditor = YAxisEditor(fig.canvas, ax) # test grid visibility self.assertEqual(xEditor._memento.grid, ax.xaxis._major_tick_kw['gridOn']) self.assertEqual(yEditor._memento.grid, ax.yaxis._major_tick_kw['gridOn']) # test limits self.assertEqual(xEditor._memento.min, ax.get_xlim()[0]) self.assertEqual(xEditor._memento.max, ax.get_xlim()[1]) self.assertEqual(yEditor._memento.min, ax.get_ylim()[0]) self.assertEqual(yEditor._memento.max, ax.get_ylim()[1]) # test format self.assertEqual(xEditor._memento.formatter, 'Decimal Format') self.assertEqual(yEditor._memento.formatter, 'Scientific Format') # test scale self.assertEqual(xEditor._memento.log, False) self.assertEqual(yEditor._memento.log, True)
def _show_axis_editor(self, event): # We assume this is used for editing axis information e.g. labels # which are outside of the axes so event.inaxes is no use. canvas = self.canvas figure = canvas.figure axes = figure.get_axes() def move_and_show(editor): editor.move(QCursor.pos()) editor.exec_() for ax in axes: if ax.title.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.title)) elif ax.xaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.xaxis.label)) elif ax.yaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.yaxis.label)) elif (ax.xaxis.contains(event)[0] or any(tick.contains(event)[0] for tick in ax.get_xticklabels())): move_and_show(XAxisEditor(canvas, ax)) elif (ax.yaxis.contains(event)[0] or any(tick.contains(event)[0] for tick in ax.get_yticklabels())): if type(ax) == Axes: move_and_show(ColorbarAxisEditor(canvas, ax)) else: move_and_show(YAxisEditor(canvas, ax)) elif hasattr(ax, 'zaxis'): if ax.zaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.zaxis.label)) elif (ax.zaxis.contains(event)[0] or any(tick.contains(event)[0] for tick in ax.get_zticklabels())): move_and_show(ZAxisEditor(canvas, ax)) elif ax.get_legend() is not None and ax.get_legend().contains(event)[0]: # We have to set the legend as non draggable else we hold onto the legend # until the mouse button is clicked again ax.get_legend().set_draggable(False) legend_texts = ax.get_legend().get_texts() active_lines = datafunctions.get_legend_handles(ax) for legend_text, curve in zip(legend_texts, active_lines): if legend_text.contains(event)[0]: move_and_show(LegendEditor(canvas, legend_text, curve)) ax.get_legend().set_draggable(True)
def test_grid_option_correct_when_grid_set_with_kwargs(self): """ Plot script generator toggles grid lines using axes.tick_params. Reproduce that here to check whether the grid line visibility is being picked up correctly by the axis editor dialog. """ # make figure fig, ax = plt.subplots(1, 1) ax.plot([1, 2, 3], [1, 10, 100], 'o') # set properties that can be accessed via the axes menu ax.tick_params(axis='x', which='major', **{ 'gridOn': True, 'tick1On': True, 'tick2On': False, 'label1On': True, 'label2On': False, 'size': 6, 'tickdir': 'out', 'width': 1 }) ax.tick_params(axis='y', which='major', **{ 'gridOn': True, 'tick1On': True, 'tick2On': False, 'label1On': True, 'label2On': False, 'size': 6, 'tickdir': 'out', 'width': 1 }) # get an AxisEditor object for x/y axes x_editor = XAxisEditor(fig.canvas, ax) y_editor = YAxisEditor(fig.canvas, ax) self.assertTrue(x_editor._memento.grid) self.assertTrue(y_editor._memento.grid)
def _show_axis_editor(self, event): """ Decides whether to show a dialog to edit axis information based on the contents of the event. Shows a dialog if necessary. @param event: the object representing the event @return: a flag to denote whether an action was taken e.g. opening a dialog. """ # We assume this is used for editing axis information e.g. labels # which are outside of the axes so event.inaxes is no use. canvas = self.canvas figure = canvas.figure axes = figure.get_axes() action_taken = False def move_and_show(editor): nonlocal action_taken action_taken = True editor.move(QCursor.pos()) editor.exec_() for ax in axes: if ax.title.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.title)) elif ax.xaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.xaxis.label)) elif ax.yaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.yaxis.label)) elif (ax.xaxis.contains(event)[0] or any( tick.contains(event)[0] for tick in ax.get_xticklabels())): move_and_show(XAxisEditor(canvas, ax)) elif (ax.yaxis.contains(event)[0] or any( tick.contains(event)[0] for tick in ax.get_yticklabels())): if type(ax) == Axes: move_and_show(ColorbarAxisEditor(canvas, ax)) else: move_and_show(YAxisEditor(canvas, ax)) elif hasattr(ax, 'zaxis'): if ax.zaxis.label.contains(event)[0]: move_and_show(LabelEditor(canvas, ax.zaxis.label)) elif (ax.zaxis.contains(event)[0] or any( tick.contains(event)[0] for tick in ax.get_zticklabels())): move_and_show(ZAxisEditor(canvas, ax)) elif ax.get_legend() is not None and ax.get_legend().contains( event)[0]: # We have to set the legend as non draggable else we hold onto the legend # until the mouse button is clicked again legend_set_draggable(ax.get_legend(), False) legend_texts = ax.get_legend().get_texts() active_lines = datafunctions.get_legend_handles(ax) remove_legend_flag = True # remove the legend if no curve texts were clicked for legend_text, curve in zip(legend_texts, active_lines): if legend_text.contains(event)[0]: remove_legend_flag = False move_and_show(LegendEditor(canvas, legend_text, curve)) legend_set_draggable(ax.get_legend(), True) if remove_legend_flag: action_taken = True legend = ax.get_legend() legend.set_visible(False) canvas.draw() return action_taken