• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

APN-Pucky / smpl / 15535986057

09 Jun 2025 01:41PM UTC coverage: 89.04% (+0.1%) from 88.914%
15535986057

push

github

web-flow
Fix ci (#351)

* try it

* comment warnings for now

* hatch exclude is back

* foramt

* Fix ci

* fix it

5 of 5 new or added lines in 2 files covered. (100.0%)

29 existing lines in 3 files now uncovered.

1568 of 1761 relevant lines covered (89.04%)

4.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.6
/smpl/plot.py
1
"""Simplified plotting."""
2

3
import matplotlib
5✔
4
import matplotlib.pyplot as plt
5✔
5
import numpy as np
5✔
6
import smplr
5✔
7
import sympy
5✔
8
import uncertainties
5✔
9
import uncertainties.unumpy as unp
5✔
10
from matplotlib import pylab
5✔
11
from matplotlib.pyplot import *
5✔
12

13
# local imports
14
from smpl import doc, interpolate, io, stat, util, wrap
5✔
15
from smpl import fit as ffit
5✔
16

17

18
def set_plot_style():
5✔
19
    # fig_size = (8, 6)
20
    # fig_legendsize = 14
21
    # fig_labelsize = 12 # ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’.
UNCOV
22
    params = {
×
23
        "legend.fontsize": "x-large",
24
        "figure.figsize": (8, 6),
25
        "axes.labelsize": "x-large",
26
        "axes.titlesize": "x-large",
27
        "xtick.labelsize": "x-large",
28
        "ytick.labelsize": "x-large",
29
    }
UNCOV
30
    pylab.rcParams.update(params)
×
UNCOV
31
    matplotlib.rcParams.update(params)
×
32
    # matplotlib.rcParams.update({'font.size': fig_labelsize})
33
    # colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
34

35

36
unv = unp.nominal_values
5✔
37
usd = unp.std_devs
5✔
38

39
default = {
5✔
40
    #    'params'        :[None      ,"Initial fit parameters",
41
    "title": [None, "Plot title"],
42
    "xlabel": [
43
        "",
44
        "X axis label",
45
    ],
46
    "ylabel": [
47
        "",
48
        "Y axis label",
49
    ],
50
    "label": [
51
        None,
52
        "Legend name of plotted ``data``",
53
    ],
54
    "fmt": [
55
        ".",
56
        "Format for plotting fit function (could be a line style, marker or a combination of both or 'step')",
57
    ],
58
    "where": [
59
        "mid",
60
        "Where to place the ticks. Possible values are 'pre', 'post', 'mid', 'edge'. Might be incompatible with uncertainties.",
61
    ],
62
    "units": [
63
        None,
64
        "Units of the fit parameters as strings. Displayed in the Legend",
65
    ],
66
    "save": [
67
        None,
68
        " File to save the plot",
69
    ],
70
    "lpos": [
71
        0,
72
        "Legend position",
73
    ],
74
    "tight": [
75
        True,
76
        "tight_layout",
77
    ],
78
    #          'frange'        :[None      ,"Limit the fit to given range. First integer is the lowest and second the highest index.",],
79
    "prange": [
80
        None,
81
        "Limit the plot of the fit to given range",
82
    ],
83
    "sigmas": [
84
        0,
85
        "Color the array of given ``sigma`` times uncertainty. Only works if the fit function is coded with ``unp``",
86
    ],
87
    "data_sigmas": [
88
        1,
89
        "Color the array of given ``sigma`` times uncertainty. Only works if the data has uncertainties",
90
    ],
91
    "init": [False, "Initialize a new plot"],
92
    "ss": [
93
        True,
94
        "save, add legends and grid to the plot",
95
    ],
96
    "also_data": [True, " also plot the data"],
97
    "also_fit": [
98
        True,
99
        "also plot the fit",
100
    ],
101
    "auto_fit": [
102
        False,
103
        "automatically fit",
104
    ],
105
    "logy": [
106
        False,
107
        "logarithmic x axis",
108
    ],
109
    "logx": [
110
        False,
111
        "logarithmic y axis",
112
    ],
113
    "function_color": [
114
        None,
115
        "Color of the function plot",
116
    ],
117
    "data_color": [
118
        None,
119
        "Color of the data plot",
120
    ],
121
    "fit_color": [
122
        None,
123
        "Color of the fit plot",
124
    ],
125
    "fit_fmt": [
126
        "-",
127
        "Format of the fit plot",
128
    ],
129
    "residue": [
130
        False,
131
        "Display difference between fit and data in a second plot",
132
    ],
133
    "residue_err": [
134
        True,
135
        "Differences between fit and data will have errorbars",
136
    ],
137
    "show": [
138
        False,
139
        "Call plt.show()",
140
    ],
141
    "size": [
142
        None,
143
        "Size of the plot as a tuple (x,y). Only has an effect if ``init`` is True",
144
    ],
145
    "number_format": [
146
        io.gf(4),
147
        "Format to display numbers.",
148
    ],
149
    # ,          'selector'      :[ None     ,"Function that takes ``x`` and ``y`` as parameters and returns an array mask in order to limit the data points for fitting. Alternatively a mask for selecting elements from datax and datay.",],
150
    # ,          'fixed_params'  :[ True     ,"Enable fixing parameters by choosing the same-named variables from ``kwargs``.",],
151
    # ,          'sortbyx'       :[ True     , "Enable sorting the x and y data so that x is sorted.",],
152
    "interpolate": [False, "Enable interpolation of the data."],
153
    "interpolate_fmt": [
154
        "-",
155
        "Either format string or linestyle tuple.",
156
    ],
157
    "interpolate_label": [
158
        None,
159
        "Label for the interpolation.",
160
    ],
161
    "extrapolate": [
162
        True,
163
        "Enable extrapolation of whole data if fit range is limited by ``frange`` or ``fselector``.",
164
    ],
165
    "extrapolate_min": [
166
        None,
167
        "Lower extrapolation bound",
168
    ],
169
    "extrapolate_max": [
170
        None,
171
        "Higher extrapolation bound",
172
    ],
173
    "extrapolate_fmt": [
174
        "--",
175
        "Format of the extrapolation line",
176
    ],
177
    "extrapolate_hatch": [
178
        r"||",
179
        "Extrapolation shape/hatch for filled area in case of ``sigmas``>0. See https://matplotlib.org/stable/gallery/shapes_and_collections/hatch_style_reference.html",
180
    ],
181
    "bbox_to_anchor": [
182
        None,
183
        "Position in a tuple (x,y),Shift position of the legend out of the main pane. ",
184
    ],
185
    "ncol": [
186
        None,
187
        "Columns in the legend if used with ``bbox_to_anchor``.",
188
    ],
189
    "steps": [
190
        1000,
191
        "resolution of the plotted function",
192
    ],
193
    "fitinline": [
194
        False,
195
        "No newlines for each fit parameter",
196
    ],
197
    "grid": [
198
        True,
199
        "Enable grid for the plot",
200
    ],
201
    "hist": [
202
        False,
203
        "Enable histogram plot",
204
    ],
205
    "stairs": [
206
        False,
207
        "Enable stair plot",
208
    ],
209
    "capsize": [5, "size of cap on error bar plot"],
210
    "axes": [None, "set current axis"],
211
    "linestyle": [None, "linestyle, only active if `fmt`=None"],
212
    "xspace": [
213
        np.linspace,
214
        "xspace gets called with xspace(xmin,xmax,steps) in :func:`function` to get the points of the function that will be drawn.",
215
    ],
216
    "alpha": [0.2, "alpha value for the fill_between plot"],
217
}
218

219

220
@doc.append_doc(ffit.fit_kwargs)
5✔
221
@doc.append_str("\t")
5✔
222
@doc.append_str(doc.array_table(default, init=False))
5✔
223
@doc.append_str(
5✔
224
    doc.array_table({"plot_kwargs        ": ["default", "description"]}, bottom=False)
225
)
226
@doc.append_str("\n\n")
5✔
227
def plot_kwargs(kwargs):
5✔
228
    """Set default :func:`plot_kwargs` if not set."""
229
    kwargs = ffit.fit_kwargs(kwargs)
5✔
230
    for k, v in default.items():
5✔
231
        if k not in kwargs:
5✔
232
            kwargs[k] = v[0]
5✔
233
    return kwargs
5✔
234

235

236
# TODO optimize to minimize number of calls to function
237
# @append_doc(default_kwargs)
238

239

240
def fit(func, *adata, **kwargs):
5✔
241
    """
242
    Fit and plot function to datax and datay.
243

244
    Parameters
245
    ----------
246
    datax : array_like
247
        X data either as ``unp.uarray`` or ``np.array`` or ``list``
248
    datay : array_like
249
        Y data either as ``unp.uarray`` or ``np.array`` or ``list``
250
    function : func
251
        Fit function with parameters: ``x``, ``params``
252
    **kwargs : optional
253
        see :func:`plot_kwargs`.
254
    Fit parameters can be fixed via ``kwargs`` eg. ``a=5``.
255

256
    Returns
257
    -------
258
    array_like
259
        Optimized fit parameters of ``function`` to ``datax`` and ``datay``.
260
        If ``datay`` is complex, both the real and imaginary part are returned.
261

262
    Examples
263
    --------
264

265
    .. plot::
266
        :include-source:
267

268
        >>> from smpl import functions as f
269
        >>> from smpl import plot
270
        >>> param = plot.fit([0,1,2],[0,1,2],f.line)
271
        >>> float(plot.unv(param).round()[0])
272
        1.0
273

274
    """
275
    if "xaxis" in kwargs and ("xlabel" not in kwargs or not kwargs["xlabel"]):
5✔
276
        # warnings.warn("xaxis is deprecated. Use xlabel instead.", DeprecationWarning, 2)
277
        kwargs["xlabel"] = kwargs["xaxis"]
5✔
278
        # TODO maybe pop
279
    if "yaxis" in kwargs and ("ylabel" not in kwargs or not kwargs["ylabel"]):
5✔
280
        # warnings.warn("yaxis is deprecated. Use ylabel instead.", DeprecationWarning, 2)
281
        kwargs["ylabel"] = kwargs["yaxis"]
5✔
282
        # TODO maybe pop
283

284
    function = func
5✔
285
    if "function" in kwargs:
5✔
286
        function = kwargs["function"]
5✔
287
        del kwargs["function"]
5✔
288
        adata = [func, *adata]
5✔
289
    # Fix parameter order if necessary
290
    elif isinstance(function, (list, tuple, np.ndarray)):
5✔
291
        adata = [adata[-1], function, *adata[:-1]]
5✔
292
        function = adata[0]
5✔
293
        adata = adata[1:]
5✔
294
    if util.true("bins", kwargs):
5✔
295
        # yvalue will be overwritten
296
        ndata = [*adata, *adata]
5✔
297
        for i, o in enumerate(adata):
5✔
298
            ndata[2 * i] = o
5✔
299
            ndata[2 * i + 1] = o * 0
5✔
300
        adata = ndata
5✔
301

302
    assert len(adata) % 2 == 0, "data must be pairs of x and y data"
5✔
303
    if len(adata) == 2:
5✔
304
        datax, datay = adata
5✔
305
    else:
306
        rs = []
5✔
307
        for i in range(0, len(adata), 2):
5✔
308
            datax, datay = adata[i], adata[i + 1]
5✔
309
            if util.true("bins", kwargs):
5✔
310
                rs.append(fit(function, datax, **kwargs))
5✔
311
            else:
312
                rs.append(fit(function, datax, datay, **kwargs))
5✔
313
        return rs
5✔
314

315
    kwargs = plot_kwargs(kwargs)
5✔
316

317
    if np.any(np.iscomplex(datay)):
5✔
318
        label = util.get("label", kwargs, "")
5✔
319
        kwargs["label"] = label + "(real)"
5✔
320
        r = fit(datax, datay.real, function=function, **kwargs)
5✔
321
        kwargs["label"] = label + "(imag)"
5✔
322
        i = fit(datax, datay.imag, function=function, **kwargs)
5✔
323
        return r, i
5✔
324
    if kwargs["auto_fit"]:
5✔
325
        best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
5✔
326
        if best_f is not None:
5✔
327
            del kwargs["auto_fit"]
5✔
328
            fit(datax, datay, best_f, **kwargs)
5✔
329
        return best_f, best_ff, lambda_f
5✔
330
    if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
5✔
331
        kwargs["lpos"] = -1
5✔
332
    return _fit_impl(datax, datay, function, **kwargs)
5✔
333

334

335
def _fit_impl(datax, datay, function, **kwargs):
5✔
336
    x = None
5✔
337
    y = None
5✔
338
    rfit = None
5✔
339
    ifit = None
5✔
340
    fig = None
5✔
341
    fig = init_plot(kwargs)
5✔
342
    ll = None
5✔
343
    if kwargs["also_data"]:
5✔
344
        ll = plt_data(datax, datay, **kwargs).get_color()
5✔
345
    if kwargs["interpolate"]:
5✔
346
        ifit, _, x, y = plt_interpolate(datax, datay, icolor=ll, **kwargs)
5✔
347
    if kwargs["also_fit"]:
5✔
348
        assert function is not None, "function must be given"
5✔
349
        rfit, kwargs["fit_color"], _, _ = plt_fit(datax, datay, function, **kwargs)
5✔
350
    if kwargs["ss"]:
5✔
351
        kwargs["oldshow"] = kwargs["show"]
5✔
352
        kwargs["show"] = kwargs["show"] and not kwargs["residue"]
5✔
353
        save_plot(**kwargs)
5✔
354
        kwargs["show"] = kwargs["oldshow"]
5✔
355
    if kwargs["residue"] and fig is not None:
5✔
356
        plt_residue(datax, datay, function, rfit, fig, **kwargs)
5✔
357
    if not kwargs["also_fit"] and kwargs["interpolate"]:
5✔
358
        return (ifit, x, y)
5✔
359
        # return ifit
360
    return rfit
5✔
361

362

363
# @append_doc(default_kwargs)
364

365

366
def data(*data, function=None, **kwargs):
5✔
367
    """
368
    Plot datay against datax via :func:`fit`
369

370
    Parameters
371
    ----------
372
    datax : array_like
373
        X data either as ``unp.uarray`` or ``np.array`` or ``list``
374
    datay : array_like
375
        Y data either as ``unp.uarray`` or ``np.array`` or ``list``
376
    function : func,optional
377
        Fit function with parameters: ``x``, ``params``
378
    **kwargs : optional
379
        see :func:`plot_kwargs`.
380
    Returns
381
    -------
382
    array_like
383
        Optimized fit parameters of ``function`` to ``datax`` and ``datay``
384
    """
385
    if "also_fit" not in kwargs:
5✔
386
        kwargs["also_fit"] = False
5✔
387
    kwargs = plot_kwargs(kwargs)
5✔
388
    return fit(function=function, *data, **kwargs)
5✔
389

390

391
# @append_doc(default_kwargs)
392

393

394
def auto(*adata, funcs=None, **kwargs):
5✔
395
    """
396
    Automatically loop over functions and fit the best one.
397

398
    Parameters
399
    ----------
400
    funcs : function array
401
        functions to consider as fit. Default all ``smpl.functions``.
402
    **kwargs : optional
403
        see :func:`plot_kwargs`.
404

405
    Returns
406
    -------
407
    The best fit function and it's parameters. Also a lambda function where the parameters are already applied.
408

409

410

411
    """
412
    if "auto_fit" not in kwargs:
5✔
413
        kwargs["auto_fit"] = True
5✔
414
    kwargs = plot_kwargs(kwargs)
5✔
415
    return fit(function=funcs, *adata, **kwargs)
5✔
416

417

418
def _function(
5✔
419
    func,
420
    xfit,
421
    fmt="-",
422
    label=None,
423
    function_color=None,
424
    sigmas=0.0,
425
    alpha=0.4,
426
    **kwargs,
427
):
428
    # kargs = {}
429
    # if util.has("fmt", kwargs):
430
    #    kargs["fmt"] = kwargs["fmt"]
431
    # if util.has("label", kwargs) and kwargs["label"] != "":
432
    #    kargs["label"] = kwargs["label"]
433
    # if util.has("function_color", kwargs) and kwargs["function_color"] != "":
434
    #    kargs["color"] = kwargs["function_color"]
435
    # if util.has("sigmas", kwargs) and kwargs["sigmas"] != "":
436
    #    kargs["sigmas"] = kwargs["sigmas"]
437
    # if util.has("alpha", kwargs) and kwargs["alpha"] != "":
438
    #    kargs["alpha"] = kwargs["alpha"]
439
    # __function(func, xfit,  **kargs)
440

441
    if label == "":
5✔
442
        label = None
5✔
443
    if function_color == "":
5✔
UNCOV
444
        function_color = None
×
445
    if sigmas == "":
5✔
UNCOV
446
        sigmas = 0.0
×
447
    if alpha == "":
5✔
UNCOV
448
        alpha = 0.4
×
449
    __function(
5✔
450
        func,
451
        xfit,
452
        fmt=fmt,
453
        label=label,
454
        color=function_color,
455
        sigmas=sigmas,
456
        alpha=alpha,
457
        **kwargs,
458
    )
459

460

461
def plt_plt(x, y, fmt, color, label, linestyle, **kwargs):
5✔
462
    if linestyle is None and fmt is not None:
5✔
463
        return plt.plot(x, y, fmt, label=label, color=color, **kwargs)
5✔
464
    if linestyle is not None and fmt is None:
5✔
UNCOV
465
        return plt.plot(x, y, label=label, color=color, linestyle=linestyle, **kwargs)
×
466
    if linestyle is None and fmt is None:
5✔
467
        return plt.plot(x, y, label=label, color=color, **kwargs)
5✔
468
    # should not reach here
UNCOV
469
    raise ValueError(
×
470
        "Either fmt or linestyle must be given, but not both. fmt=%s, linestyle=%s"
471
        % (fmt, linestyle)
472
    )
473

474

475
def __function(
5✔
476
    gfunc,
477
    xlinspace,
478
    fmt="-",
479
    label=None,
480
    color=None,
481
    hatch=None,
482
    sigmas=0.0,
483
    linestyle=None,
484
    alpha=0.4,
485
    **kwargs,
486
):
487
    # filter unused bad kwargs here to avoid passing them down
488
    # TODO it would be better to not pass them down in the first place
489
    for key in [
5✔
490
        "pre",
491
        "post",
492
        "xaxis",
493
        "yaxis",
494
        "xvar",
495
        "xmin",
496
        "xmax",
497
        "xlabel",
498
        "ylabel",
499
        "bins",
500
        "binunc",
501
        "bbox_to_anchor",
502
        "tight",
503
        "residue",
504
        "lpos",
505
        "interpolate",
506
        "params",
507
        "also_fit",
508
        "init",
509
        "frange",
510
        "epsfcn",
511
        "units",
512
        "fselector",
513
        "maxfev",
514
        "sortbyx",
515
        "xerror",
516
        "yerror",
517
        "fixed_params",
518
        "autotqdm",
519
        "fitter",
520
        "title",
521
        "where",
522
        "save",
523
        "prange",
524
        "ss",
525
        "also_data",
526
        "auto_fit",
527
        "data_sigmas",
528
        "logy",
529
        "logx",
530
        "data_color",
531
        "fit_color",
532
        "fit_fmt",
533
        "show",
534
        "size",
535
        "number_format",
536
        "selector",
537
        "fitinline",
538
        "grid",
539
        "hist",
540
        "stairs",
541
        "capsize",
542
        "axes",
543
        "xspace",
544
        "extrapolate",
545
        "extrapolate_min",
546
        "extrapolate_max",
547
        "extrapolate_fmt",
548
        "extrapolate_hatch",
549
        "function_color",
550
        "residue_err",
551
        "interpolate_fmt",
552
        "interpolate_label",
553
        "interpolate_lower_uncertainty",
554
        "ncol",
555
        "steps",
556
        "interpolator",
557
        "next_color",
558
    ]:
559
        kwargs.pop(key, None)
5✔
560
    func = gfunc
5✔
561
    x = xlinspace
5✔
562
    l = label
5✔
563

564
    if isinstance(func(x)[0], uncertainties.UFloat):
5✔
565
        if sigmas > 0:
5✔
566
            (ll,) = plt_plt(
5✔
567
                x,
568
                unv(func(x)),
569
                fmt,
570
                label=None,
571
                color=color,
572
                linestyle=linestyle,
573
                **kwargs,
574
            )
575
            y = func(x)
5✔
576
            plt.fill_between(
5✔
577
                x,
578
                unv(y) - sigmas * usd(y),
579
                unv(y) + sigmas * usd(y),
580
                alpha=alpha,
581
                label=l,
582
                color=ll.get_color(),
583
                hatch=hatch,
584
                **kwargs,
585
            )
586
        else:
587
            (ll,) = plt_plt(
5✔
588
                x,
589
                unv(func(x)),
590
                fmt,
591
                label=l,
592
                color=color,
593
                linestyle=linestyle,
594
                **kwargs,
595
            )
596
    else:
597
        (ll,) = plt_plt(
5✔
598
            x, func(x), fmt, label=l, color=color, linestyle=linestyle, **kwargs
599
        )
600
    return ll
5✔
601

602

603
def function(func, *args, fmt="-", **kwargs):
5✔
604
    """
605
    Plot function ``func`` between ``xmin`` and ``xmax``
606

607
    Parameters
608
    ----------
609
    func : function
610
        Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
611
    *args : optional
612
        arguments for ``func``
613
    **kwargs : optional
614
        see :func:`plot_kwargs`.
615
    """
616
    kwargs["fmt"] = fmt
5✔
617
    if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
5✔
618
        kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
5✔
619
        # raise Exception("xmin or xmax missing.")
620

621
    # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
622
    #    kwargs['lpos'] = -1
623

624
    if "label" not in kwargs:
5✔
625
        kwargs = plot_kwargs(kwargs)
5✔
626
        kwargs["label"] = get_fnc_legend(func, args, **kwargs)
5✔
627
    else:
628
        kwargs = plot_kwargs(kwargs)
5✔
629

630
    xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
5✔
631
    init_plot(kwargs)
5✔
632

633
    # kwargs['lpos'] = 0
634
    # _plot(xfit, func(xfit, *args), **kwargs)
635
    _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
5✔
636
    if kwargs["ss"]:
5✔
637
        save_plot(**kwargs)
5✔
638

639

640
# xaxis="",yaxis="",fit_color=None,save = None,residue_err=True,show=False):
641
def plt_residue(datax, datay, gfunction, rfit, fig, **kwargs):
5✔
642
    function = wrap.get_lambda(gfunction, kwargs["xvar"])
5✔
643
    fig.add_axes((0.1, 0.1, 0.8, 0.2))
5✔
644
    kwargs["ylabel"] = "$\\Delta$" + kwargs["ylabel"]
5✔
645
    kwargs["data_color"] = kwargs["fit_color"]
5✔
646

647
    if kwargs["residue_err"]:
5✔
648
        plt_data(datax, datay - function(datax, *rfit), **kwargs)
5✔
649
    else:
650
        plt_data(unv(datax), unv(datay - function(datax, *rfit)), **kwargs)
5✔
651
    kwargs["lpos"] = -1
5✔
652
    save_plot(**kwargs)
5✔
653

654

655
def data_split(datax, datay, **kwargs):
5✔
656
    return ffit.data_split(datax, datay, **kwargs)
5✔
657

658

659
def _fit(datax, datay, function, **kwargs):
5✔
660
    """
661
    Returns a fit like :func:`fit` but does no plotting.
662
    """
663
    return ffit.fit(datax, datay, function, **kwargs)
5✔
664

665

666
def plt_data(datax, datay, **kwargs):
5✔
667
    """
668
    Plot datay vs datax
669
    """
670
    x, y, xerr, yerr = data_split(datax, datay, **kwargs)
5✔
671
    if xerr is not None:
5✔
672
        xerr = xerr * kwargs["data_sigmas"]
5✔
673
    if yerr is not None:
5✔
674
        yerr = yerr * kwargs["data_sigmas"]
5✔
675

676
    ll = None
5✔
677
    if xerr is None and yerr is None:
5✔
678
        if kwargs["fmt"] is None:
5✔
UNCOV
679
            if kwargs["linestyle"] is None:
×
UNCOV
680
                (ll,) = plt.plot(
×
681
                    x, y, label=kwargs["label"], color=kwargs["data_color"]
682
                )
683
            else:
UNCOV
684
                (ll,) = plt.plot(
×
685
                    x,
686
                    y,
687
                    label=kwargs["label"],
688
                    color=kwargs["data_color"],
689
                    linestyle=kwargs["linestyle"],
690
                )
691
        elif kwargs["fmt"] == "step":
5✔
UNCOV
692
            (ll,) = plt.step(
×
693
                x,
694
                y,
695
                where=kwargs["where"],
696
                label=kwargs["label"],
697
                color=kwargs["data_color"],
698
            )
699
        elif kwargs["fmt"] == "hist":
5✔
700
            (ll,) = plt.step(
5✔
701
                x,
702
                y,
703
                where=kwargs["where"],
704
                label=kwargs["label"],
705
                color=kwargs["data_color"],
706
            )
707
            plt.fill_between(x, y, step="mid", color=ll.get_color())
5✔
708
        else:
709
            (ll,) = plt.plot(
5✔
710
                x, y, kwargs["fmt"], label=kwargs["label"], color=kwargs["data_color"]
711
            )
712
    elif kwargs["fmt"] is None:
5✔
713
        if kwargs["linestyle"] is None:
5✔
714
            (
5✔
715
                ll,
716
                _,
717
                _,
718
            ) = plt.errorbar(
719
                x,
720
                y,
721
                yerr=yerr,
722
                xerr=xerr,
723
                fmt=" ",
724
                capsize=kwargs["capsize"],
725
                label=kwargs["label"],
726
                color=kwargs["data_color"],
727
            )
728
        else:
729
            (
1✔
730
                ll,
731
                _,
732
                _,
733
            ) = plt.errorbar(
734
                x,
735
                y,
736
                yerr=yerr,
737
                xerr=xerr,
738
                fmt=" ",
739
                capsize=kwargs["capsize"],
740
                label=kwargs["label"],
741
                color=kwargs["data_color"],
742
                linestyle=kwargs["linestyle"],
743
            )
744
    elif kwargs["fmt"] == "step":
5✔
745
        (ll,) = plt.step(x, y, where=kwargs["where"], color=kwargs["data_color"])
5✔
746
        if xerr is not None:
5✔
747
            for ix, xv in enumerate(x):
5✔
748
                dx = xerr[ix]
5✔
749
                tx = [xv - dx, xv + dx]
5✔
750
                plt.fill_between(
5✔
751
                    tx,
752
                    y[ix] - yerr[ix],
753
                    y[ix] + yerr[ix],
754
                    label=kwargs["label"] if ix == 1 else None,
755
                    alpha=kwargs["alpha"],
756
                    step="pre",
757
                    color=ll.get_color(),
758
                )
759
        else:
760
            plt.fill_between(
5✔
761
                x,
762
                y - yerr,
763
                y + yerr,
764
                label=kwargs["label"],
765
                alpha=kwargs["alpha"],
766
                step="mid",
767
                color=ll.get_color(),
768
            )
769
    elif kwargs["fmt"] == "hist":
5✔
770
        (
5✔
771
            ll,
772
            _,
773
            _,
774
        ) = plt.errorbar(
775
            x,
776
            y,
777
            yerr=yerr,
778
            xerr=xerr,
779
            fmt=" ",
780
            capsize=kwargs["capsize"],
781
            color="black",
782
        )
783
        plt.fill_between(x, y, step="mid", label=kwargs["label"], color=ll.get_color())
5✔
784
    else:
785
        (
5✔
786
            ll,
787
            _,
788
            _,
789
        ) = plt.errorbar(
790
            x,
791
            y,
792
            yerr=yerr,
793
            xerr=xerr,
794
            fmt=kwargs["fmt"],
795
            capsize=kwargs["capsize"],
796
            label=kwargs["label"],
797
            color=kwargs["data_color"],
798
        )
799
    return ll
5✔
800

801

802
def get_fnc_legend(function, rfit, **kwargs):
5✔
803
    l = wrap.get_latex(function)
5✔
804

805
    vnames = wrap.get_varnames(function, kwargs["xvar"])
5✔
806
    for i in range(1, len(vnames)):
5✔
807
        l = l + ("\n" if not kwargs["fitinline"] or i == 1 else " ")
5✔
808
        l = l + "$" + sympy.latex(sympy.symbols(str(vnames[i]))) + "$="
5✔
809
        if kwargs["units"] is not None and usd(rfit[i - 1]) > 0:
5✔
810
            l = l + "("
5✔
811
        if "number_format" in kwargs:
5✔
812
            l = l + kwargs["number_format"].format(rfit[i - 1])
5✔
813
        else:
UNCOV
814
            l = l + "%s" % (rfit[i - 1])
×
815

816
        if kwargs["units"] is not None and usd(rfit[i - 1]) > 0:
5✔
817
            l = l + ")"
5✔
818
        if kwargs["units"] is not None:
5✔
819
            l = l + " " + kwargs["units"][i - 1]
5✔
820
    return l
5✔
821

822

823
def plt_fit_or_interpolate(
5✔
824
    datax, datay, fitted, l=None, c=None, f=None, ls=None, **kwargs
825
):
826
    # just filter these kwargs out, so they dont get passed down and are replaced by above args
827
    # TODO why not pass label=XXX directly to this?
828
    #      -> probably since there are cases where both e.g. color and replace color are needed
829
    for key in ["color", "label", "fmt", "linestyle", "hatch"]:
5✔
830
        kwargs.pop(key, None)
5✔
831
    if kwargs["prange"] is None:
5✔
832
        x, _, _, _ = ffit.fit_split(datax, datay, **kwargs)
5✔
833
        xfit = kwargs["xspace"](np.min(unv(x)), np.max(unv(x)), kwargs["steps"])
5✔
834
    else:
UNCOV
835
        xfit = kwargs["xspace"](
×
836
            kwargs["prange"][0], kwargs["prange"][1], kwargs["steps"]
837
        )
838
    ll = __function(
5✔
839
        fitted,
840
        xfit,
841
        kwargs["fit_fmt"] if f is not None and ls is None else f,
842
        label=l,
843
        color=kwargs["fit_color"] if c is None else c,
844
        linestyle=ls,
845
        **kwargs,
846
    )
847

848
    if (
5✔
849
        (
850
            (kwargs["frange"] is not None or kwargs["fselector"] is not None)
851
            and util.true("extrapolate", kwargs)
852
        )
853
        or util.has("extrapolate_max", kwargs)
854
        or util.has("extrapolate_min", kwargs)
855
    ):
856
        xxfit = kwargs["xspace"](
5✔
857
            util.get("extrapolate_min", kwargs, np.min(unv(datax))),
858
            util.get("extrapolate_max", kwargs, np.max(unv(datax))),
859
            kwargs["steps"],
860
        )
861
        for pmin, pmax in [
5✔
862
            (np.min(xxfit), np.min(xfit)),
863
            (np.max(xfit), np.max(xxfit)),
864
        ]:
865
            __function(
5✔
866
                fitted,
867
                kwargs["xspace"](pmin, pmax, kwargs["steps"]),
868
                util.get("extrapolate_fmt", kwargs, "--"),
869
                color=ll.get_color(),
870
                hatch=util.get("extrapolate_hatch", kwargs, r"||"),
871
                **kwargs,
872
            )
873
    return ll.get_color(), xfit, fitted(xfit)
5✔
874

875

876
def plt_interpolate(datax, datay, icolor=None, **kwargs):
5✔
877
    """
878
    Interpolate and Plot that Interpolation.
879
    """
880
    inter = interpolate.interpolate(datax, datay, **kwargs)
5✔
881
    kargs = {}
5✔
882
    if isinstance(kwargs["interpolate_fmt"], tuple):
5✔
UNCOV
883
        kargs["ls"] = kwargs["interpolate_fmt"]
×
884
    else:
885
        kargs["f"] = kwargs["interpolate_fmt"]
5✔
886
    if kwargs["interpolate_label"] is not None:
5✔
887
        kargs["l"] = kwargs["interpolate_label"]
5✔
888
    # l = None so that no label
889
    return (
5✔
890
        inter,
891
        *plt_fit_or_interpolate(datax, datay, inter, c=icolor, **kargs, **kwargs),
892
    )
893

894

895
def plt_fit(datax, datay, gfunction, **kwargs):
5✔
896
    """
897
    Fit and Plot that Fit.
898
    """
899
    func = wrap.get_lambda(gfunction, kwargs["xvar"])
5✔
900
    rfit = _fit(datax, datay, gfunction, **kwargs)
5✔
901

902
    def fitted(x):
5✔
903
        return func(x, *rfit)
5✔
904

905
    vnames = wrap.get_varnames(gfunction, kwargs["xvar"])
5✔
906
    for v in vnames[1:]:  # remove fixed parameters from kwargs
5✔
907
        kwargs.pop(v, None)
5✔
908

909
    l = get_fnc_legend(gfunction, rfit, **kwargs)
5✔
910
    return (rfit, *plt_fit_or_interpolate(datax, datay, fitted, l, **kwargs))
5✔
911

912

913
def init_plot(kwargs):
5✔
914
    fig = None
5✔
915
    if util.has("axes", kwargs) and kwargs["axes"] is not None:
5✔
916
        plt.sca(kwargs["axes"])
5✔
917
        fig = kwargs["axes"].get_figure()
5✔
918
    if kwargs["init"] or util.true("residue", kwargs):
5✔
919
        if kwargs["size"] is None:
5✔
920
            fig = plt.figure()
5✔
921
        else:
UNCOV
922
            fig = plt.figure(figsize=kwargs["size"])
×
923
        if kwargs["residue"]:
5✔
924
            fig.add_axes((0.1, 0.3, 0.8, 0.6))
5✔
925
    if util.has("next_color", kwargs) and not kwargs["next_color"]:
5✔
926
        lines = plt.gca()._get_lines
5✔
927
        tmp_color = lines._cycler_items[lines._idx]["color"]
5✔
928

929
        if kwargs["data_color"] is None:
5✔
930
            kwargs["data_color"] = tmp_color
5✔
931
        if kwargs["fit_color"] is None:
5✔
932
            kwargs["fit_color"] = tmp_color
5✔
933
        if kwargs["function_color"] is None:
5✔
934
            kwargs["function_color"] = tmp_color
5✔
935
    return fig
5✔
936

937

938
def save_plot(**kwargs):
5✔
939
    """
940
    save plot
941
    """
942
    smplr.style_plot1d(**kwargs)
5✔
943
    if "lpos" in kwargs and kwargs["lpos"] >= 0:
5✔
944
        if util.has("bbox_to_anchor", kwargs):
5✔
945
            if util.has("ncol", kwargs):
5✔
946
                plt.legend(
5✔
947
                    loc=kwargs["lpos"],
948
                    bbox_to_anchor=kwargs["bbox_to_anchor"],
949
                    ncol=kwargs["ncol"],
950
                    borderaxespad=0,
951
                )
952
            else:
953
                plt.legend(loc=kwargs["lpos"], bbox_to_anchor=kwargs["bbox_to_anchor"])
5✔
954
        else:
955
            plt.legend(loc=kwargs["lpos"])
5✔
956
    if "save" in kwargs and kwargs["save"] is not None:
5✔
UNCOV
957
        io.mkdirs(kwargs["save"])
×
UNCOV
958
        plt.savefig(kwargs["save"] + ".pdf")
×
959
    if kwargs.get("show"):
5✔
960
        show(**kwargs)
5✔
961

962

963
def show(**kwargs):
5✔
964
    kwargs = plot_kwargs(kwargs)
5✔
965

966
    smplr.style_plot1d(**kwargs)
5✔
967
    plt.show()
5✔
968

969

970
from smpl.plot2d import plot2d as _plot2d
5✔
971
from smpl.plot2d import plot2d_kwargs as _plot2d_kwargs
5✔
972

973

974
def plot2d(*args, **kwargs):
5✔
975
    _plot2d(*args, **kwargs)
5✔
976

977

978
def plot2d_kwargs(*args, **kwargs):
5✔
UNCOV
979
    _plot2d_kwargs(*args, **kwargs)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc