def get_dict_for_axes(self, ax): ax_dict = { "properties": self.get_dict_from_axes_properties(ax), "title": ax.get_title(), "xAxisTitle": ax.get_xlabel(), "yAxisTitle": ax.get_ylabel(), "colorbar": self.get_dict_for_axes_colorbar(ax) } # Get lines from the axes and store it's data lines_list = [] for index, line in enumerate(ax.lines): lines_list.append(self.get_dict_from_line(line, index)) ax_dict["lines"] = lines_list texts_list = [] for text in ax.texts: texts_list.append(self.get_dict_from_text(text)) ax_dict["texts"] = texts_list # Potentially need to handle artists that are Text artist_text_dict = {} for artist in ax.artists: if isinstance(artist, matplotlib.text.Text): artist_text_dict = self.get_dict_from_text(artist) ax_dict["textFromArtists"] = artist_text_dict legend_dict = {} legend = ax.get_legend() if legend is not None: legend_dict["exists"] = True legend_dict.update(LegendProperties.from_legend(legend)) else: legend_dict["exists"] = False ax_dict["legend"] = legend_dict # add value to determine if ax has been normalised ws_artists = [art for art in ax.tracked_workspaces.values()] is_norm = all(art[0].is_normalized for art in ws_artists) ax_dict["_is_norm"] = is_norm return ax_dict
def apply_properties(self): """Take properties from views and set them on the selected curve""" ax = self.get_selected_ax() if ax.legend_: self.legend_props = LegendProperties.from_legend(ax.legend_) view_props = self.get_view_properties() if view_props == self.current_view_properties: return plot_kwargs = view_props.get_plot_kwargs() # Re-plot curve self._replot_current_curve(plot_kwargs) curve = self.get_current_curve() # Set the curve's new name in the names dict and combo box self.set_new_curve_name_in_dict_and_list(curve, view_props.label) FigureErrorsManager.toggle_errors(curve, view_props) self.current_view_properties = view_props FigureErrorsManager.update_limits_and_legend(ax, self.legend_props)
def generate_title_font_commands(legend, legend_object_var): """ Generate commands for setting properties for the legend title font. """ title_commands = [] kwargs = LegendProperties.from_legend(legend) _remove_kwargs_if_default(kwargs) if 'title_font' in kwargs: title_commands.append(legend_object_var + ".get_title().set_fontname('" + kwargs['title_font'] + "')") if 'title_color' in kwargs: title_commands.append(legend_object_var + ".get_title().set_color('" + kwargs['title_color'] + "')") if 'title_size' in kwargs: title_commands.append(legend_object_var + ".get_title().set_fontsize('" + str(kwargs['title_size']) + "')") return title_commands
def setUpClass(cls): # Mock axes tab view mock_axes_view = Mock( get_selected_ax_name=lambda: '(0, 0)', get_properties=lambda: AxProperties(new_ax_view_props)) cls.ax_view_patch = patch(AX_VIEW, lambda x: mock_axes_view) cls.ax_view_mock = cls.ax_view_patch.start() # Mock curves tab view cls.curve_view_mock = Mock( get_selected_ax_name=lambda: '(0, 0)', select_curve_list=Mock(selectedItems=lambda: []), get_properties=lambda: CurveProperties(new_curve_view_props)) cls.curve_view_patch = patch(CURVE_VIEW, lambda x: cls.curve_view_mock) cls.curve_view_patch.start() cls.ax = _run_apply_properties_on_figure_with_curve( cls.curve_view_mock) cls.new_curve = cls.ax.containers[0] # Mock images tab view cls.img_view_mock = Mock( get_selected_image_name=lambda: '(0, 0) - image0', get_properties=lambda: ImageProperties(new_image_props)) cls.img_view_patch = patch(IMAGE_VIEW, lambda x: cls.img_view_mock) cls.img_view_patch.start() cls.img_ax = _run_apply_properties_on_figure_with_image() cls.new_img = cls.img_ax.images[0] # Mock legend tab view cls.legend_view_mock = Mock( get_properties=lambda: LegendProperties(new_legend_props)) cls.legend_view_patch = patch(LEGEND_VIEW, lambda x: cls.legend_view_mock) cls.legend_view_patch.start() cls.legend_ax = _run_apply_properties_on_figure_with_legend( cls.curve_view_mock) cls.new_legend = cls.legend_ax.get_legend()
def toggle_error_bars_for(cls, ax, curve, make_visible=None): # get legend properties if ax.legend_: legend_props = LegendProperties.from_legend(ax.legend_) else: legend_props = None if isinstance(curve, Line2D): curve_index = ax.get_lines().index(curve) else: curve_index = ax.get_lines().index(curve[0]) # get all curve properties curve_props = CurveProperties.from_curve(curve) # and remove the ones that matplotlib doesn't recognise plot_kwargs = curve_props.get_plot_kwargs() new_curve = cls.replot_curve(ax, curve, plot_kwargs) if isinstance(ax, MantidAxes): errorbar_cap_lines = datafunctions.remove_and_return_errorbar_cap_lines( ax) else: errorbar_cap_lines = [] ax.lines.insert(curve_index, ax.lines.pop()) if isinstance(ax, MantidAxes) and ax.is_waterfall(): datafunctions.convert_single_line_to_waterfall(ax, curve_index) for cap in errorbar_cap_lines: ax.add_line(cap) # Inverts either the current state of hide_errors # or the make_visible kwarg that forces a state: # If make visible is True, then hide_errors must be False # for the intended effect curve_props.hide_errors = not curve_props.hide_errors if make_visible is None else not make_visible cls.toggle_errors(new_curve, curve_props) cls.update_limits_and_legend(ax, legend_props)
def remove_selected_curve(self): """ Remove selected curve from figure and combobox. If there are no curves left on the axes remove that axes from the axes combo box """ ax = self.get_selected_ax() if ax.legend_: self.legend_props = LegendProperties.from_legend(ax.legend_) # Remove curve from ax and remove from curve names dictionary remove_curve_from_ax(self.get_selected_curve()) self.curve_names_dict.pop(self.view.get_selected_curve_name()) self.set_apply_to_all_buttons_enabled() ax = self.get_selected_ax() # Update the legend and redraw FigureErrorsManager.update_limits_and_legend(ax, self.legend_props) ax.figure.canvas.draw() # Remove the curve from the curve selection combo box if self.remove_selected_curve_combo_box_entry(): return self.update_view()
def get_dict_for_axes(self, ax): ax_dict = { "properties": self.get_dict_from_axes_properties(ax), "title": ax.get_title(), "xAxisTitle": ax.get_xlabel(), "yAxisTitle": ax.get_ylabel(), "colorbar": self.get_dict_for_axes_colorbar(ax) } # Get lines from the axes and store it's data lines_list = [] for index, line in enumerate(ax.lines): lines_list.append(self.get_dict_from_line(line, index)) ax_dict["lines"] = lines_list texts_list = [] for text in ax.texts: texts_list.append(self.get_dict_from_text(text)) ax_dict["texts"] = texts_list # Potentially need to handle artists that are Text artist_text_dict = {} for artist in ax.artists: if isinstance(artist, matplotlib.text.Text): artist_text_dict = self.get_dict_from_text(artist) ax_dict["textFromArtists"] = artist_text_dict legend_dict = {} legend = ax.get_legend() if legend is not None: legend_dict["exists"] = True legend_dict.update(LegendProperties.from_legend(legend)) else: legend_dict["exists"] = False ax_dict["legend"] = legend_dict return ax_dict
def update_legend(ax, legend): if not legend["exists"] and ax.get_legend(): ax.get_legend().remove() return if legend["exists"]: LegendProperties.create_legend(legend, ax)
def get_properties(self): props = LegendProperties.from_view(self) advanced_props = self.advanced_options.get_properties() props.update(advanced_props) return props
def update_limits_and_legend(ax, legend_props=None): ax.relim() ax.autoscale() if legend_props: LegendProperties.create_legend(legend_props, ax)
def test_calling_create_legend_with_no_props_adds_legend_to_plot(self): ax = plt.gca() LegendProperties.create_legend(props=None, ax=ax) self.assertNotEqual(ax.get_legend(), None)
def get_properties(self): return LegendProperties.from_view_advanced(self)