MESQUAL Plotly Util px_category_utils¶
px_category_utils
¶
Utilities for working with categorical data in Plotly Express subplots and faceted figures.
This module provides functions to navigate and manipulate subplot structures created by Plotly Express, particularly when dealing with faceted plots and categorical data. It enables precise positioning of additional elements on specific subplot axes.
get_x_y_axis_for_category
¶
get_x_y_axis_for_category(fig: Figure, category_args: dict[str, str]) -> tuple[str, str]
Find the x and y axis names for a subplot matching specific category values.
Searches through figure traces to find one whose hovertemplate contains all specified category key-value pairs, then returns the corresponding axis names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure object containing subplot traces. |
required |
category_args
|
dict[str, str]
|
Dictionary mapping category names to their values (e.g., {'sex': 'Male', 'smoker': 'Yes'}). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Tuple containing the x-axis and y-axis names (e.g., ('x', 'y') or |
str
|
('x2', 'y3')). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no trace contains all the specified category key-value pairs in its hovertemplate. |
Example:
>>> category_args = {'sex': 'Male', 'smoker': 'Yes'}
>>> x_axis, y_axis = get_x_y_axis_for_category(fig, category_args)
>>> print(f"Found axes: {x_axis}, {y_axis}")
Found axes: x2, y3
Source code in submodules/mesqual/mesqual/utils/plotly_utils/px_category_utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
get_all_x_axis_names
¶
get_all_x_axis_names(fig: Figure) -> list[str]
Get all x-axis names from a Plotly figure layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure object. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of x-axis attribute names found in the figure layout (e.g., |
list[str]
|
['xaxis', 'xaxis2', 'xaxis3']). |
Source code in submodules/mesqual/mesqual/utils/plotly_utils/px_category_utils.py
47 48 49 50 51 52 53 54 55 56 57 | |
get_all_y_axis_names
¶
get_all_y_axis_names(fig: Figure) -> list[str]
Get all y-axis names from a Plotly figure layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure object. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of y-axis attribute names found in the figure layout (e.g., |
list[str]
|
['yaxis', 'yaxis2', 'yaxis3']). |
Source code in submodules/mesqual/mesqual/utils/plotly_utils/px_category_utils.py
60 61 62 63 64 65 66 67 68 69 70 | |
get_row_col_for_x_y_axis
¶
get_row_col_for_x_y_axis(fig: Figure, x_axis: str, y_axis: str) -> tuple[int, int]
Convert axis names to subplot row and column indices.
Determines the subplot grid position by analyzing axis domains within the figure layout. This is useful for adding elements to specific subplots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure object containing subplots. |
required |
x_axis
|
str
|
X-axis name (e.g., 'x', 'x2', 'xaxis', 'xaxis2'). |
required |
y_axis
|
str
|
Y-axis name (e.g., 'y', 'y2', 'yaxis', 'yaxis2'). |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Tuple of (row, col) indices for the subplot (1-indexed). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the specified axis names are not found in the figure layout. |
Example:
>>> row, col = get_row_col_for_x_y_axis(fig, 'x2', 'y3')
>>> print(f"Subplot at row {row}, column {col}")
Subplot at row 2, column 3
Source code in submodules/mesqual/mesqual/utils/plotly_utils/px_category_utils.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
get_subplot_row_and_col_for_category
¶
get_subplot_row_and_col_for_category(fig: Figure, category_args: dict[str, str]) -> tuple[int, int]
Get subplot row and column for a specific category combination.
Combines axis lookup and position conversion to directly find the subplot location for given category values. This is the main convenience function for adding elements to category-specific subplots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure object containing faceted subplots. |
required |
category_args
|
dict[str, str]
|
Dictionary mapping category names to their values. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Tuple of (row, col) indices for the subplot (1-indexed). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no subplot matches the specified category combination or if axis names cannot be found. |
Example:
>>> category_args = {'sex': 'Female', 'smoker': 'No'}
>>> row, col = get_subplot_row_and_col_for_category(fig, category_args)
>>> fig.add_trace(trace, row=row, col=col)
Source code in submodules/mesqual/mesqual/utils/plotly_utils/px_category_utils.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
get_index_for_category_on_axis
¶
get_index_for_category_on_axis(fig: Figure, axis: str, category_value: str) -> int
Get the numerical index of a categorical value on a specific axis.
Converts a categorical string value to its corresponding numerical position on the specified axis. Useful for precise positioning of annotations or additional traces on categorical axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure object. |
required |
axis
|
str
|
Axis name (e.g., 'x', 'x2', 'y', 'y2', 'xaxis', 'yaxis2'). |
required |
category_value
|
str
|
String value of the category to find. |
required |
Returns:
| Type | Description |
|---|---|
int
|
1-indexed numerical position of the category on the axis. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If category_value is not a string. |
KeyError
|
If the axis name is invalid or the category value is not found in the axis categoryarray. |
Example:
>>> index = get_index_for_category_on_axis(fig, 'x', 'Dinner')
>>> print(f"'Dinner' is at position {index}")
'Dinner' is at position 2
Source code in submodules/mesqual/mesqual/utils/plotly_utils/px_category_utils.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |