def test_get_properties_from_legend_returns_none_if_legend_has_no_entries( self): legend = plt.legend() props = LegendProperties.from_legend(legend) self.assertEqual(props, None)
def remove_selected_curves(self): """ Remove selected curves from figure and curves list. 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 curves from ax and remove from curve names dictionary curves_to_remove_names = self.view.get_selected_curves_names() for name in curves_to_remove_names: remove_curve_from_ax(self.curve_names_dict[name]) self.curve_names_dict.pop(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 list if self.remove_selected_curve_list_entry(): return self.update_view()
def init_view(self): """Sets all of the initial values of the input fields when the tab is first loaded""" if int(matplotlib.__version__[0]) < 2: self.view.hide_box_properties() legend_props = None for ax in self.axes: legend_props = LegendProperties.from_legend(ax.get_legend()) if legend_props: break # This *should* never raise an error because there's a check before this presenter is created that at least one # axes on the plot has a legend with text. This is here just to be on the safe side. assert legend_props, "None of the axes have a non-empty legend." self.check_font_in_list(legend_props.entries_font) self.check_font_in_list(legend_props.title_font) self.view.set_title(legend_props.title) self.view.set_background_color(legend_props.background_color) self.view.set_edge_color(legend_props.edge_color) # Converts alpha value (opacity value between 0 and 1) to transparency percentage. if int(matplotlib.__version__[0]) >= 2: transparency = int(100 - (legend_props.transparency * 100)) self.view.set_transparency_spin_box(transparency) self.view.set_transparency_slider(transparency) self.view.set_entries_font(legend_props.entries_font) self.view.set_entries_size(legend_props.entries_size) self.view.set_entries_color(legend_props.entries_color) self.view.set_title_font(legend_props.title_font) self.view.set_title_size(legend_props.title_size) self.view.set_title_color(legend_props.title_color) self.view.set_marker_size(legend_props.marker_size) self.view.advanced_options.set_border_padding( legend_props.border_padding) self.view.advanced_options.set_column_spacing( legend_props.column_spacing) self.view.advanced_options.set_label_spacing( legend_props.label_spacing) self.view.advanced_options.set_marker_position( legend_props.marker_position) self.view.advanced_options.set_number_of_columns(legend_props.columns) self.view.advanced_options.set_number_of_markers(legend_props.markers) self.view.advanced_options.set_round_edges(legend_props.round_edges) self.view.advanced_options.set_shadow(legend_props.shadow) self.view.advanced_options.set_marker_label_padding( legend_props.marker_label_padding) visible = legend_props.visible self.view.set_hide_legend(not visible) self.hide_legend_ticked(visible) box_visible = legend_props.box_visible self.view.set_hide_box(not box_visible) self.hide_box_ticked(box_visible) self.current_view_properties = legend_props
def get_legend_command_kwargs(legend): """ Returns a list of matplotlib legend kwargs, removing any that are default values. """ kwargs = LegendProperties.from_legend(legend) _remove_kwargs_if_default(kwargs) # Convert the kwargs to the matplotlib ones. return get_mpl_kwargs(kwargs)
def test_from_legend_correctly_returns_properties_if_title_is_none(self): fig, ax = plt.subplots() ax.plot([0, 1, 2], [6, 4, 6]) ax.legend(labels='A', title=None) legend_props = LegendProperties.from_legend(ax.legend_) self.assertEqual(legend_props['title'], '')
def generate_visible_command(legend, legend_object_var): """ Returns a command to set the visibility of the legend if it's different to the default value. It's returned as a list for convenience, so it can be added to the end of a list without checking if it's empty. """ visible_command = [] kwargs = LegendProperties.from_legend(legend) _remove_kwargs_if_default(kwargs) if 'visible' in kwargs: visible_command.append(legend_object_var + ".set_visible(" + str(kwargs['visible']) + ")") return visible_command
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_) waterfall = False if isinstance(ax, MantidAxes): waterfall = ax.is_waterfall() if waterfall: # Waterfall plots are reset so they can be reconverted after the curve is removed. x, y = ax.waterfall_x_offset, ax.waterfall_y_offset ax.update_waterfall(0, 0) # If the curves have a fill, the one which corresponds to the curve being removed also needs to be removed. current_curve_index = self.view.select_curve_combo_box.currentIndex( ) i = 0 for collection in ax.collections: if isinstance(collection, PolyCollection): if current_curve_index == i: ax.collections.remove(collection) break i = i + 1 # 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() # If there is now only one curve on a waterfall plot, the plot becomes non-waterfall. if waterfall: ax.update_waterfall(x, y) if len(ax.get_lines()) <= 1: ax.set_waterfall(False) 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 init_view(self): if int(matplotlib.__version__[0]) < 2: self.view.hide_box_properties() """Sets all of the initial values of the input fields when the tab is first loaded""" legend_props = LegendProperties.from_legend(self.axes[0].get_legend()) self.check_font_in_list(legend_props.entries_font) self.check_font_in_list(legend_props.title_font) self.view.set_title(legend_props.title) self.view.set_background_color(legend_props.background_color) self.view.set_edge_color(legend_props.edge_color) # Converts alpha value (opacity value between 0 and 1) to transparency percentage. if int(matplotlib.__version__[0]) >= 2: transparency = int(100 - (legend_props.transparency * 100)) self.view.set_transparency_spin_box(transparency) self.view.set_transparency_slider(transparency) self.view.set_entries_font(legend_props.entries_font) self.view.set_entries_size(legend_props.entries_size) self.view.set_entries_color(legend_props.entries_color) self.view.set_title_font(legend_props.title_font) self.view.set_title_size(legend_props.title_size) self.view.set_title_color(legend_props.title_color) self.view.set_marker_size(legend_props.marker_size) self.view.advanced_options.set_border_padding( legend_props.border_padding) self.view.advanced_options.set_column_spacing( legend_props.column_spacing) self.view.advanced_options.set_label_spacing( legend_props.label_spacing) self.view.advanced_options.set_marker_position( legend_props.marker_position) self.view.advanced_options.set_number_of_columns(legend_props.columns) self.view.advanced_options.set_number_of_markers(legend_props.markers) self.view.advanced_options.set_round_edges(legend_props.round_edges) self.view.advanced_options.set_shadow(legend_props.shadow) self.view.advanced_options.set_marker_label_padding( legend_props.marker_label_padding) visible = legend_props.visible self.view.set_hide_legend(not visible) self.hide_legend_ticked(visible) box_visible = legend_props.box_visible self.view.set_hide_box(not box_visible) self.hide_box_ticked(box_visible) self.current_view_properties = legend_props
def generate_label_font_commands(legend, legend_object_var): """ Generate python commands for setting the legend text label properties. The size is not present here because it is already included in the list of legend properties. """ label_commands = [] kwargs = LegendProperties.from_legend(legend) _remove_kwargs_if_default(kwargs) if 'entries_font' in kwargs: label_commands.append("[label.set_fontname('" + kwargs['entries_font'] + "') for label in " + legend_object_var + ".get_texts()]") if 'entries_color' in kwargs: label_commands.append("[label.set_color('" + kwargs['entries_color'] + "') for label in " + legend_object_var + ".get_texts()]") return label_commands
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 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 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 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 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