sacredfig.main.add_labels_to_axes()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
cc 2
nop 5
1
import string
2
3
import matplotlib.pyplot as plt
4
import matplotlib.transforms as mtrans
5
6
7
# Convert centimeters to inches
8
cm2in = 1 / 2.54
9
10
11
def add_labels_to_axes(fig: plt.Figure, axes: list[plt.Axes], fontsize: int = 9, Δx: float = -0.25, Δy: float = 0.1) -> None:
12
    """Add bold numbers (A, B, C, etc.) on the top left of every matplotlib axis."""
13
14
    for n, ax in enumerate(axes):
15
        trans_offset = mtrans.offset_copy(ax.transAxes, fig=fig, x=Δx, y=Δy, units="inches")
16
        ax.text(
17
            0,
18
            1,
19
            string.ascii_lowercase[n] + ".",
20
            fontweight=500,
21
            transform=trans_offset,
22
            size=fontsize,
23
        )
24
25
26
def restyle_axis(ax: plt.Axes, *, lineprops: dict = {}, artistprops: dict = {}) -> None:
27
    if lineprops:
28
        plt.setp(ax.lines, **lineprops)
29
30
    if artistprops:
31
        plt.setp(ax.patches, **artistprops)
32
        plt.setp(ax.collections, **artistprops)
33
34
35
# See <https://matplotlib.org/stable/tutorials/introductory/customizing.html> for all
36
# possible rcParams.
37
style = {
38
    # Lines
39
    "lines.linewidth": 1.0,  # line width in points
40
    "lines.linestyle": "-",  # solid line
41
    "lines.color": "b",  # has no affect on plot(); see axes.prop_cycle
42
    "lines.marker": "",  # the default marker
43
    "lines.markerfacecolor": "auto",  # the default markerfacecolor
44
    "lines.markeredgecolor": "auto",  # the default markeredgecolor
45
    "lines.markeredgewidth": 0.5,  # the line width around the marker symbol
46
    "lines.markersize": 6,  # markersize, in points
47
    "lines.dash_joinstyle": "round",  # miter|round|bevel
48
    "lines.dash_capstyle": "butt",  # butt|round|projecting
49
    "lines.solid_joinstyle": "round",  # miter|round|bevel
50
    "lines.solid_capstyle": "projecting",  # butt|round|projecting
51
    "lines.antialiased": True,  # render lines in antialiased (no jaggies)
52
    "lines.dashed_pattern": (6, 6),
53
    "lines.dashdot_pattern": (3, 5, 1, 5),
54
    "lines.dotted_pattern": (1, 3),
55
    "lines.scale_dashes": False,
56
    # Axes
57
    "axes.linewidth": 0.5,
58
    "axes.grid": True,
59
    "axes.titlesize": 7,
60
    "axes.labelpad": 2,
61
    "axes.labelsize": 7,
62
    ## Main tick parameters
63
    "xtick.labelsize": 5,
64
    "xtick.direction": "in",
65
    "ytick.labelsize": "5",
66
    "ytick.direction": "in",
67
    ## Major and minor ticks
68
    "xtick.major.width": 0.2,
69
    "xtick.major.size": 4,
70
    "xtick.major.pad": 2.5,
71
    "ytick.major.width": 0.2,
72
    "ytick.major.size": 4,
73
    "ytick.major.pad": 2.5,
74
    "xtick.minor.width": 0.1,
75
    "xtick.minor.size": 2,
76
    "xtick.minor.pad": 2.5,
77
    "ytick.minor.width": 0.1,
78
    "ytick.minor.size": 2,
79
    "ytick.minor.pad": 2.5,
80
    # Grid
81
    "grid.color": "#999999",
82
    "grid.linestyle": "-",
83
    "grid.linewidth": 0.1,
84
    # Remove legend frame
85
    "legend.frameon": False,
86
    # Typography
87
    "font.weight": "regular",
88
    "font.size": 7,
89
    "font.family": "sans-serif",
90
    "font.serif": "PT Serif, DejaVu Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif",
91
    "font.sans-serif": "Source Sans Pro, DejaVu Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif",
92
    "font.cursive": "Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive",
93
    "font.fantasy": "Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, fantasy",
94
    "font.monospace": "DejaVu Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace",
95
    ## Math fonts
96
    "mathtext.fontset": "custom",
97
    "mathtext.cal": "cursive",
98
    "mathtext.rm": "sans",
99
    "mathtext.tt": "monospace",
100
    "mathtext.it": "sans:italic",
101
    "mathtext.bf": "sans:bold",
102
    "mathtext.sf": "sans:serif",
103
    # Boxplots
104
    "boxplot.notch": False,
105
    "boxplot.vertical": True,
106
    "boxplot.whiskers": 1.5,
107
    "boxplot.bootstrap": None,
108
    "boxplot.patchartist": False,
109
    "boxplot.showmeans": False,
110
    "boxplot.showcaps": True,
111
    "boxplot.showbox": True,
112
    "boxplot.showfliers": True,
113
    "boxplot.meanline": False,
114
    "boxplot.flierprops.color": "black",
115
    "boxplot.flierprops.marker": "o",
116
    "boxplot.flierprops.markerfacecolor": "none",
117
    "boxplot.flierprops.markeredgecolor": "black",
118
    "boxplot.flierprops.markeredgewidth": 0.5,
119
    "boxplot.flierprops.markersize": 2,
120
    "boxplot.flierprops.linestyle": "none",
121
    "boxplot.flierprops.linewidth": 1.0,
122
    "boxplot.boxprops.color": "black",
123
    "boxplot.boxprops.linewidth": 0.5,
124
    "boxplot.boxprops.linestyle": "-",
125
    "boxplot.whiskerprops.color": "black",
126
    "boxplot.whiskerprops.linewidth": 0.5,
127
    "boxplot.whiskerprops.linestyle": "-",
128
    "boxplot.capprops.color": "black",
129
    "boxplot.capprops.linewidth": 0.5,
130
    "boxplot.capprops.linestyle": "-",
131
    "boxplot.medianprops.color": "black",
132
    "boxplot.medianprops.linewidth": 0.5,
133
    "boxplot.medianprops.linestyle": "-",
134
    "boxplot.meanprops.color": "C2",
135
    "boxplot.meanprops.marker": "^",
136
    "boxplot.meanprops.markerfacecolor": "C2",
137
    "boxplot.meanprops.markeredgecolor": "C2",
138
    "boxplot.meanprops.markersize": 6,
139
    "boxplot.meanprops.linestyle": "--",
140
    "boxplot.meanprops.linewidth": 1.0,
141
}
142