risk-network 0.0.7b12__py3-none-any.whl → 0.0.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- risk/__init__.py +1 -1
- risk/annotations/__init__.py +1 -1
- risk/annotations/annotations.py +85 -53
- risk/annotations/io.py +3 -3
- risk/log/__init__.py +1 -1
- risk/log/{config.py → console.py} +2 -2
- risk/log/params.py +6 -6
- risk/neighborhoods/community.py +68 -61
- risk/neighborhoods/domains.py +41 -18
- risk/neighborhoods/neighborhoods.py +134 -69
- risk/network/geometry.py +5 -2
- risk/network/graph.py +69 -235
- risk/network/io.py +44 -6
- risk/network/plot/__init__.py +6 -0
- risk/network/plot/canvas.py +290 -0
- risk/network/plot/contour.py +327 -0
- risk/network/plot/labels.py +929 -0
- risk/network/plot/network.py +288 -0
- risk/network/plot/plotter.py +137 -0
- risk/network/plot/utils/color.py +424 -0
- risk/network/plot/utils/layout.py +91 -0
- risk/risk.py +70 -37
- risk/stats/hypergeom.py +1 -1
- risk/stats/permutation/permutation.py +21 -8
- risk/stats/poisson.py +2 -2
- risk/stats/stats.py +12 -10
- {risk_network-0.0.7b12.dist-info → risk_network-0.0.8.dist-info}/METADATA +84 -21
- risk_network-0.0.8.dist-info/RECORD +37 -0
- {risk_network-0.0.7b12.dist-info → risk_network-0.0.8.dist-info}/WHEEL +1 -1
- risk/network/plot.py +0 -1450
- risk_network-0.0.7b12.dist-info/RECORD +0 -30
- {risk_network-0.0.7b12.dist-info → risk_network-0.0.8.dist-info}/LICENSE +0 -0
- {risk_network-0.0.7b12.dist-info → risk_network-0.0.8.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,288 @@
|
|
1
|
+
"""
|
2
|
+
risk/network/plot/network
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
|
+
"""
|
5
|
+
|
6
|
+
from typing import Any, List, Tuple, Union
|
7
|
+
|
8
|
+
import networkx as nx
|
9
|
+
import numpy as np
|
10
|
+
|
11
|
+
from risk.log import params
|
12
|
+
from risk.network.graph import NetworkGraph
|
13
|
+
from risk.network.plot.utils.color import get_domain_colors, to_rgba
|
14
|
+
|
15
|
+
|
16
|
+
class Network:
|
17
|
+
"""Class for plotting nodes and edges in a network graph."""
|
18
|
+
|
19
|
+
def __init__(self, graph: NetworkGraph, ax: Any = None) -> None:
|
20
|
+
"""Initialize the NetworkPlotter class.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
graph (NetworkGraph): The network data and attributes to be visualized.
|
24
|
+
ax (Any, optional): Axes object to plot the network graph. Defaults to None.
|
25
|
+
"""
|
26
|
+
self.graph = graph
|
27
|
+
self.ax = ax
|
28
|
+
|
29
|
+
def plot_network(
|
30
|
+
self,
|
31
|
+
node_size: Union[int, np.ndarray] = 50,
|
32
|
+
node_shape: str = "o",
|
33
|
+
node_edgewidth: float = 1.0,
|
34
|
+
edge_width: float = 1.0,
|
35
|
+
node_color: Union[str, List, Tuple, np.ndarray] = "white",
|
36
|
+
node_edgecolor: Union[str, List, Tuple, np.ndarray] = "black",
|
37
|
+
edge_color: Union[str, List, Tuple, np.ndarray] = "black",
|
38
|
+
node_alpha: Union[float, None] = 1.0,
|
39
|
+
edge_alpha: Union[float, None] = 1.0,
|
40
|
+
) -> None:
|
41
|
+
"""Plot the network graph with customizable node colors, sizes, edge widths, and node edge widths.
|
42
|
+
|
43
|
+
Args:
|
44
|
+
node_size (int or np.ndarray, optional): Size of the nodes. Can be a single integer or an array of sizes. Defaults to 50.
|
45
|
+
node_shape (str, optional): Shape of the nodes. Defaults to "o".
|
46
|
+
node_edgewidth (float, optional): Width of the node edges. Defaults to 1.0.
|
47
|
+
edge_width (float, optional): Width of the edges. Defaults to 1.0.
|
48
|
+
node_color (str, List, Tuple, or np.ndarray, optional): Color of the nodes. Can be a single color or an array of colors.
|
49
|
+
Defaults to "white".
|
50
|
+
node_edgecolor (str, List, Tuple, or np.ndarray, optional): Color of the node edges. Can be a single color or an array of colors.
|
51
|
+
Defaults to "black".
|
52
|
+
edge_color (str, List, Tuple, or np.ndarray, optional): Color of the edges. Can be a single color or an array of colors.
|
53
|
+
Defaults to "black".
|
54
|
+
node_alpha (float, None, optional): Alpha value (transparency) for the nodes. If provided, it overrides any existing alpha
|
55
|
+
values found in node_color. Defaults to 1.0. Annotated node_color alphas will override this value.
|
56
|
+
edge_alpha (float, None, optional): Alpha value (transparency) for the edges. If provided, it overrides any existing alpha
|
57
|
+
values found in edge_color. Defaults to 1.0.
|
58
|
+
"""
|
59
|
+
# Log the plotting parameters
|
60
|
+
params.log_plotter(
|
61
|
+
network_node_size=(
|
62
|
+
"custom" if isinstance(node_size, np.ndarray) else node_size
|
63
|
+
), # np.ndarray usually indicates custom sizes
|
64
|
+
network_node_shape=node_shape,
|
65
|
+
network_node_edgewidth=node_edgewidth,
|
66
|
+
network_edge_width=edge_width,
|
67
|
+
network_node_color=(
|
68
|
+
"custom" if isinstance(node_color, np.ndarray) else node_color
|
69
|
+
), # np.ndarray usually indicates custom colors
|
70
|
+
network_node_edgecolor=node_edgecolor,
|
71
|
+
network_edge_color=edge_color,
|
72
|
+
network_node_alpha=node_alpha,
|
73
|
+
network_edge_alpha=edge_alpha,
|
74
|
+
)
|
75
|
+
|
76
|
+
# Convert colors to RGBA using the to_rgba helper function
|
77
|
+
# If node_colors was generated using get_annotated_node_colors, its alpha values will override node_alpha
|
78
|
+
node_color_rgba = to_rgba(
|
79
|
+
color=node_color, alpha=node_alpha, num_repeats=len(self.graph.network.nodes)
|
80
|
+
)
|
81
|
+
node_edgecolor_rgba = to_rgba(
|
82
|
+
color=node_edgecolor, alpha=1.0, num_repeats=len(self.graph.network.nodes)
|
83
|
+
)
|
84
|
+
edge_color_rgba = to_rgba(
|
85
|
+
color=edge_color, alpha=edge_alpha, num_repeats=len(self.graph.network.edges)
|
86
|
+
)
|
87
|
+
|
88
|
+
# Extract node coordinates from the network graph
|
89
|
+
node_coordinates = self.graph.node_coordinates
|
90
|
+
|
91
|
+
# Draw the nodes of the graph
|
92
|
+
nx.draw_networkx_nodes(
|
93
|
+
self.graph.network,
|
94
|
+
pos=node_coordinates,
|
95
|
+
node_size=node_size,
|
96
|
+
node_shape=node_shape,
|
97
|
+
node_color=node_color_rgba,
|
98
|
+
edgecolors=node_edgecolor_rgba,
|
99
|
+
linewidths=node_edgewidth,
|
100
|
+
ax=self.ax,
|
101
|
+
)
|
102
|
+
# Draw the edges of the graph
|
103
|
+
nx.draw_networkx_edges(
|
104
|
+
self.graph.network,
|
105
|
+
pos=node_coordinates,
|
106
|
+
width=edge_width,
|
107
|
+
edge_color=edge_color_rgba,
|
108
|
+
ax=self.ax,
|
109
|
+
)
|
110
|
+
|
111
|
+
def plot_subnetwork(
|
112
|
+
self,
|
113
|
+
nodes: Union[List, Tuple, np.ndarray],
|
114
|
+
node_size: Union[int, np.ndarray] = 50,
|
115
|
+
node_shape: str = "o",
|
116
|
+
node_edgewidth: float = 1.0,
|
117
|
+
edge_width: float = 1.0,
|
118
|
+
node_color: Union[str, List, Tuple, np.ndarray] = "white",
|
119
|
+
node_edgecolor: Union[str, List, Tuple, np.ndarray] = "black",
|
120
|
+
edge_color: Union[str, List, Tuple, np.ndarray] = "black",
|
121
|
+
node_alpha: Union[float, None] = None,
|
122
|
+
edge_alpha: Union[float, None] = None,
|
123
|
+
) -> None:
|
124
|
+
"""Plot a subnetwork of selected nodes with customizable node and edge attributes.
|
125
|
+
|
126
|
+
Args:
|
127
|
+
nodes (List, Tuple, or np.ndarray): List of node labels to include in the subnetwork. Accepts nested lists.
|
128
|
+
node_size (int or np.ndarray, optional): Size of the nodes. Can be a single integer or an array of sizes. Defaults to 50.
|
129
|
+
node_shape (str, optional): Shape of the nodes. Defaults to "o".
|
130
|
+
node_edgewidth (float, optional): Width of the node edges. Defaults to 1.0.
|
131
|
+
edge_width (float, optional): Width of the edges. Defaults to 1.0.
|
132
|
+
node_color (str, List, Tuple, or np.ndarray, optional): Color of the nodes. Defaults to "white".
|
133
|
+
node_edgecolor (str, List, Tuple, or np.ndarray, optional): Color of the node edges. Defaults to "black".
|
134
|
+
edge_color (str, List, Tuple, or np.ndarray, optional): Color of the edges. Defaults to "black".
|
135
|
+
node_alpha (float, None, optional): Transparency for the nodes. If provided, it overrides any existing alpha values
|
136
|
+
found in node_color. Defaults to 1.0.
|
137
|
+
edge_alpha (float, None, optional): Transparency for the edges. If provided, it overrides any existing alpha values
|
138
|
+
found in node_color. Defaults to 1.0.
|
139
|
+
|
140
|
+
Raises:
|
141
|
+
ValueError: If no valid nodes are found in the network graph.
|
142
|
+
"""
|
143
|
+
# Flatten nested lists of nodes, if necessary
|
144
|
+
if any(isinstance(item, (list, tuple, np.ndarray)) for item in nodes):
|
145
|
+
nodes = [node for sublist in nodes for node in sublist]
|
146
|
+
|
147
|
+
# Filter to get node IDs and their coordinates
|
148
|
+
node_ids = [
|
149
|
+
self.graph.node_label_to_node_id_map.get(node)
|
150
|
+
for node in nodes
|
151
|
+
if node in self.graph.node_label_to_node_id_map
|
152
|
+
]
|
153
|
+
if not node_ids:
|
154
|
+
raise ValueError("No nodes found in the network graph.")
|
155
|
+
|
156
|
+
# Check if node_color is a single color or a list of colors
|
157
|
+
if not isinstance(node_color, (str, Tuple, np.ndarray)):
|
158
|
+
node_color = [
|
159
|
+
node_color[nodes.index(node)]
|
160
|
+
for node in nodes
|
161
|
+
if node in self.graph.node_label_to_node_id_map
|
162
|
+
]
|
163
|
+
|
164
|
+
# Convert colors to RGBA using the to_rgba helper function
|
165
|
+
node_color_rgba = to_rgba(color=node_color, alpha=node_alpha, num_repeats=len(node_ids))
|
166
|
+
node_edgecolor_rgba = to_rgba(color=node_edgecolor, alpha=1.0, num_repeats=len(node_ids))
|
167
|
+
edge_color_rgba = to_rgba(
|
168
|
+
color=edge_color, alpha=edge_alpha, num_repeats=len(self.graph.network.edges)
|
169
|
+
)
|
170
|
+
|
171
|
+
# Get the coordinates of the filtered nodes
|
172
|
+
node_coordinates = {node_id: self.graph.node_coordinates[node_id] for node_id in node_ids}
|
173
|
+
|
174
|
+
# Draw the nodes in the subnetwork
|
175
|
+
nx.draw_networkx_nodes(
|
176
|
+
self.graph.network,
|
177
|
+
pos=node_coordinates,
|
178
|
+
nodelist=node_ids,
|
179
|
+
node_size=node_size,
|
180
|
+
node_shape=node_shape,
|
181
|
+
node_color=node_color_rgba,
|
182
|
+
edgecolors=node_edgecolor_rgba,
|
183
|
+
linewidths=node_edgewidth,
|
184
|
+
ax=self.ax,
|
185
|
+
)
|
186
|
+
# Draw the edges between the specified nodes in the subnetwork
|
187
|
+
subgraph = self.graph.network.subgraph(node_ids)
|
188
|
+
nx.draw_networkx_edges(
|
189
|
+
subgraph,
|
190
|
+
pos=node_coordinates,
|
191
|
+
width=edge_width,
|
192
|
+
edge_color=edge_color_rgba,
|
193
|
+
ax=self.ax,
|
194
|
+
)
|
195
|
+
|
196
|
+
def get_annotated_node_colors(
|
197
|
+
self,
|
198
|
+
cmap: str = "gist_rainbow",
|
199
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
200
|
+
blend_colors: bool = False,
|
201
|
+
blend_gamma: float = 2.2,
|
202
|
+
min_scale: float = 0.8,
|
203
|
+
max_scale: float = 1.0,
|
204
|
+
scale_factor: float = 1.0,
|
205
|
+
alpha: Union[float, None] = 1.0,
|
206
|
+
nonenriched_color: Union[str, List, Tuple, np.ndarray] = "white",
|
207
|
+
nonenriched_alpha: Union[float, None] = 1.0,
|
208
|
+
random_seed: int = 888,
|
209
|
+
) -> np.ndarray:
|
210
|
+
"""Adjust the colors of nodes in the network graph based on enrichment.
|
211
|
+
|
212
|
+
Args:
|
213
|
+
cmap (str, optional): Colormap to use for coloring the nodes. Defaults to "gist_rainbow".
|
214
|
+
color (str, List, Tuple, np.ndarray, or None, optional): Color to use for the nodes. Can be a single color or an array of colors.
|
215
|
+
If None, the colormap will be used. Defaults to None.
|
216
|
+
blend_colors (bool, optional): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
217
|
+
blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
|
218
|
+
min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
|
219
|
+
max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
|
220
|
+
scale_factor (float, optional): Factor for adjusting the color scaling intensity. Defaults to 1.0.
|
221
|
+
alpha (float, None, optional): Alpha value for enriched nodes. If provided, it overrides any existing alpha values found in `color`.
|
222
|
+
Defaults to 1.0.
|
223
|
+
nonenriched_color (str, List, Tuple, or np.ndarray, optional): Color for non-enriched nodes. Can be a single color or an array of colors.
|
224
|
+
Defaults to "white".
|
225
|
+
nonenriched_alpha (float, None, optional): Alpha value for non-enriched nodes. If provided, it overrides any existing alpha values found
|
226
|
+
in `nonenriched_color`. Defaults to 1.0.
|
227
|
+
random_seed (int, optional): Seed for random number generation. Defaults to 888.
|
228
|
+
|
229
|
+
Returns:
|
230
|
+
np.ndarray: Array of RGBA colors adjusted for enrichment status.
|
231
|
+
"""
|
232
|
+
# Get the initial domain colors for each node, which are returned as RGBA
|
233
|
+
network_colors = get_domain_colors(
|
234
|
+
graph=self.graph,
|
235
|
+
cmap=cmap,
|
236
|
+
color=color,
|
237
|
+
blend_colors=blend_colors,
|
238
|
+
blend_gamma=blend_gamma,
|
239
|
+
min_scale=min_scale,
|
240
|
+
max_scale=max_scale,
|
241
|
+
scale_factor=scale_factor,
|
242
|
+
random_seed=random_seed,
|
243
|
+
)
|
244
|
+
# Apply the alpha value for enriched nodes
|
245
|
+
network_colors[:, 3] = alpha # Apply the alpha value to the enriched nodes' A channel
|
246
|
+
# Convert the non-enriched color to RGBA using the to_rgba helper function
|
247
|
+
nonenriched_color_rgba = to_rgba(
|
248
|
+
color=nonenriched_color, alpha=nonenriched_alpha, num_repeats=1
|
249
|
+
) # num_repeats=1 for a single color
|
250
|
+
# Adjust node colors: replace any nodes where all three RGB values are equal and less than 0.1
|
251
|
+
# 0.1 is a predefined threshold for the minimum color intensity
|
252
|
+
adjusted_network_colors = np.where(
|
253
|
+
(
|
254
|
+
np.all(network_colors[:, :3] < 0.1, axis=1)
|
255
|
+
& np.all(network_colors[:, :3] == network_colors[:, 0:1], axis=1)
|
256
|
+
)[:, None],
|
257
|
+
np.tile(
|
258
|
+
np.array(nonenriched_color_rgba), (network_colors.shape[0], 1)
|
259
|
+
), # Replace with the full RGBA non-enriched color
|
260
|
+
network_colors, # Keep the original colors where no match is found
|
261
|
+
)
|
262
|
+
return adjusted_network_colors
|
263
|
+
|
264
|
+
def get_annotated_node_sizes(
|
265
|
+
self, enriched_size: int = 50, nonenriched_size: int = 25
|
266
|
+
) -> np.ndarray:
|
267
|
+
"""Adjust the sizes of nodes in the network graph based on whether they are enriched or not.
|
268
|
+
|
269
|
+
Args:
|
270
|
+
enriched_size (int): Size for enriched nodes. Defaults to 50.
|
271
|
+
nonenriched_size (int): Size for non-enriched nodes. Defaults to 25.
|
272
|
+
|
273
|
+
Returns:
|
274
|
+
np.ndarray: Array of node sizes, with enriched nodes larger than non-enriched ones.
|
275
|
+
"""
|
276
|
+
# Merge all enriched nodes from the domain_id_to_node_ids_map dictionary
|
277
|
+
enriched_nodes = set()
|
278
|
+
for _, node_ids in self.graph.domain_id_to_node_ids_map.items():
|
279
|
+
enriched_nodes.update(node_ids)
|
280
|
+
|
281
|
+
# Initialize all node sizes to the non-enriched size
|
282
|
+
node_sizes = np.full(len(self.graph.network.nodes), nonenriched_size)
|
283
|
+
# Set the size for enriched nodes
|
284
|
+
for node in enriched_nodes:
|
285
|
+
if node in self.graph.network.nodes:
|
286
|
+
node_sizes[node] = enriched_size
|
287
|
+
|
288
|
+
return node_sizes
|
@@ -0,0 +1,137 @@
|
|
1
|
+
"""
|
2
|
+
risk/network/plot/plotter
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
|
+
"""
|
5
|
+
|
6
|
+
from typing import List, Tuple, Union
|
7
|
+
|
8
|
+
import matplotlib.pyplot as plt
|
9
|
+
import numpy as np
|
10
|
+
|
11
|
+
from risk.log import params
|
12
|
+
from risk.network.graph import NetworkGraph
|
13
|
+
from risk.network.plot.canvas import Canvas
|
14
|
+
from risk.network.plot.contour import Contour
|
15
|
+
from risk.network.plot.labels import Labels
|
16
|
+
from risk.network.plot.network import Network
|
17
|
+
from risk.network.plot.utils.color import to_rgba
|
18
|
+
from risk.network.plot.utils.layout import calculate_bounding_box
|
19
|
+
|
20
|
+
|
21
|
+
class NetworkPlotter(Canvas, Network, Contour, Labels):
|
22
|
+
"""A class for visualizing network graphs with customizable options.
|
23
|
+
|
24
|
+
The NetworkPlotter class uses a NetworkGraph object and provides methods to plot the network with
|
25
|
+
flexible node and edge properties. It also supports plotting labels, contours, drawing the network's
|
26
|
+
perimeter, and adjusting background colors.
|
27
|
+
"""
|
28
|
+
|
29
|
+
def __init__(
|
30
|
+
self,
|
31
|
+
graph: NetworkGraph,
|
32
|
+
figsize: Tuple = (10, 10),
|
33
|
+
background_color: Union[str, List, Tuple, np.ndarray] = "white",
|
34
|
+
background_alpha: Union[float, None] = 1.0,
|
35
|
+
pad: float = 0.3,
|
36
|
+
) -> None:
|
37
|
+
"""Initialize the NetworkPlotter with a NetworkGraph object and plotting parameters.
|
38
|
+
|
39
|
+
Args:
|
40
|
+
graph (NetworkGraph): The network data and attributes to be visualized.
|
41
|
+
figsize (Tuple, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
|
42
|
+
background_color (str, List, Tuple, np.ndarray, optional): Background color of the plot. Defaults to "white".
|
43
|
+
background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides
|
44
|
+
any existing alpha values found in background_color. Defaults to 1.0.
|
45
|
+
pad (float, optional): Padding value to adjust the axis limits. Defaults to 0.3.
|
46
|
+
"""
|
47
|
+
self.graph = graph
|
48
|
+
# Initialize the plot with the specified parameters
|
49
|
+
self.ax = self._initialize_plot(
|
50
|
+
graph=graph,
|
51
|
+
figsize=figsize,
|
52
|
+
background_color=background_color,
|
53
|
+
background_alpha=background_alpha,
|
54
|
+
pad=pad,
|
55
|
+
)
|
56
|
+
super().__init__(graph=graph, ax=self.ax)
|
57
|
+
|
58
|
+
def _initialize_plot(
|
59
|
+
self,
|
60
|
+
graph: NetworkGraph,
|
61
|
+
figsize: Tuple,
|
62
|
+
background_color: Union[str, List, Tuple, np.ndarray],
|
63
|
+
background_alpha: Union[float, None],
|
64
|
+
pad: float,
|
65
|
+
) -> plt.Axes:
|
66
|
+
"""Set up the plot with figure size and background color.
|
67
|
+
|
68
|
+
Args:
|
69
|
+
graph (NetworkGraph): The network data and attributes to be visualized.
|
70
|
+
figsize (Tuple): Size of the figure in inches (width, height).
|
71
|
+
background_color (str, List, Tuple, or np.ndarray): Background color of the plot. Can be a single color or an array of colors.
|
72
|
+
background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides any existing
|
73
|
+
alpha values found in `background_color`.
|
74
|
+
pad (float, optional): Padding value to adjust the axis limits.
|
75
|
+
|
76
|
+
Returns:
|
77
|
+
plt.Axes: The axis object for the plot.
|
78
|
+
"""
|
79
|
+
# Log the plotter settings
|
80
|
+
params.log_plotter(
|
81
|
+
figsize=figsize,
|
82
|
+
background_color=background_color,
|
83
|
+
background_alpha=background_alpha,
|
84
|
+
pad=pad,
|
85
|
+
)
|
86
|
+
|
87
|
+
# Extract node coordinates from the network graph
|
88
|
+
node_coordinates = graph.node_coordinates
|
89
|
+
# Calculate the center and radius of the bounding box around the network
|
90
|
+
center, radius = calculate_bounding_box(node_coordinates)
|
91
|
+
|
92
|
+
# Create a new figure and axis for plotting
|
93
|
+
fig, ax = plt.subplots(figsize=figsize)
|
94
|
+
fig.tight_layout() # Adjust subplot parameters to give specified padding
|
95
|
+
# Set axis limits based on the calculated bounding box and radius
|
96
|
+
ax.set_xlim([center[0] - radius - pad, center[0] + radius + pad])
|
97
|
+
ax.set_ylim([center[1] - radius - pad, center[1] + radius + pad])
|
98
|
+
ax.set_aspect("equal") # Ensure the aspect ratio is equal
|
99
|
+
|
100
|
+
# Set the background color of the plot
|
101
|
+
# Convert color to RGBA using the to_rgba helper function
|
102
|
+
fig.patch.set_facecolor(
|
103
|
+
to_rgba(color=background_color, alpha=background_alpha, num_repeats=1)
|
104
|
+
) # num_repeats=1 for single color
|
105
|
+
ax.invert_yaxis() # Invert the y-axis to match typical image coordinates
|
106
|
+
# Remove axis spines for a cleaner look
|
107
|
+
for spine in ax.spines.values():
|
108
|
+
spine.set_visible(False)
|
109
|
+
|
110
|
+
# Hide axis ticks and labels
|
111
|
+
ax.set_xticks([])
|
112
|
+
ax.set_yticks([])
|
113
|
+
ax.patch.set_visible(False) # Hide the axis background
|
114
|
+
|
115
|
+
return ax
|
116
|
+
|
117
|
+
@staticmethod
|
118
|
+
def savefig(*args, pad_inches: float = 0.5, dpi: int = 100, **kwargs) -> None:
|
119
|
+
"""Save the current plot to a file with additional export options.
|
120
|
+
|
121
|
+
Args:
|
122
|
+
*args: Positional arguments passed to `plt.savefig`.
|
123
|
+
pad_inches (float, optional): Padding around the figure when saving. Defaults to 0.5.
|
124
|
+
dpi (int, optional): Dots per inch (DPI) for the exported image. Defaults to 300.
|
125
|
+
**kwargs: Keyword arguments passed to `plt.savefig`, such as filename and format.
|
126
|
+
"""
|
127
|
+
plt.savefig(*args, bbox_inches="tight", pad_inches=pad_inches, dpi=dpi, **kwargs)
|
128
|
+
|
129
|
+
@staticmethod
|
130
|
+
def show(*args, **kwargs) -> None:
|
131
|
+
"""Display the current plot.
|
132
|
+
|
133
|
+
Args:
|
134
|
+
*args: Positional arguments passed to `plt.show`.
|
135
|
+
**kwargs: Keyword arguments passed to `plt.show`.
|
136
|
+
"""
|
137
|
+
plt.show(*args, **kwargs)
|