Colormaps¶
The colormap module contains definition of common colormaps and tools to manipulate and create them.
Colormap widget¶
This module provides a basic widget to edit a colormap that contains a multi-slider and a colorap representation.
Reference¶
- class plotpy.widgets.colormap.widget.EditableColormap(*args, name: str | None = None)[source]¶
Overload of the QwtLinearColorMap class to add some features. This class is temporary and should be removed when its features are added to QwtPython.
- Parameters:
*args – QwtLinearColorMap arguments
name – Optional str name given to the colormap. Useful for the interactions with the rest of PlotPy, notably with the gobal colormaps dictionaries such as colormaps.ALL_COLORMAPS. If None, the colormap name will be set to “temporary”. Defaults to None.
- rgb(interval: QwtInterval, value: float) int [source]¶
Returns the color of the colormap at the given value. This overriden method is used to add the possibility to invert the colormap colors.
- Parameters:
interval – QwtInterval of the colormap
value – value to get the color from
- Returns:
Color of the colormap at the given value.
- property color_stop_values: list[float]¶
Returns the position values of the color stops.
- Returns:
List of color stop positions.
- is_boundary_stop_index(stop_index: int) bool [source]¶
Checks if the given index is a boundary index (first or last).
- Parameters:
stop_index – stop color index to check
- Returns:
True if the index is a boundary index, False otherwise.
- update_stops_steps(stop_index: int) None [source]¶
Updates the steps of the previous and next color stops of the given index. The steps are the distance between the current stop and the next color stop. This method can update the given color stop index, the previous one and the next stop if necessary.
- Parameters:
stop_index – current color stop index to update.
- classmethod from_iterable(iterable: Sequence[tuple[float, str]], name=None) EditableColormap [source]¶
Converts the given iterable of tuples to a colormap (instance of Self). The iterable must be at least of length 2. If the iterable is of length 1, the colormap will be composed of two identical colors. If the iterable is empty, the colormap will be composed of two default colors (blue and yellow).
- Parameters:
iterable – Iterable of tuples composed of a float position and a hex color
string.
name – Name of the new colormap instance. Defaults to None.
- Returns:
New CustomQwtLinearColormap instance.
- to_tuples() tuple[tuple[float, str], ...] [source]¶
Converts a colormap to a tuple of tuples composed of a float position and a hex color string.
- Returns:
Tuple of tuples composed of a float position and a hex color string.
- move_color_stop(stop_index: int, new_pos: float, new_color: QColor | None = None) None [source]¶
Moves a color stop to a new position and updates the steps of the previous and next color stops if necessary. Mutates the colormap object!
- Parameters:
stop_index – color stop index to move
new_pos – new color stop position
new_color – new color stop color. If None, will use the current stop color. Defaults to None.
- class plotpy.widgets.colormap.widget.ColorMapWidget(parent: QWidget | None, cmap_width: int = 400, cmap_height: int = 50, color1: QColor | None = None, color2: QColor | None = None, colormap: EditableColormap | None = None)[source]¶
Simple colormap widget containing a horizontal slider and a colorbar image. The slider is used to edit the colormap that changes in real time. It is possible to add/remove handles by right-clicking on the colorbar image. This widget does not provide a way to change a handle color. For this, use the ColorMapEditor widget or the ColorMapManager widget.
- Parameters:
parent – parent widget
cmap_width – minimum width of the widget. Defaults to 400.
cmap_height – minimum height of the colorbar. Defaults to 50.
color1 – first color. Ignored if the ‘colormap’ argument is used. If None, default color is blue. Defaults to None.
color2 – second color. Ignored if the ‘colormap’ argument is used. If None, default color is yellow. Defaults to None.
colormap – colormap instance. If None, color1 and color2 will be used to create a new colormap. Defaults to None.
- set_colormap(colormap: EditableColormap) None [source]¶
Replaces the current colormap.
- Parameters:
colormap – replacement colormap
- get_colormap() EditableColormap [source]¶
Get the current colormap being edited.
- Returns:
current colormap
- emit_handle_selected() None [source]¶
When called, computes a slider position from the current position of the mouse and maps it to slider range. Then finds which handles was selected and emits the custom signal HANDLE_SELECTED(handle_index) and caches the index.
- get_color_from_value(value: float, as_type: type[~plotpy.widgets.colormap.widget.ColorTypeT] = <class 'PyQt5.QtGui.QColor'>) ColorTypeT [source]¶
Returns the color assigned to a given value using the current colormap.
- Parameters:
value – value to assign a color to
as_type – Color type to be returned (int or QG.QColor). Defaults to
QG.QColor.
- Returns:
The assigned color for the input value using the current colormap.
- get_handle_color(handle_index: int, as_type: type[~plotpy.widgets.colormap.widget.ColorTypeT] = <class 'PyQt5.QtGui.QColor'>) ColorTypeT [source]¶
Returns the color of a handled using its index. Directly return the color of the ColorStop object of the given index.
- Parameters:
handle_index – index of the handled to assign a color too.
as_type – Color type to be returned (int or QG.QColor). Defaults to
QG.QColor.
- Returns:
The assigned color for the input handle index using the current colormap.
- get_hex_color(handle_index: int) str [source]¶
Same as self.get_handle_color but returns the color as a hex color string.
- Parameters:
handle_index – index of the handled to assign a color too.
- Returns:
Handle’s hex color string
- edit_color_stop(index: int, new_pos: float | None = None, new_color: QColor | int | None = None) None [source]¶
Edit an existing color stop in the current colormap. Mutates the colormap object. Also edits the slider handle position.
- Parameters:
index – color stop index to mutate
new_pos – new color stop position. If not set will remain the same. Defaults to None.
new_color – new color. If not set will remain the same. Defaults to None.
Setups the contextual menu used to insert and delete handles.
- Returns:
Colorbar contextual menu
- add_handle_at_relative_pos(relative_pos: float, new_color: QColor | int | None = None) None [source]¶
insert a handle in the widget at the relative position (between 0. and 1.). Mutates the colormap object. If the relative position is already occupied by a handle, the new handle will be inserted at the closest available position then will be moved back to the requested position.
- Parameters:
relative_pos – insertion position
- get_handles_list() list[float] [source]¶
Return the current handles as a mutable list. If the data doesn’t need to be mutated, prefer the method self.get_handles_tuple() that directly returns the handles tuple without making a conversion to a list.
- Returns:
Mutable list of handles position
- get_handles_tuple() tuple[float, ...] [source]¶
Passthrough to get the tuple of handles position.
- Returns:
Immutable tuple of handles position
- set_handles_values(values: Sequence[float]) None [source]¶
Passthrough to set handles position.
- Parameters:
values – sequence of values with a minimum length of 2 (min and max).
Opens the contextual menu at input position.
- Parameters:
pos – contextual menu position
- class plotpy.widgets.colormap.widget.ColorTypeT¶
Type variable for color types
- plotpy.widgets.colormap.widget.QColorInitTypes¶
Type for color initialization types
alias of
QColor
|int
|Tuple
[int
,int
,int
,int
] |str
|GlobalColor
|None
- class plotpy.widgets.colormap.widget.EditableColormap(*args, name: str | None = None)[source]¶
Overload of the QwtLinearColorMap class to add some features. This class is temporary and should be removed when its features are added to QwtPython.
- Parameters:
*args – QwtLinearColorMap arguments
name – Optional str name given to the colormap. Useful for the interactions with the rest of PlotPy, notably with the gobal colormaps dictionaries such as colormaps.ALL_COLORMAPS. If None, the colormap name will be set to “temporary”. Defaults to None.
- rgb(interval: QwtInterval, value: float) int [source]¶
Returns the color of the colormap at the given value. This overriden method is used to add the possibility to invert the colormap colors.
- Parameters:
interval – QwtInterval of the colormap
value – value to get the color from
- Returns:
Color of the colormap at the given value.
- property color_stop_values: list[float]¶
Returns the position values of the color stops.
- Returns:
List of color stop positions.
- is_boundary_stop_index(stop_index: int) bool [source]¶
Checks if the given index is a boundary index (first or last).
- Parameters:
stop_index – stop color index to check
- Returns:
True if the index is a boundary index, False otherwise.
- update_stops_steps(stop_index: int) None [source]¶
Updates the steps of the previous and next color stops of the given index. The steps are the distance between the current stop and the next color stop. This method can update the given color stop index, the previous one and the next stop if necessary.
- Parameters:
stop_index – current color stop index to update.
- classmethod from_iterable(iterable: Sequence[tuple[float, str]], name=None) EditableColormap [source]¶
Converts the given iterable of tuples to a colormap (instance of Self). The iterable must be at least of length 2. If the iterable is of length 1, the colormap will be composed of two identical colors. If the iterable is empty, the colormap will be composed of two default colors (blue and yellow).
- Parameters:
iterable – Iterable of tuples composed of a float position and a hex color
string.
name – Name of the new colormap instance. Defaults to None.
- Returns:
New CustomQwtLinearColormap instance.
- to_tuples() tuple[tuple[float, str], ...] [source]¶
Converts a colormap to a tuple of tuples composed of a float position and a hex color string.
- Returns:
Tuple of tuples composed of a float position and a hex color string.
- move_color_stop(stop_index: int, new_pos: float, new_color: QColor | None = None) None [source]¶
Moves a color stop to a new position and updates the steps of the previous and next color stops if necessary. Mutates the colormap object!
- Parameters:
stop_index – color stop index to move
new_pos – new color stop position
new_color – new color stop color. If None, will use the current stop color. Defaults to None.
- class plotpy.widgets.colormap.widget.ColorMapWidget(parent: QWidget | None, cmap_width: int = 400, cmap_height: int = 50, color1: QColor | None = None, color2: QColor | None = None, colormap: EditableColormap | None = None)[source]¶
Simple colormap widget containing a horizontal slider and a colorbar image. The slider is used to edit the colormap that changes in real time. It is possible to add/remove handles by right-clicking on the colorbar image. This widget does not provide a way to change a handle color. For this, use the ColorMapEditor widget or the ColorMapManager widget.
- Parameters:
parent – parent widget
cmap_width – minimum width of the widget. Defaults to 400.
cmap_height – minimum height of the colorbar. Defaults to 50.
color1 – first color. Ignored if the ‘colormap’ argument is used. If None, default color is blue. Defaults to None.
color2 – second color. Ignored if the ‘colormap’ argument is used. If None, default color is yellow. Defaults to None.
colormap – colormap instance. If None, color1 and color2 will be used to create a new colormap. Defaults to None.
- set_colormap(colormap: EditableColormap) None [source]¶
Replaces the current colormap.
- Parameters:
colormap – replacement colormap
- get_colormap() EditableColormap [source]¶
Get the current colormap being edited.
- Returns:
current colormap
- emit_handle_selected() None [source]¶
When called, computes a slider position from the current position of the mouse and maps it to slider range. Then finds which handles was selected and emits the custom signal HANDLE_SELECTED(handle_index) and caches the index.
- get_color_from_value(value: float, as_type: type[~plotpy.widgets.colormap.widget.ColorTypeT] = <class 'PyQt5.QtGui.QColor'>) ColorTypeT [source]¶
Returns the color assigned to a given value using the current colormap.
- Parameters:
value – value to assign a color to
as_type – Color type to be returned (int or QG.QColor). Defaults to
QG.QColor.
- Returns:
The assigned color for the input value using the current colormap.
- get_handle_color(handle_index: int, as_type: type[~plotpy.widgets.colormap.widget.ColorTypeT] = <class 'PyQt5.QtGui.QColor'>) ColorTypeT [source]¶
Returns the color of a handled using its index. Directly return the color of the ColorStop object of the given index.
- Parameters:
handle_index – index of the handled to assign a color too.
as_type – Color type to be returned (int or QG.QColor). Defaults to
QG.QColor.
- Returns:
The assigned color for the input handle index using the current colormap.
- get_hex_color(handle_index: int) str [source]¶
Same as self.get_handle_color but returns the color as a hex color string.
- Parameters:
handle_index – index of the handled to assign a color too.
- Returns:
Handle’s hex color string
- edit_color_stop(index: int, new_pos: float | None = None, new_color: QColor | int | None = None) None [source]¶
Edit an existing color stop in the current colormap. Mutates the colormap object. Also edits the slider handle position.
- Parameters:
index – color stop index to mutate
new_pos – new color stop position. If not set will remain the same. Defaults to None.
new_color – new color. If not set will remain the same. Defaults to None.
- setup_menu() QMenu [source]¶
Setups the contextual menu used to insert and delete handles.
- Returns:
Colorbar contextual menu
- add_handle_at_relative_pos(relative_pos: float, new_color: QColor | int | None = None) None [source]¶
insert a handle in the widget at the relative position (between 0. and 1.). Mutates the colormap object. If the relative position is already occupied by a handle, the new handle will be inserted at the closest available position then will be moved back to the requested position.
- Parameters:
relative_pos – insertion position
- get_handles_list() list[float] [source]¶
Return the current handles as a mutable list. If the data doesn’t need to be mutated, prefer the method self.get_handles_tuple() that directly returns the handles tuple without making a conversion to a list.
- Returns:
Mutable list of handles position
- get_handles_tuple() tuple[float, ...] [source]¶
Passthrough to get the tuple of handles position.
- Returns:
Immutable tuple of handles position
Colormap editor¶
This module provides a more complete colormap editor widget than the one provided by ColorMapWidget (plotpy/widgets/colormap_widget.py). It allows to edit a colormap by changing its color stops (add/delete/move/change color).
Reference¶
- class plotpy.widgets.colormap.editor.ColorPickDataSet(title: str | None = None, comment: str | None = None, icon: str = '', readonly: bool = False)[source]¶
Dataset with the field used to edit a color stop.
- lock_position(lock=True) None [source]¶
Used to lock the position of the cursor value.
- Parameters:
lock – True if value must be locked. Defaults to True.
- is_position_locked()[source]¶
Returns True if the position is locked.
- Returns:
True if position is locked, False otherwise.
- set_position(position: float) None [source]¶
Set a new position for the cursor. Value is rounded to 2 decimals.
- Parameters:
position – new position to set
- get_position() float [source]¶
Return the current position of the cursor.
- Returns:
float value of the cursor position
- class plotpy.widgets.colormap.editor.ColorMapEditor(parent: QWidget | None, cmap_width: int = 400, cmap_height: int = 50, color1: QColor | None = None, color2: QColor | None = None, colormap: EditableColormap | None = None)[source]¶
Widget that allows a user to edit a colormap by changing its color stops ( add/delete/move/change color). Right click on the colorbar or slider to add or remove colors stops. A existing colormap instance can be used. However, the modifications are inplace so you should copy the object if you do not want to mutate it.
- Parameters:
parent – Parent widget.
cmap_width – Cmap size in pixels. Defaults to 400.
cmap_height – Cmap height in pixels. Defaults to 50.
color1 – First color of the colormap. If None, color blue is used. Ignored if argument ‘colormap’ is used. Defaults to None.
color2 – Last color of the colormap. If None, color yellow is used.Ignored if argument ‘colormap’ is used. Defaults to None
colormap – An already initialized colormap to use in the widget. Defaults to None.
- set_colormap(colormap: EditableColormap) None [source]¶
Replaces the current colormap.
- Parameters:
colormap – replacement colormap
- get_colormap() EditableColormap [source]¶
Get the current colormap being edited.
- Returns:
current colormap
- change_current_dataset(dataset_index: int) None [source]¶
Wrapper functio to change the current dataset (=current tab)
- Parameters:
dataset_index – dataset index (=tab) to set current
- update_tab_color(tab_index: int) None [source]¶
Update the tab icon color for the given tab index.
- Parameters:
tab_index – index of the tab to update
- update_tabs_names_from_current() None [source]¶
Update all the tab names from the current active tab (useful when a new one is added).
- new_tab(index: int, handle_pos: float) None [source]¶
Add/insert a new tab at the given index and set its relative position and color.
- Parameters:
index – index of the insertion/appending.
handle_pos – relative value to set in the tab (new handle current position)
- setup_tabs() None [source]¶
Clear and setup all the tabs from the current colormap. Can be used to reset all tabs after initialization.
- delete_tab(tab_index: int) None [source]¶
Removes the tab/dataset at given index.
- Parameters:
tab_index – index to remove
- class plotpy.widgets.colormap.editor.ColorPickDataSet(title: str | None = None, comment: str | None = None, icon: str = '', readonly: bool = False)[source]¶
Dataset with the field used to edit a color stop.
- lock_position(lock=True) None [source]¶
Used to lock the position of the cursor value.
- Parameters:
lock – True if value must be locked. Defaults to True.
- is_position_locked()[source]¶
Returns True if the position is locked.
- Returns:
True if position is locked, False otherwise.
- set_position(position: float) None [source]¶
Set a new position for the cursor. Value is rounded to 2 decimals.
- Parameters:
position – new position to set
- get_position() float [source]¶
Return the current position of the cursor.
- Returns:
float value of the cursor position
- class plotpy.widgets.colormap.editor.ColorMapEditor(parent: QWidget | None, cmap_width: int = 400, cmap_height: int = 50, color1: QColor | None = None, color2: QColor | None = None, colormap: EditableColormap | None = None)[source]¶
Widget that allows a user to edit a colormap by changing its color stops ( add/delete/move/change color). Right click on the colorbar or slider to add or remove colors stops. A existing colormap instance can be used. However, the modifications are inplace so you should copy the object if you do not want to mutate it.
- Parameters:
parent – Parent widget.
cmap_width – Cmap size in pixels. Defaults to 400.
cmap_height – Cmap height in pixels. Defaults to 50.
color1 – First color of the colormap. If None, color blue is used. Ignored if argument ‘colormap’ is used. Defaults to None.
color2 – Last color of the colormap. If None, color yellow is used.Ignored if argument ‘colormap’ is used. Defaults to None
colormap – An already initialized colormap to use in the widget. Defaults to None.
- set_colormap(colormap: EditableColormap) None [source]¶
Replaces the current colormap.
- Parameters:
colormap – replacement colormap
- get_colormap() EditableColormap [source]¶
Get the current colormap being edited.
- Returns:
current colormap
- change_current_dataset(dataset_index: int) None [source]¶
Wrapper functio to change the current dataset (=current tab)
- Parameters:
dataset_index – dataset index (=tab) to set current
- update_tab_color(tab_index: int) None [source]¶
Update the tab icon color for the given tab index.
- Parameters:
tab_index – index of the tab to update
- update_tabs_names_from_current() None [source]¶
Update all the tab names from the current active tab (useful when a new one is added).
- new_tab(index: int, handle_pos: float) None [source]¶
Add/insert a new tab at the given index and set its relative position and color.
- Parameters:
index – index of the insertion/appending.
handle_pos – relative value to set in the tab (new handle current position)
- setup_tabs() None [source]¶
Clear and setup all the tabs from the current colormap. Can be used to reset all tabs after initialization.
- delete_tab(tab_index: int) None [source]¶
Removes the tab/dataset at given index.
- Parameters:
tab_index – index to remove
Colormap manager¶
This module provides a fully featured colormap manager widget and dialog window that allows to select, edit and save colormaps (existing or new).
Reference¶
- class plotpy.widgets.colormap.manager.ColorMapNameEdit(parent: QWidget | None = None, title: str = '', name: str = '')[source]¶
Dialog box to enter a colormap name.
- Parameters:
parent – parent QWidget. Defaults to None.
title – dialog box title. Defaults to “”.
name – default colormap name. Defaults to “”.
- get_colormap_name() str [source]¶
Return the colormap name entered in the QLineEdit.
- Returns:
colormap name
- classmethod edit(parent: QWidget | None = None, title: str = '', name: str = '') str | None [source]¶
Open the dialog box and return the colormap name entered in the QLineEdit.
- Parameters:
parent – parent QWidget. Defaults to None.
title – dialog box title. Defaults to “”.
name – default colormap name. Defaults to “”.
- Returns:
colormap name, or None if the dialog box was canceled.
- class plotpy.widgets.colormap.manager.ColorMapManager(parent: QWidget | None = None, active_colormap: str | None = None)[source]¶
Colormap manager dialog box. Allows to select, edit and save colormaps.
- Parameters:
parent – parent QWidget. Defaults to None.
active_colormap – name of the active colormap.
Note
The active colormap is the colormap that will be selected by default when the dialog box is opened. If the colormap does not exist (or if None is provided), the first colormap in the list will be selected by default.
The active colormap cannot be removed. If the active colormap is a custom colormap, the remove button will be enabled but a dialog box will warn the user that the colormap cannot be removed.
- button_clicked(button: QAbstractButton) None [source]¶
Callback function to be called when a button is clicked.
- Parameters:
button – button clicked
- property current_changes_saved: bool¶
Getter to know if the current colormap has been saved or not.
- Returns:
True if the current colormap has been saved, False otherwise.
- set_colormap(index: int) None [source]¶
Set the current colormap to the value present at the given index in the QComboBox. Makes a copy of the colormap object so the ColorMapEditor does not mutate the original colormap object.
- Parameters:
index – index of the colormap in the QComboBox.
- get_colormap() EditableColormap | None [source]¶
Return the selected colormap object.
- Returns:
selected colormap object
- save_colormap(cmap: EditableColormap | None = None) bool [source]¶
Saves the current colormap and handles the validation process. The saved colormaps can only be saved in the custom colormaps.
- Parameters:
cmap – colormap to save. If None, will use the current colormap.
- Returns:
True if the colormap has been saved, False otherwise.
- class plotpy.widgets.colormap.manager.ColorMapNameEdit(parent: QWidget | None = None, title: str = '', name: str = '')[source]¶
Dialog box to enter a colormap name.
- Parameters:
parent – parent QWidget. Defaults to None.
title – dialog box title. Defaults to “”.
name – default colormap name. Defaults to “”.
- get_colormap_name() str [source]¶
Return the colormap name entered in the QLineEdit.
- Returns:
colormap name
- classmethod edit(parent: QWidget | None = None, title: str = '', name: str = '') str | None [source]¶
Open the dialog box and return the colormap name entered in the QLineEdit.
- Parameters:
parent – parent QWidget. Defaults to None.
title – dialog box title. Defaults to “”.
name – default colormap name. Defaults to “”.
- Returns:
colormap name, or None if the dialog box was canceled.
- class plotpy.widgets.colormap.manager.ColorMapManager(parent: QWidget | None = None, active_colormap: str | None = None)[source]¶
Colormap manager dialog box. Allows to select, edit and save colormaps.
- Parameters:
parent – parent QWidget. Defaults to None.
active_colormap – name of the active colormap.
Note
The active colormap is the colormap that will be selected by default when the dialog box is opened. If the colormap does not exist (or if None is provided), the first colormap in the list will be selected by default.
The active colormap cannot be removed. If the active colormap is a custom colormap, the remove button will be enabled but a dialog box will warn the user that the colormap cannot be removed.
- button_clicked(button: QAbstractButton) None [source]¶
Callback function to be called when a button is clicked.
- Parameters:
button – button clicked
- property current_changes_saved: bool¶
Getter to know if the current colormap has been saved or not.
- Returns:
True if the current colormap has been saved, False otherwise.
- set_colormap(index: int) None [source]¶
Set the current colormap to the value present at the given index in the QComboBox. Makes a copy of the colormap object so the ColorMapEditor does not mutate the original colormap object.
- Parameters:
index – index of the colormap in the QComboBox.
- get_colormap() EditableColormap | None [source]¶
Return the selected colormap object.
- Returns:
selected colormap object
- save_colormap(cmap: EditableColormap | None = None) bool [source]¶
Saves the current colormap and handles the validation process. The saved colormaps can only be saved in the custom colormaps.
- Parameters:
cmap – colormap to save. If None, will use the current colormap.
- Returns:
True if the colormap has been saved, False otherwise.