py2ls 0.1.6.0__py3-none-any.whl → 0.1.6.2__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.
- py2ls/.DS_Store +0 -0
- py2ls/.git/logs/refs/remotes/origin/HEAD +12 -0
- py2ls/ips.py +38 -5
- py2ls/plot/catplot.py +437 -0
- py2ls/plot/figsets.py +372 -0
- py2ls/plot/get_color.py +68 -0
- py2ls/plot/stdshade.py +227 -0
- {py2ls-0.1.6.0.dist-info → py2ls-0.1.6.2.dist-info}/METADATA +1 -1
- {py2ls-0.1.6.0.dist-info → py2ls-0.1.6.2.dist-info}/RECORD +10 -7
- {py2ls-0.1.6.0.dist-info → py2ls-0.1.6.2.dist-info}/WHEEL +1 -1
- py2ls/plot.py +0 -665
py2ls/plot.py
DELETED
@@ -1,665 +0,0 @@
|
|
1
|
-
from scipy.signal import savgol_filter
|
2
|
-
import numpy as np
|
3
|
-
import matplotlib
|
4
|
-
import matplotlib.pyplot as plt
|
5
|
-
import matplotlib.ticker as tck
|
6
|
-
import seaborn as sns
|
7
|
-
from cycler import cycler
|
8
|
-
|
9
|
-
|
10
|
-
def read_mplstyle(style_file):
|
11
|
-
# Load the style file
|
12
|
-
plt.style.use(style_file)
|
13
|
-
|
14
|
-
# Get the current style properties
|
15
|
-
style_dict = plt.rcParams
|
16
|
-
|
17
|
-
# Convert to dictionary
|
18
|
-
style_dict = dict(style_dict)
|
19
|
-
# Print the style dictionary
|
20
|
-
for i, j in style_dict.items():
|
21
|
-
print(f"\n{i}::::{j}")
|
22
|
-
return style_dict
|
23
|
-
# #example usage:
|
24
|
-
# style_file = "/ std-colors.mplstyle"
|
25
|
-
# style_dict = read_mplstyle(style_file)
|
26
|
-
|
27
|
-
|
28
|
-
# set up the colorlist, give the number, or the colormap's name
|
29
|
-
def get_color(n=1, cmap="auto", by="start"):
|
30
|
-
# Extract the colormap as a list
|
31
|
-
def cmap2hex(cmap_name):
|
32
|
-
cmap_ = matplotlib.pyplot.get_cmap(cmap_name)
|
33
|
-
colors = [cmap_(i) for i in range(cmap_.N)]
|
34
|
-
return [matplotlib.colors.rgb2hex(color) for color in colors]
|
35
|
-
# usage: clist = cmap2hex("viridis")
|
36
|
-
# cycle times, total number is n (defaultn=10)
|
37
|
-
def cycle2list(colorlist, n=10):
|
38
|
-
cycler_ = cycler(tmp=colorlist)
|
39
|
-
clist = []
|
40
|
-
for i, c_ in zip(range(n), cycler_()):
|
41
|
-
clist.append(c_["tmp"])
|
42
|
-
if i > n:
|
43
|
-
break
|
44
|
-
return clist
|
45
|
-
def hue2rgb(hex_colors):
|
46
|
-
def hex_to_rgb(hex_color):
|
47
|
-
"""Converts a hexadecimal color code to RGB values."""
|
48
|
-
if hex_colors.startswith("#"):
|
49
|
-
hex_color = hex_color.lstrip("#")
|
50
|
-
return tuple(int(hex_color[i : i + 2], 16) / 255.0 for i in (0, 2, 4))
|
51
|
-
if isinstance(hex_colors, str):
|
52
|
-
return hex_to_rgb(hex_colors)
|
53
|
-
elif isinstance(hex_colors, (list)):
|
54
|
-
"""Converts a list of hexadecimal color codes to a list of RGB values."""
|
55
|
-
rgb_values = [hex_to_rgb(hex_color) for hex_color in hex_colors]
|
56
|
-
return rgb_values
|
57
|
-
if "aut" in cmap:
|
58
|
-
colorlist = [
|
59
|
-
"#474747",
|
60
|
-
"#FF2C00",
|
61
|
-
"#0C5DA5",
|
62
|
-
"#845B97",
|
63
|
-
"#58BBCC",
|
64
|
-
"#FF9500",
|
65
|
-
"#D57DBE",
|
66
|
-
]
|
67
|
-
else:
|
68
|
-
colorlist = cmap2hex(cmap)
|
69
|
-
if "st" in by.lower() or "be" in by.lower():
|
70
|
-
# cycle it
|
71
|
-
clist = cycle2list(colorlist, n=n)
|
72
|
-
if "l" in by.lower() or "p" in by.lower():
|
73
|
-
clist = []
|
74
|
-
[
|
75
|
-
clist.append(colorlist[i])
|
76
|
-
for i in [int(i) for i in np.linspace(0, len(colorlist) - 1, n)]
|
77
|
-
]
|
78
|
-
|
79
|
-
return clist # a color list
|
80
|
-
# example usage: clist = get_color(4,cmap="auto", by="start") # get_color(4, cmap="hot", by="linspace")
|
81
|
-
|
82
|
-
"""
|
83
|
-
# n = 7
|
84
|
-
# clist = get_color(n, cmap="auto", by="linspace") # get_color(100)
|
85
|
-
# plt.figure(figsize=[8, 5], dpi=100)
|
86
|
-
# x = np.linspace(0, 2 * np.pi, 50) * 100
|
87
|
-
# y = np.sin(x)
|
88
|
-
# for i in range(1, n + 1):
|
89
|
-
# plt.plot(x, y + i, c=clist[i - 1], lw=5, label=str(i))
|
90
|
-
# plt.legend()
|
91
|
-
# plt.ylim(-2, 20)
|
92
|
-
# figsets(plt.gca(), {"style": "whitegrid"}) """
|
93
|
-
|
94
|
-
def stdshade(ax=None,*args, **kwargs):
|
95
|
-
# Separate kws_line and kws_fill if necessary
|
96
|
-
kws_line = kwargs.pop('kws_line', {})
|
97
|
-
kws_fill = kwargs.pop('kws_fill', {})
|
98
|
-
|
99
|
-
# Merge kws_line and kws_fill into kwargs
|
100
|
-
kwargs.update(kws_line)
|
101
|
-
kwargs.update(kws_fill)
|
102
|
-
def str2list(str_):
|
103
|
-
l = []
|
104
|
-
[l.append(x) for x in str_]
|
105
|
-
return l
|
106
|
-
def hue2rgb(hex_colors):
|
107
|
-
def hex_to_rgb(hex_color):
|
108
|
-
"""Converts a hexadecimal color code to RGB values."""
|
109
|
-
if hex_colors.startswith("#"):
|
110
|
-
hex_color = hex_color.lstrip("#")
|
111
|
-
return tuple(int(hex_color[i : i + 2], 16) / 255.0 for i in (0, 2, 4))
|
112
|
-
if isinstance(hex_colors, str):
|
113
|
-
return hex_to_rgb(hex_colors)
|
114
|
-
elif isinstance(hex_colors, (list)):
|
115
|
-
"""Converts a list of hexadecimal color codes to a list of RGB values."""
|
116
|
-
rgb_values = [hex_to_rgb(hex_color) for hex_color in hex_colors]
|
117
|
-
return rgb_values
|
118
|
-
if (
|
119
|
-
isinstance(ax, np.ndarray)
|
120
|
-
and ax.ndim == 2
|
121
|
-
and min(ax.shape) > 1
|
122
|
-
and max(ax.shape) > 1
|
123
|
-
):
|
124
|
-
y = ax
|
125
|
-
ax = plt.gca()
|
126
|
-
if ax is None:
|
127
|
-
ax = plt.gca()
|
128
|
-
alpha = 0.5
|
129
|
-
acolor = "k"
|
130
|
-
paraStdSem = "sem"
|
131
|
-
plotStyle = "-"
|
132
|
-
plotMarker = "none"
|
133
|
-
smth = 1
|
134
|
-
l_c_one = ["r", "g", "b", "m", "c", "y", "k", "w"]
|
135
|
-
l_style2 = ["--", "-."]
|
136
|
-
l_style1 = ["-", ":"]
|
137
|
-
l_mark = ["o", "+", "*", ".", "x", "_", "|", "s", "d", "^", "v", ">", "<", "p", "h"]
|
138
|
-
# Check each argument
|
139
|
-
for iarg in range(len(args)):
|
140
|
-
if (
|
141
|
-
isinstance(args[iarg], np.ndarray)
|
142
|
-
and args[iarg].ndim == 2
|
143
|
-
and min(args[iarg].shape) > 1
|
144
|
-
and max(args[iarg].shape) > 1
|
145
|
-
):
|
146
|
-
y = args[iarg]
|
147
|
-
# Except y, continuous data is 'F'
|
148
|
-
if (isinstance(args[iarg], np.ndarray) and args[iarg].ndim == 1) or isinstance(
|
149
|
-
args[iarg], range
|
150
|
-
):
|
151
|
-
x = args[iarg]
|
152
|
-
if isinstance(x, range):
|
153
|
-
x = np.arange(start=x.start, stop=x.stop, step=x.step)
|
154
|
-
# Only one number( 0~1), 'alpha' / color
|
155
|
-
if isinstance(args[iarg], (int, float)):
|
156
|
-
if np.size(args[iarg]) == 1 and 0 <= args[iarg] <= 1:
|
157
|
-
alpha = args[iarg]
|
158
|
-
if isinstance(args[iarg], (list, tuple)) and np.size(args[iarg]) == 3:
|
159
|
-
acolor = args[iarg]
|
160
|
-
acolor = tuple(acolor) if isinstance(acolor, list) else acolor
|
161
|
-
# Color / plotStyle /
|
162
|
-
if (
|
163
|
-
isinstance(args[iarg], str)
|
164
|
-
and len(args[iarg]) == 1
|
165
|
-
and args[iarg] in l_c_one
|
166
|
-
):
|
167
|
-
acolor = args[iarg]
|
168
|
-
else:
|
169
|
-
if isinstance(args[iarg], str):
|
170
|
-
if args[iarg] in ["sem", "std"]:
|
171
|
-
paraStdSem = args[iarg]
|
172
|
-
if args[iarg].startswith("#"):
|
173
|
-
acolor=hue2rgb(args[iarg])
|
174
|
-
if str2list(args[iarg])[0] in l_c_one:
|
175
|
-
if len(args[iarg]) == 3:
|
176
|
-
k = [i for i in str2list(args[iarg]) if i in l_c_one]
|
177
|
-
if k != []:
|
178
|
-
acolor = k[0]
|
179
|
-
st = [i for i in l_style2 if i in args[iarg]]
|
180
|
-
if st != []:
|
181
|
-
plotStyle = st[0]
|
182
|
-
elif len(args[iarg]) == 2:
|
183
|
-
k = [i for i in str2list(args[iarg]) if i in l_c_one]
|
184
|
-
if k != []:
|
185
|
-
acolor = k[0]
|
186
|
-
mk = [i for i in str2list(args[iarg]) if i in l_mark]
|
187
|
-
if mk != []:
|
188
|
-
plotMarker = mk[0]
|
189
|
-
st = [i for i in l_style1 if i in args[iarg]]
|
190
|
-
if st != []:
|
191
|
-
plotStyle = st[0]
|
192
|
-
if len(args[iarg]) == 1:
|
193
|
-
k = [i for i in str2list(args[iarg]) if i in l_c_one]
|
194
|
-
if k != []:
|
195
|
-
acolor = k[0]
|
196
|
-
mk = [i for i in str2list(args[iarg]) if i in l_mark]
|
197
|
-
if mk != []:
|
198
|
-
plotMarker = mk[0]
|
199
|
-
st = [i for i in l_style1 if i in args[iarg]]
|
200
|
-
if st != []:
|
201
|
-
plotStyle = st[0]
|
202
|
-
if len(args[iarg]) == 2:
|
203
|
-
st = [i for i in l_style2 if i in args[iarg]]
|
204
|
-
if st != []:
|
205
|
-
plotStyle = st[0]
|
206
|
-
# smth
|
207
|
-
if (
|
208
|
-
isinstance(args[iarg], (int, float))
|
209
|
-
and np.size(args[iarg]) == 1
|
210
|
-
and args[iarg] >= 1
|
211
|
-
):
|
212
|
-
smth = args[iarg]
|
213
|
-
smth = kwargs.get('smth', smth)
|
214
|
-
if "x" not in locals() or x is None:
|
215
|
-
x = np.arange(1, y.shape[1] + 1)
|
216
|
-
elif len(x) < y.shape[1]:
|
217
|
-
y = y[:, x]
|
218
|
-
nRow = y.shape[0]
|
219
|
-
nCol = y.shape[1]
|
220
|
-
print(f"y was corrected, please confirm that {nRow} row, {nCol} col")
|
221
|
-
else:
|
222
|
-
x = np.arange(1, y.shape[1] + 1)
|
223
|
-
|
224
|
-
if x.shape[0] != 1:
|
225
|
-
x = x.T
|
226
|
-
yMean = np.nanmean(y, axis=0)
|
227
|
-
if smth > 1:
|
228
|
-
yMean = savgol_filter(np.nanmean(y, axis=0), smth, 1)
|
229
|
-
else:
|
230
|
-
yMean = np.nanmean(y, axis=0)
|
231
|
-
if paraStdSem == "sem":
|
232
|
-
if smth > 1:
|
233
|
-
wings = savgol_filter(np.nanstd(y, axis=0) / np.sqrt(y.shape[0]), smth, 1)
|
234
|
-
else:
|
235
|
-
wings = np.nanstd(y, axis=0) / np.sqrt(y.shape[0])
|
236
|
-
elif paraStdSem == "std":
|
237
|
-
if smth > 1:
|
238
|
-
wings = savgol_filter(np.nanstd(y, axis=0), smth, 1)
|
239
|
-
else:
|
240
|
-
wings = np.nanstd(y, axis=0)
|
241
|
-
|
242
|
-
# fill_kws = kwargs.get('fill_kws', {})
|
243
|
-
# line_kws = kwargs.get('line_kws', {})
|
244
|
-
|
245
|
-
# setting form kwargs
|
246
|
-
lw = kwargs.get('lw', 1.5)
|
247
|
-
ls= kwargs.get('ls', plotStyle)
|
248
|
-
marker=kwargs.get("marker",plotMarker)
|
249
|
-
label=kwargs.get("label",None)
|
250
|
-
label_line = kwargs.get("label_line",None)
|
251
|
-
label_fill = kwargs.get('label_fill',None)
|
252
|
-
alpha=kwargs.get('alpha',alpha)
|
253
|
-
color=kwargs.get('color', acolor)
|
254
|
-
if not label_line and label:
|
255
|
-
label_line = label
|
256
|
-
kwargs['lw'] = lw
|
257
|
-
kwargs['ls'] = ls
|
258
|
-
kwargs['label_line'] = label_line
|
259
|
-
kwargs['label_fill'] = label_fill
|
260
|
-
|
261
|
-
# set kws_line
|
262
|
-
if 'color' not in kws_line:
|
263
|
-
kws_line['color']=color
|
264
|
-
if 'lw' not in kws_line:
|
265
|
-
kws_line['lw']=lw
|
266
|
-
if 'ls' not in kws_line:
|
267
|
-
kws_line['ls']=ls
|
268
|
-
if 'marker' not in kws_line:
|
269
|
-
kws_line['marker']=marker
|
270
|
-
if 'label' not in kws_line:
|
271
|
-
kws_line['label']=label_line
|
272
|
-
|
273
|
-
# set kws_line
|
274
|
-
if 'color' not in kws_fill:
|
275
|
-
kws_fill['color']=color
|
276
|
-
if 'alpha' not in kws_fill:
|
277
|
-
kws_fill['alpha']=alpha
|
278
|
-
if 'lw' not in kws_fill:
|
279
|
-
kws_fill['lw']=0
|
280
|
-
if 'label' not in kws_fill:
|
281
|
-
kws_fill['label']=label_fill
|
282
|
-
|
283
|
-
fill = ax.fill_between(x, yMean + wings, yMean - wings, **kws_fill)
|
284
|
-
line = ax.plot(x, yMean, **kws_line)
|
285
|
-
return line[0], fill
|
286
|
-
|
287
|
-
|
288
|
-
"""
|
289
|
-
########## Usage 1 ##########
|
290
|
-
plot.stdshade(data,
|
291
|
-
'b',
|
292
|
-
':',
|
293
|
-
'd',
|
294
|
-
0.1,
|
295
|
-
4,
|
296
|
-
label='ddd',
|
297
|
-
label_line='label_line',
|
298
|
-
label_fill="label-fill")
|
299
|
-
plt.legend()
|
300
|
-
|
301
|
-
########## Usage 2 ##########
|
302
|
-
plot.stdshade(data,
|
303
|
-
'm-',
|
304
|
-
alpha=0.1,
|
305
|
-
lw=2,
|
306
|
-
ls=':',
|
307
|
-
marker='d',
|
308
|
-
color='b',
|
309
|
-
smth=4,
|
310
|
-
label='ddd',
|
311
|
-
label_line='label_line',
|
312
|
-
label_fill="label-fill")
|
313
|
-
plt.legend()
|
314
|
-
|
315
|
-
"""
|
316
|
-
|
317
|
-
def adjust_spines(ax=None, spines=['left', 'bottom'],distance=2):
|
318
|
-
if ax is None:
|
319
|
-
ax = plt.gca()
|
320
|
-
for loc, spine in ax.spines.items():
|
321
|
-
if loc in spines:
|
322
|
-
spine.set_position(('outward', distance)) # outward by 2 points
|
323
|
-
# spine.set_smart_bounds(True)
|
324
|
-
else:
|
325
|
-
spine.set_color('none') # don't draw spine
|
326
|
-
# turn off ticks where there is no spine
|
327
|
-
if 'left' in spines:
|
328
|
-
ax.yaxis.set_ticks_position('left')
|
329
|
-
else:
|
330
|
-
ax.yaxis.set_ticks([])
|
331
|
-
if 'bottom' in spines:
|
332
|
-
ax.xaxis.set_ticks_position('bottom')
|
333
|
-
else:
|
334
|
-
# no xaxis ticks
|
335
|
-
ax.xaxis.set_ticks([])
|
336
|
-
# And then plot the data:
|
337
|
-
def figsets(*args):
|
338
|
-
fig = plt.gcf()
|
339
|
-
fontsize = 11
|
340
|
-
fontname = "Arial"
|
341
|
-
sns_themes = ["white", "whitegrid", "dark", "darkgrid", "ticks"]
|
342
|
-
sns_contexts = ["notebook", "talk", "poster"] # now available "paper"
|
343
|
-
scienceplots_styles = ["science","nature",
|
344
|
-
"scatter","ieee","no-latex","std-colors","high-vis","bright","dark_background","science",
|
345
|
-
"high-vis","vibrant","muted","retro","grid","high-contrast","light","cjk-tc-font","cjk-kr-font",
|
346
|
-
]
|
347
|
-
def sets_priority(ax,key, value):
|
348
|
-
if ("fo" in key) and (("size" in key) or ("sz" in key)):
|
349
|
-
fontsize=value
|
350
|
-
plt.rcParams.update({"font.size": value})
|
351
|
-
# style
|
352
|
-
if "st" in key.lower() or "th" in key.lower():
|
353
|
-
if isinstance(value, str):
|
354
|
-
if (value in plt.style.available) or (value in scienceplots_styles):
|
355
|
-
plt.style.use(value)
|
356
|
-
elif value in sns_themes:
|
357
|
-
sns.set_style(value)
|
358
|
-
elif value in sns_contexts:
|
359
|
-
sns.set_context(value)
|
360
|
-
else:
|
361
|
-
print(
|
362
|
-
f"\nWarning\n'{value}' is not a plt.style,select on below:\n{plt.style.available+sns_themes+sns_contexts+scienceplots_styles}"
|
363
|
-
)
|
364
|
-
if isinstance(value, list):
|
365
|
-
for i in value:
|
366
|
-
if (i in plt.style.available) or (i in scienceplots_styles):
|
367
|
-
plt.style.use(i)
|
368
|
-
elif i in sns_themes:
|
369
|
-
sns.set_style(i)
|
370
|
-
elif i in sns_contexts:
|
371
|
-
sns.set_context(i)
|
372
|
-
else:
|
373
|
-
print(
|
374
|
-
f"\nWarning\n'{i}' is not a plt.style,select on below:\n{plt.style.available+sns_themes+sns_contexts+scienceplots_styles}"
|
375
|
-
)
|
376
|
-
if "la" in key.lower():
|
377
|
-
if "loc" in key.lower() or "po" in key.lower():
|
378
|
-
for i in value:
|
379
|
-
if "l" in i.lower():
|
380
|
-
ax.yaxis.set_label_position("left")
|
381
|
-
if "r" in i.lower():
|
382
|
-
ax.yaxis.set_label_position("right")
|
383
|
-
if "t" in i.lower():
|
384
|
-
ax.xaxis.set_label_position("top")
|
385
|
-
if "b" in i.lower():
|
386
|
-
ax.xaxis.set_label_position("bottom")
|
387
|
-
if ("x" in key.lower()) and (
|
388
|
-
"tic" not in key.lower() and "tk" not in key.lower()
|
389
|
-
):
|
390
|
-
ax.set_xlabel(value, fontname=fontname)
|
391
|
-
if ("y" in key.lower()) and (
|
392
|
-
"tic" not in key.lower() and "tk" not in key.lower()
|
393
|
-
):
|
394
|
-
ax.set_ylabel(value, fontname=fontname)
|
395
|
-
if ("z" in key.lower()) and (
|
396
|
-
"tic" not in key.lower() and "tk" not in key.lower()
|
397
|
-
):
|
398
|
-
ax.set_zlabel(value, fontname=fontname)
|
399
|
-
# tick location
|
400
|
-
if "tic" in key.lower() or "tk" in key.lower():
|
401
|
-
if ("loc" in key.lower()) or ("po" in key.lower()):
|
402
|
-
if isinstance(value,str):
|
403
|
-
value=[value]
|
404
|
-
if isinstance(value, list):
|
405
|
-
loc = []
|
406
|
-
for i in value:
|
407
|
-
if ("l" in i.lower()) and ("a" not in i.lower()):
|
408
|
-
ax.yaxis.set_ticks_position("left")
|
409
|
-
if "r" in i.lower():
|
410
|
-
ax.yaxis.set_ticks_position("right")
|
411
|
-
if "t" in i.lower():
|
412
|
-
ax.xaxis.set_ticks_position("top")
|
413
|
-
if "b" in i.lower():
|
414
|
-
ax.xaxis.set_ticks_position("bottom")
|
415
|
-
if i.lower() in ["a", "both", "all", "al", ":"]:
|
416
|
-
ax.xaxis.set_ticks_position("both")
|
417
|
-
ax.yaxis.set_ticks_position("both")
|
418
|
-
if i.lower() in ["xnone",'xoff',"none"]:
|
419
|
-
ax.xaxis.set_ticks_position("none")
|
420
|
-
if i.lower() in ["ynone",'yoff','none']:
|
421
|
-
ax.yaxis.set_ticks_position("none")
|
422
|
-
# ticks / labels
|
423
|
-
elif "x" in key.lower():
|
424
|
-
if value is None:
|
425
|
-
value=[]
|
426
|
-
if "la" not in key.lower():
|
427
|
-
ax.set_xticks(value)
|
428
|
-
if "la" in key.lower():
|
429
|
-
ax.set_xticklabels(value)
|
430
|
-
elif "y" in key.lower():
|
431
|
-
if value is None:
|
432
|
-
value=[]
|
433
|
-
if "la" not in key.lower():
|
434
|
-
ax.set_yticks(value)
|
435
|
-
if "la" in key.lower():
|
436
|
-
ax.set_yticklabels(value)
|
437
|
-
elif "z" in key.lower():
|
438
|
-
if value is None:
|
439
|
-
value=[]
|
440
|
-
if "la" not in key.lower():
|
441
|
-
ax.set_zticks(value)
|
442
|
-
if "la" in key.lower():
|
443
|
-
ax.set_zticklabels(value)
|
444
|
-
# rotation
|
445
|
-
if "angle" in key.lower() or ("rot" in key.lower()):
|
446
|
-
if "x" in key.lower():
|
447
|
-
ax.tick_params(axis="x", rotation=value)
|
448
|
-
if "y" in key.lower():
|
449
|
-
ax.tick_params(axis="y", rotation=value)
|
450
|
-
|
451
|
-
if "bo" in key in key: # and ("p" in key or "l" in key):
|
452
|
-
# print("'ticks' style is recommended")
|
453
|
-
if isinstance(value, (str, list)):
|
454
|
-
locations = []
|
455
|
-
for i in value:
|
456
|
-
if "l" in i.lower():
|
457
|
-
locations.append("left")
|
458
|
-
if "r" in i.lower():
|
459
|
-
locations.append("right")
|
460
|
-
if "t" in i.lower():
|
461
|
-
locations.append("top")
|
462
|
-
if "b" in i.lower():
|
463
|
-
locations.append("bottom")
|
464
|
-
if i.lower() in ["a", "both", "all", "al", ":"]:
|
465
|
-
[
|
466
|
-
locations.append(x)
|
467
|
-
for x in ["left", "right", "top", "bottom"]
|
468
|
-
]
|
469
|
-
for i in value:
|
470
|
-
if i.lower() in "none":
|
471
|
-
locations = []
|
472
|
-
# check spines
|
473
|
-
for loc, spi in ax.spines.items():
|
474
|
-
if loc in locations:
|
475
|
-
spi.set_position(("outward", 0))
|
476
|
-
else:
|
477
|
-
spi.set_color("none") # no spine
|
478
|
-
if key == "tick" or key == "ticks" or key == "ticks_para":
|
479
|
-
if isinstance(value, dict):
|
480
|
-
for k, val in value.items():
|
481
|
-
if "wh" in k.lower():
|
482
|
-
ax.tick_params(
|
483
|
-
which=val
|
484
|
-
) # {'major', 'minor', 'both'}, default: 'major'
|
485
|
-
elif "dir" in k.lower():
|
486
|
-
ax.tick_params(direction=val) # {'in', 'out', 'inout'}
|
487
|
-
elif "len" in k.lower():
|
488
|
-
ax.tick_params(length=val)
|
489
|
-
elif ("wid" in k.lower()) or ("wd" in k.lower()):
|
490
|
-
ax.tick_params(width=val)
|
491
|
-
elif "ax" in k.lower():
|
492
|
-
ax.tick_params(axis=val) # {'x', 'y', 'both'}, default: 'both'
|
493
|
-
elif ("c" in k.lower()) and ("ect" not in k.lower()):
|
494
|
-
ax.tick_params(colors=val) # Tick color.
|
495
|
-
elif "pad" in k.lower():
|
496
|
-
ax.tick_params(
|
497
|
-
pad=val
|
498
|
-
) # float, distance in points between tick and label
|
499
|
-
elif (
|
500
|
-
("lab" in k.lower())
|
501
|
-
and ("s" in k.lower())
|
502
|
-
and ("z" in k.lower())
|
503
|
-
):
|
504
|
-
ax.tick_params(
|
505
|
-
labelsize=val
|
506
|
-
) # float, distance in points between tick and label
|
507
|
-
|
508
|
-
if "mi" in key.lower() and "tic" in key.lower():
|
509
|
-
if "x" in value.lower() or "x" in key.lower():
|
510
|
-
ax.xaxis.set_minor_locator(tck.AutoMinorLocator()) # ax.minorticks_on()
|
511
|
-
if "y" in value.lower() or "y" in key.lower():
|
512
|
-
ax.yaxis.set_minor_locator(
|
513
|
-
tck.AutoMinorLocator()
|
514
|
-
) # ax.minorticks_off()
|
515
|
-
if value.lower() in ["both", ":", "all", "a", "b", "on"]:
|
516
|
-
ax.minorticks_on()
|
517
|
-
if key == "colormap" or key == "cmap":
|
518
|
-
plt.set_cmap(value)
|
519
|
-
def sets_small(ax,key, value):
|
520
|
-
if key == "figsize":
|
521
|
-
pass
|
522
|
-
if key == "xlim":
|
523
|
-
ax.set_xlim(value)
|
524
|
-
if key == "ylim":
|
525
|
-
ax.set_ylim(value)
|
526
|
-
if key == "zlim":
|
527
|
-
ax.set_zlim(value)
|
528
|
-
if "sc" in key.lower():
|
529
|
-
if "x" in key.lower():
|
530
|
-
ax.set_xscale(value)
|
531
|
-
if "y" in key.lower():
|
532
|
-
ax.set_yscale(value)
|
533
|
-
if "z" in key.lower():
|
534
|
-
ax.set_zscale(value)
|
535
|
-
if key == "grid":
|
536
|
-
if isinstance(value, dict):
|
537
|
-
for k, val in value.items():
|
538
|
-
if "wh" in k.lower():
|
539
|
-
ax.grid(
|
540
|
-
which=val
|
541
|
-
) # {'major', 'minor', 'both'}, default: 'major'
|
542
|
-
elif "ax" in k.lower():
|
543
|
-
ax.grid(axis=val) # {'x', 'y', 'both'}, default: 'both'
|
544
|
-
elif ("c" in k.lower()) and ("ect" not in k.lower()):
|
545
|
-
ax.grid(color=val) # Tick color.
|
546
|
-
elif "l" in k.lower() and ("s" in k.lower()):
|
547
|
-
ax.grid(linestyle=val)
|
548
|
-
elif "l" in k.lower() and ("w" in k.lower()):
|
549
|
-
ax.grid(linewidth=val)
|
550
|
-
elif "al" in k.lower():
|
551
|
-
ax.grid(alpha=val)
|
552
|
-
else:
|
553
|
-
if value == "on" or value is True:
|
554
|
-
ax.grid(visible=True)
|
555
|
-
elif value == "off" or value is False:
|
556
|
-
ax.grid(visible=False)
|
557
|
-
if "tit" in key.lower():
|
558
|
-
if "sup" in key.lower():
|
559
|
-
plt.suptitle(value)
|
560
|
-
else:
|
561
|
-
ax.set_title(value)
|
562
|
-
if key.lower() in ["spine", "adjust", "ad", "sp", "spi", "adj","spines"]:
|
563
|
-
if isinstance(value, bool) or (value in ["go", "do", "ja", "yes"]):
|
564
|
-
if value:
|
565
|
-
adjust_spines(ax) # dafault distance=2
|
566
|
-
if isinstance(value, (float, int)):
|
567
|
-
adjust_spines(ax=ax, distance=value)
|
568
|
-
if "c" in key.lower() and ("sp" in key.lower() or "ax" in key.lower()):
|
569
|
-
for loc, spi in ax.spines.items():
|
570
|
-
spi.set_color(value)
|
571
|
-
|
572
|
-
for arg in args:
|
573
|
-
if isinstance(arg,matplotlib.axes._axes.Axes):
|
574
|
-
ax=arg
|
575
|
-
args=args[1:]
|
576
|
-
if 'ax' not in locals():
|
577
|
-
ax=plt.gca()
|
578
|
-
|
579
|
-
for arg in args:
|
580
|
-
if isinstance(arg, dict):
|
581
|
-
for k, val in arg.items():
|
582
|
-
sets_priority(ax,k, val)
|
583
|
-
for k, val in arg.items():
|
584
|
-
sets_small(ax,k, val)
|
585
|
-
else:
|
586
|
-
Nargin = len(args) // 2
|
587
|
-
ax.labelFontSizeMultiplier = 1
|
588
|
-
ax.titleFontSizeMultiplier = 1
|
589
|
-
ax.set_facecolor("w")
|
590
|
-
|
591
|
-
for ip in range(Nargin):
|
592
|
-
key = args[ip * 2].lower()
|
593
|
-
value = args[ip * 2 + 1]
|
594
|
-
sets_priority(ax,key, value)
|
595
|
-
for ip in range(Nargin):
|
596
|
-
key = args[ip * 2].lower()
|
597
|
-
value = args[ip * 2 + 1]
|
598
|
-
sets_small(ax,key, value)
|
599
|
-
colors = [
|
600
|
-
"#474747",
|
601
|
-
"#FF2C00",
|
602
|
-
"#0C5DA5",
|
603
|
-
"#845B97",
|
604
|
-
"#58BBCC",
|
605
|
-
"#FF9500",
|
606
|
-
"#D57DBE",
|
607
|
-
]
|
608
|
-
matplotlib.rcParams["axes.prop_cycle"] = cycler(color=colors)
|
609
|
-
if len(fig.get_axes()) > 1:
|
610
|
-
plt.tight_layout()
|
611
|
-
plt.gcf().align_labels()
|
612
|
-
|
613
|
-
|
614
|
-
def figsave(*args,dpi=300):
|
615
|
-
dir_save = None
|
616
|
-
fname = None
|
617
|
-
for arg in args:
|
618
|
-
if isinstance(arg, str):
|
619
|
-
if '/' in arg or '\\' in arg:
|
620
|
-
dir_save = arg
|
621
|
-
elif '/' not in arg and '\\' not in arg:
|
622
|
-
fname = arg
|
623
|
-
# Backup original values
|
624
|
-
if '/' in dir_save:
|
625
|
-
if dir_save[-1] != '/':
|
626
|
-
dir_save = dir_save + '/'
|
627
|
-
elif '\\' in dir_save:
|
628
|
-
if dir_save[-1] != '\\':
|
629
|
-
dir_save = dir_save + '\\'
|
630
|
-
else:
|
631
|
-
raise ValueError('Check the Path of dir_save Directory')
|
632
|
-
ftype = fname.split('.')[-1]
|
633
|
-
if len(fname.split('.')) == 1:
|
634
|
-
ftype = 'nofmt'
|
635
|
-
fname = dir_save + fname + '.' + ftype
|
636
|
-
else:
|
637
|
-
fname = dir_save + fname
|
638
|
-
# Save figure based on file type
|
639
|
-
if ftype.lower() == 'eps':
|
640
|
-
plt.savefig(fname, format='eps', bbox_inches='tight')
|
641
|
-
plt.savefig(fname.replace('.eps', '.pdf'),
|
642
|
-
format='pdf', bbox_inches='tight',dpi=dpi)
|
643
|
-
elif ftype.lower() == 'nofmt': # default: both "tif" and "pdf"
|
644
|
-
fname_corr=fname.replace('nofmt','pdf')
|
645
|
-
plt.savefig(fname_corr, format='pdf', bbox_inches='tight',dpi=dpi)
|
646
|
-
fname=fname.replace('nofmt','tif')
|
647
|
-
plt.savefig(fname, format='tiff', dpi=dpi, bbox_inches='tight')
|
648
|
-
print(f"default saving filetype: both 'tif' and 'pdf")
|
649
|
-
elif ftype.lower() == 'pdf':
|
650
|
-
plt.savefig(fname, format='pdf', bbox_inches='tight',dpi=dpi)
|
651
|
-
elif ftype.lower() in ['jpg', 'jpeg']:
|
652
|
-
plt.savefig(fname, format='jpeg', dpi=dpi, bbox_inches='tight')
|
653
|
-
elif ftype.lower() == 'png':
|
654
|
-
plt.savefig(fname, format='png', dpi=dpi,
|
655
|
-
bbox_inches='tight', transparent=True)
|
656
|
-
elif ftype.lower() in ['tiff', 'tif']:
|
657
|
-
plt.savefig(fname, format='tiff', dpi=dpi, bbox_inches='tight')
|
658
|
-
elif ftype.lower() == 'emf':
|
659
|
-
plt.savefig(fname, format='emf', dpi=dpi, bbox_inches='tight')
|
660
|
-
elif ftype.lower() == 'fig':
|
661
|
-
plt.savefig(fname, format='pdf', bbox_inches='tight',dpi=dpi)
|
662
|
-
print(f'\nSaved @: dpi={dpi}\n{fname}')
|
663
|
-
|
664
|
-
def get_cmap():
|
665
|
-
return plt.colormaps()
|