-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfigure_customizer.py
144 lines (122 loc) · 5.04 KB
/
figure_customizer.py
1
2
3
4
5
6
7
8
9
10
11
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
115
116
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
144
"""
This script contains a set of functions to customize matplotlib plots.
These functions allow users to easily modify aspects of a plot such as titles,
labels, spines, ticks, and legends for improved visual appeal and clarity.
This module should be imported and contains the following:
* set_titles_and_labels: Sets the titles and labels for the axes.
* customize_spines: Customizes the appearance of the subplot's spines.
* customize_ticks: Customizes the tick marks and labels on the axes.
"""
import matplotlib
import numpy as np
from typing import Dict, Union
# Define default styling parameters
DEFAULT_FONT_SIZE = 14
DEFAULT_LINE_WIDTH = 2.5
DEFAULT_COLOR = '0.2'
DEFAULT_FONT_WEIGHT = 'bold'
DEFAULT_SPINE_COLOR = {'bottom': DEFAULT_COLOR, 'left': DEFAULT_COLOR}
DEFAULT_TICK_PARAMS = {'axis': 'both', 'width': DEFAULT_LINE_WIDTH}
DEFAULT_LABEL_FONTDICT = {
'weight': 'bold', 'fontsize': DEFAULT_FONT_SIZE, 'color': DEFAULT_COLOR
}
DEFAULT_LEGEND_PROP = {"size": DEFAULT_FONT_SIZE}
DEFAULT_LEGEND_LINE_WIDTH = 3.5
def set_titles_and_labels(
ax: matplotlib.axes.Axes, title: str = None, xlabel: str = None,
ylabel: str = None,
fontdict: Dict[str, Union[str, float]] = DEFAULT_LABEL_FONTDICT
):
"""
Set titles and labels for the axes.
Args:
ax (matplotlib.axes.Axes): The axes object of the subplot.
title (str, optional): The title of the subplot. Defaults to None.
xlabel (str, optional): The label for the x-axis. Defaults to None.
ylabel (str, optional): The label for the y-axis. Defaults to None.
fontdict (Dict[str, Union[str, float]], optional): Dictionary for font
properties. Defaults to DEFAULT_LABEL_FONTDICT.
"""
if title:
ax.set_title(title, **fontdict)
if xlabel:
ax.set_xlabel(xlabel, **fontdict)
if ylabel:
ax.set_ylabel(ylabel, **fontdict)
def customize_spines(
ax: matplotlib.axes.Axes, spine_colors: Dict[str,
str] = DEFAULT_SPINE_COLOR,
linewidth: float = DEFAULT_LINE_WIDTH
):
"""
Customize the appearance of the subplot's spines.
Args:
ax (matplotlib.axes.Axes): The axes object of the subplot.
spine_colors (Dict[str, str], optional): Dictionary specifying colors for
each visible spine. Defaults to DEFAULT_SPINE_COLOR.
linewidth (float, optional): Width of the spines. Defaults to
DEFAULT_LINE_WIDTH.
"""
for spine in spine_colors:
ax.spines[spine].set_linewidth(linewidth)
ax.spines[spine].set_color(spine_colors[spine])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
def customize_ticks(
ax: matplotlib.axes.Axes, remove_ticks_lines: bool = False,
rotate_x_ticks: int = 0,
tick_params: Dict[str, Union[str, float]] = DEFAULT_TICK_PARAMS,
label_fontdict: Dict[str, Union[str, float]] = DEFAULT_LABEL_FONTDICT
):
"""
Customize the tick marks and labels.
Args:
ax (matplotlib.axes.Axes): The axes object of the subplot.
tick_params (Dict[str, Union[str, float]], optional): Parameters for tick
customization. Defaults to DEFAULT_TICK_PARAMS.
label_fontdict (Dict[str, Union[str, float]], optional): Font properties
for tick labels. Defaults to DEFAULT_LABEL_FONTDICT.
"""
if tick_params:
ax.tick_params(**tick_params)
if rotate_x_ticks:
for label in ax.get_xticklabels():
label.set_rotation(rotate_x_ticks)
if label_fontdict:
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontweight(label_fontdict.get('weight'))
label.set_fontsize(label_fontdict.get('fontsize'))
label.set_color(label_fontdict.get('color'))
if remove_ticks_lines:
ax.tick_params(axis='both', length=0)
def customize_colorbar(
cbar: matplotlib.colorbar.Colorbar, number_of_ticks: int = 5,
tick_params: Dict[str, Union[str, float]] = DEFAULT_TICK_PARAMS,
label_fontdict: Dict[str, Union[str, float]] = DEFAULT_LABEL_FONTDICT
):
"""
Customize the color bar in the plot.
Args:
cbar (matplotlib.colorbar.Colorbar): The color bar object of the subplot.
number_of_ticks (int): Number of ticks in the color bar including min, max.
tick_params (Dict[str, Union[str, float]], optional): Parameters for tick
customization.
label_fontdict (Dict[str, Union[str, float]], optional): Font properties
for tick labels.
"""
vmin, vmax = cbar.mappable.get_clim()
vmin_rounded = np.floor(vmin)
vmax_rounded = np.ceil(vmax)
ticks = np.linspace(vmin_rounded, vmax_rounded, number_of_ticks)
ticks = np.unique(np.concatenate(([vmin_rounded], ticks, [vmax_rounded])))
cbar.set_ticks(ticks)
cbar.set_ticklabels([f"{int(tick)}" for tick in ticks])
cbar.mappable.set_clim(vmin_rounded, vmax_rounded)
cbar.ax.tick_params(**tick_params)
for label in cbar.ax.get_yticklabels():
label.set_fontweight(label_fontdict.get('weight'))
label.set_fontsize(label_fontdict.get('fontsize'))
label.set_color(label_fontdict.get('color'))
for spine in cbar.ax.spines.values():
spine.set_linewidth(DEFAULT_LINE_WIDTH)
spine.set_color(DEFAULT_COLOR)