def set_up_color_selector_toolbar_button(self, fig): # check if the action is already in the toolbar if self._actions.get('line_colour'): return a = self.addAction(get_icon('mdi.palette'), "Line Colour", lambda: None) self._actions['line_colour'] = a if figure_type(fig) == FigureType.Wireframe: a.setToolTip("Set the colour of the wireframe.") else: a.setToolTip("Set the colour of the contour lines.") line_collection = next(col for col in fig.get_axes()[0].collections if isinstance(col, LineCollection)) initial_colour = convert_color_to_hex(line_collection.get_color()[0]) colour_dialog = QtWidgets.QColorDialog(QtGui.QColor(initial_colour)) colour_dialog.setOption(QtWidgets.QColorDialog.NoButtons) colour_dialog.setOption(QtWidgets.QColorDialog.DontUseNativeDialog) colour_dialog.currentColorChanged.connect(self.change_line_collection_colour) button = [child for child in self.children() if isinstance(child, QtWidgets.QToolButton)][-1] menu = QtWidgets.QMenu("Menu", parent=button) colour_selector_action = QtWidgets.QWidgetAction(menu) colour_selector_action.setDefaultWidget(colour_dialog) menu.addAction(colour_selector_action) button.setMenu(menu) button.setPopupMode(QtWidgets.QToolButton.InstantPopup)
def waterfall_update_fill(ax): # Get the colours of each fill so they can be reapplied after updating. colours = [] for collection in ax.collections: if isinstance(collection, PolyCollection): colours.append(collection.get_facecolor()) waterfall_remove_fill(ax) waterfall_create_fill(ax) poly_collections = get_waterfall_fills(ax) line_colours = True # If there are more fill areas than colours, this means that new curves have been added to the plot # (overplotting). In which case, we need to determine whether the fill colours are set to match the line # colours by checking that the colour of each fill that existed previously is the same as the line it belongs # to. If so, the list of colours is appended to with the colours of the new lines. Otherwise the fills are # all set to the same colour and so the list of colours is extended with the same colour for each new curve. if len(poly_collections) > len(colours): for i in range(len(colours) - 1): if convert_color_to_hex( colours[i][0]) != ax.get_lines()[i].get_color(): line_colours = False break colours_length = len(colours) if line_colours: for i in range(colours_length, len(poly_collections)): colours.append(ax.get_lines()[i].get_color()) else: for i in range(colours_length, len(poly_collections)): colours.append(colours[0]) for i, collection in enumerate(poly_collections): collection.set_color(colours[i])
def waterfall_fill_is_line_colour(ax): i = 0 # Check that for each line, the fill area is the same colour as the line. for collection in ax.collections: if isinstance(collection, PolyCollection): line_colour = ax.get_lines()[i].get_color() poly_colour = convert_color_to_hex(collection.get_facecolor()[0]) if line_colour != poly_colour: return False i += 1 return True
def test_toggle_normalisation_on_contour_plot_maintains_contour_line_colour(self): from mantid.plots.legend import convert_color_to_hex ws = CreateWorkspace(DataX=[1, 2, 3, 4, 2, 4, 6, 8], DataY=[2] * 8, NSpec=2, OutputWorkspace="test_ws") fig = plot_contour([ws]) for col in fig.get_axes()[0].collections: col.set_color("#ff9900") mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) fig_interactor._toggle_normalization(fig.axes[0]) self.assertTrue(all(convert_color_to_hex(col.get_color()[0]) == "#ff9900" for col in fig.get_axes()[0].collections))
def test_overplotting_onto_waterfall_plot_with_line_colour_fills_adds_another_filled_area_with_new_line_colour(self): fig, ax = plt.subplots(subplot_kw={'projection': 'mantid'}) ax.plot([0, 1], [0, 1], color="#ff9900") ax.plot([0, 1], [0, 1], color="#00d1ff") # Make a waterfall plot. ax.set_waterfall(True) # Add filled areas. ax.set_waterfall_fill(True) # Set the fills to be the same colour as their lines. ax.collections[0].set_facecolor(ax.lines[0].get_color()) ax.collections[1].set_facecolor(ax.lines[0].get_color()) # Plot another line and make it join the waterfall. ax.plot([0, 1], [0,1], color='#00fff0') datafunctions.convert_single_line_to_waterfall(ax, 2) datafunctions.waterfall_update_fill(ax) # Check that there are now three filled areas and the new line colour matches the new fill colour. self.assertEqual(convert_color_to_hex(ax.collections[2].get_facecolor()[0]), ax.lines[2].get_color())
def set_color(self, color_hex): self.line_edit.setText(convert_color_to_hex(color_hex))
# Mantid Repository : https://github.com/mantidproject/mantid # # Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source, # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantid workbench. from mantid.plots.legend import convert_color_to_hex from matplotlib import rcParams from qtpy.QtCore import QRegExp from qtpy.QtGui import QColor, QRegExpValidator from qtpy.QtWidgets import (QWidget, QLineEdit, QPushButton, QHBoxLayout, QColorDialog) MPL_DEFAULT = convert_color_to_hex(rcParams['lines.color']) class ColorSelector(QWidget): def __init__(self, initial_color=MPL_DEFAULT, parent=None): super(ColorSelector, self).__init__(parent=parent) self.initial_color = QColor(initial_color) # Create line edit and push button and add to a horizontal layout self.line_edit = QLineEdit(self) self.button = QPushButton(self) self.h_layout = QHBoxLayout(self) self.h_layout.addWidget(self.line_edit) self.h_layout.addWidget(self.button) self.h_layout.setContentsMargins(0, 0, 0, 0)
# Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source, # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantid workbench. from matplotlib import rcParams from matplotlib.font_manager import FontProperties from mantid.plots.legend import LegendProperties, convert_color_to_hex from workbench.plotting.plotscriptgenerator.utils import convert_args_to_string # Default values of all options that are accessible via the legend tab in the plot settings. mpl_default_kwargs = { 'visible': True, 'title': '', 'background_color': convert_color_to_hex(rcParams['axes.facecolor']), # inherits from axes by default 'edge_color': convert_color_to_hex(rcParams['legend.edgecolor']), 'transparency': rcParams['legend.framealpha'], 'entries_font': 'DejaVu Sans', 'entries_size': rcParams['legend.fontsize'], 'entries_color': '#000000', 'title_font': 'DejaVu Sans', 'title_size': rcParams['axes.labelsize'], # Uses axes size by default 'title_color': '#000000', 'marker_size': rcParams['legend.handlelength'], 'box_visible': rcParams['legend.frameon'], 'shadow': rcParams['legend.shadow'], 'round_edges': rcParams['legend.fancybox'], 'columns': 1, 'column_spacing': rcParams['legend.columnspacing'], 'label_spacing': rcParams['legend.labelspacing'],