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

APN-Pucky / smpl / 17499274440

05 Sep 2025 04:46PM UTC coverage: 89.036%. Remained the same
17499274440

push

github

APN-Pucky
dof -> ndf

2 of 6 new or added lines in 2 files covered. (33.33%)

1 existing line in 1 file now uncovered.

1616 of 1815 relevant lines covered (89.04%)

4.45 hits per line

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

84.02
/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’.
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
    }
30
    pylab.rcParams.update(params)
×
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
    "ratio": [
138
        False,
139
        "Display ratio between fit and data in a second plot",
140
    ],
141
    "ratio_err": [
142
        True,
143
        "Ratio between fit and data will have errorbars",
144
    ],
145
    "show": [
146
        False,
147
        "Call plt.show()",
148
    ],
149
    "size": [
150
        None,
151
        "Size of the plot as a tuple (x,y). Only has an effect if ``init`` is True",
152
    ],
153
    "number_format": [
154
        io.gf(4),
155
        "Format to display numbers.",
156
    ],
157
    # ,          '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.",],
158
    # ,          'fixed_params'  :[ True     ,"Enable fixing parameters by choosing the same-named variables from ``kwargs``.",],
159
    # ,          'sortbyx'       :[ True     , "Enable sorting the x and y data so that x is sorted.",],
160
    "interpolate": [False, "Enable interpolation of the data."],
161
    "interpolate_fmt": [
162
        "-",
163
        "Either format string or linestyle tuple.",
164
    ],
165
    "interpolate_label": [
166
        None,
167
        "Label for the interpolation.",
168
    ],
169
    "extrapolate": [
170
        True,
171
        "Enable extrapolation of whole data if fit range is limited by ``frange`` or ``fselector``.",
172
    ],
173
    "extrapolate_min": [
174
        None,
175
        "Lower extrapolation bound",
176
    ],
177
    "extrapolate_max": [
178
        None,
179
        "Higher extrapolation bound",
180
    ],
181
    "extrapolate_fmt": [
182
        "--",
183
        "Format of the extrapolation line",
184
    ],
185
    "extrapolate_hatch": [
186
        r"||",
187
        "Extrapolation shape/hatch for filled area in case of ``sigmas``>0. See https://matplotlib.org/stable/gallery/shapes_and_collections/hatch_style_reference.html",
188
    ],
189
    "bbox_to_anchor": [
190
        None,
191
        "Position in a tuple (x,y),Shift position of the legend out of the main pane. ",
192
    ],
193
    "ncol": [
194
        None,
195
        "Columns in the legend if used with ``bbox_to_anchor``.",
196
    ],
197
    "steps": [
198
        1000,
199
        "resolution of the plotted function",
200
    ],
201
    "fitinline": [
202
        False,
203
        "No newlines for each fit parameter",
204
    ],
205
    "grid": [
206
        True,
207
        "Enable grid for the plot",
208
    ],
209
    "hist": [
210
        False,
211
        "Enable histogram plot",
212
    ],
213
    "stairs": [
214
        False,
215
        "Enable stair plot",
216
    ],
217
    "capsize": [5, "size of cap on error bar plot"],
218
    "axes": [None, "set current axis"],
219
    "linestyle": [None, "linestyle, only active if `fmt`=None"],
220
    "xspace": [
221
        np.linspace,
222
        "xspace gets called with xspace(xmin,xmax,steps) in :func:`function` to get the points of the function that will be drawn.",
223
    ],
224
    "alpha": [0.2, "alpha value for the fill_between plot"],
225
    "append_chi2": [False, "Append chi2 to legend"],
226
    "append_r2": [False, "Append r2 to legend"],
227
    "append_ndf": [False, "Append number of degrees of freedom to legend"],
228
}
229

230

231
@doc.append_doc(ffit.fit_kwargs)
5✔
232
@doc.append_str("\t")
5✔
233
@doc.append_str(doc.array_table(default, init=False))
5✔
234
@doc.append_str(
5✔
235
    doc.array_table({"plot_kwargs        ": ["default", "description"]}, bottom=False)
236
)
237
@doc.append_str("\n\n")
5✔
238
def plot_kwargs(kwargs):
5✔
239
    """Set default :func:`plot_kwargs` if not set."""
240
    kwargs = ffit.fit_kwargs(kwargs)
5✔
241
    for k, v in default.items():
5✔
242
        if k not in kwargs:
5✔
243
            kwargs[k] = v[0]
5✔
244
    return kwargs
5✔
245

246

247
# TODO optimize to minimize number of calls to function
248
# @append_doc(default_kwargs)
249

250

251
def fit(func, *adata, **kwargs):
5✔
252
    """
253
    Fit and plot function to datax and datay.
254

255
    Parameters
256
    ----------
257
    datax : array_like
258
        X data either as ``unp.uarray`` or ``np.array`` or ``list``
259
    datay : array_like
260
        Y data either as ``unp.uarray`` or ``np.array`` or ``list``
261
    function : func
262
        Fit function with parameters: ``x``, ``params``
263
    **kwargs : optional
264
        see :func:`plot_kwargs`.
265
    Fit parameters can be fixed via ``kwargs`` eg. ``a=5``.
266

267
    Returns
268
    -------
269
    array_like
270
        Optimized fit parameters of ``function`` to ``datax`` and ``datay``.
271
        If ``datay`` is complex, both the real and imaginary part are returned.
272

273
    Examples
274
    --------
275

276
    .. plot::
277
        :include-source:
278

279
        >>> from smpl import functions as f
280
        >>> from smpl import plot
281
        >>> param = plot.fit([0,1,2],[0,1,2],f.line)
282
        >>> float(plot.unv(param).round()[0])
283
        1.0
284

285
    """
286
    if "xaxis" in kwargs and ("xlabel" not in kwargs or not kwargs["xlabel"]):
5✔
287
        # warnings.warn("xaxis is deprecated. Use xlabel instead.", DeprecationWarning, 2)
288
        kwargs["xlabel"] = kwargs["xaxis"]
5✔
289
        # TODO maybe pop
290
    if "yaxis" in kwargs and ("ylabel" not in kwargs or not kwargs["ylabel"]):
5✔
291
        # warnings.warn("yaxis is deprecated. Use ylabel instead.", DeprecationWarning, 2)
292
        kwargs["ylabel"] = kwargs["yaxis"]
5✔
293
        # TODO maybe pop
294

295
    function = func
5✔
296
    if "function" in kwargs:
5✔
297
        function = kwargs["function"]
5✔
298
        del kwargs["function"]
5✔
299
        adata = [func, *adata]
5✔
300
    # Fix parameter order if necessary
301
    elif isinstance(function, (list, tuple, np.ndarray)):
5✔
302
        adata = [adata[-1], function, *adata[:-1]]
5✔
303
        function = adata[0]
5✔
304
        adata = adata[1:]
5✔
305
    if util.true("bins", kwargs):
5✔
306
        # yvalue will be overwritten
307
        ndata = [*adata, *adata]
5✔
308
        for i, o in enumerate(adata):
5✔
309
            ndata[2 * i] = o
5✔
310
            ndata[2 * i + 1] = o * 0
5✔
311
        adata = ndata
5✔
312

313
    assert len(adata) % 2 == 0, "data must be pairs of x and y data"
5✔
314
    if len(adata) == 2:
5✔
315
        datax, datay = adata
5✔
316
    else:
317
        rs = []
5✔
318
        for i in range(0, len(adata), 2):
5✔
319
            datax, datay = adata[i], adata[i + 1]
5✔
320
            if util.true("bins", kwargs):
5✔
321
                rs.append(fit(function, datax, **kwargs))
5✔
322
            else:
323
                rs.append(fit(function, datax, datay, **kwargs))
5✔
324
        return rs
5✔
325

326
    kwargs = plot_kwargs(kwargs)
5✔
327

328
    if np.any(np.iscomplex(datay)):
5✔
329
        label = util.get("label", kwargs, "")
5✔
330
        kwargs["label"] = label + "(real)"
5✔
331
        r = fit(datax, datay.real, function=function, **kwargs)
5✔
332
        kwargs["label"] = label + "(imag)"
5✔
333
        i = fit(datax, datay.imag, function=function, **kwargs)
5✔
334
        return r, i
5✔
335
    if kwargs["auto_fit"]:
5✔
336
        best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
5✔
337
        if best_f is not None:
5✔
338
            del kwargs["auto_fit"]
5✔
339
            fit(datax, datay, best_f, **kwargs)
5✔
340
        return best_f, best_ff, lambda_f
5✔
341
    if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
5✔
342
        kwargs["lpos"] = -1
5✔
343
    return _fit_impl(datax, datay, function, **kwargs)
5✔
344

345

346
def _fit_impl(datax, datay, function, **kwargs):
5✔
347
    x = None
5✔
348
    y = None
5✔
349
    rfit = None
5✔
350
    ifit = None
5✔
351
    fig = None
5✔
352
    fig = init_plot(kwargs)
5✔
353
    ll = None
5✔
354
    if kwargs["also_data"]:
5✔
355
        ll = plt_data(datax, datay, **kwargs).get_color()
5✔
356
    if kwargs["interpolate"]:
5✔
357
        ifit, _, x, y = plt_interpolate(datax, datay, icolor=ll, **kwargs)
5✔
358
    if kwargs["also_fit"]:
5✔
359
        assert function is not None, "function must be given"
5✔
360
        rfit, kwargs["fit_color"], _, _ = plt_fit(datax, datay, function, **kwargs)
5✔
361
    if kwargs["ss"]:
5✔
362
        kwargs["oldshow"] = kwargs["show"]
5✔
363
        kwargs["show"] = (
5✔
364
            kwargs["show"] and not kwargs["residue"] and not kwargs["ratio"]
365
        )
366
        save_plot(**kwargs)
5✔
367
        kwargs["show"] = kwargs["oldshow"]
5✔
368
    if kwargs["residue"] and fig is not None:
5✔
369
        plt_residue(datax, datay, function, rfit, fig, **kwargs)
5✔
370
    if kwargs["ratio"] and fig is not None:
5✔
371
        plt_ratio(datax, datay, function, rfit, fig, **kwargs)
×
372
    if not kwargs["also_fit"] and kwargs["interpolate"]:
5✔
373
        return (ifit, x, y)
5✔
374
        # return ifit
375
    return rfit
5✔
376

377

378
# @append_doc(default_kwargs)
379

380

381
def data(*data, function=None, **kwargs):
5✔
382
    """
383
    Plot datay against datax via :func:`fit`
384

385
    Parameters
386
    ----------
387
    datax : array_like
388
        X data either as ``unp.uarray`` or ``np.array`` or ``list``
389
    datay : array_like
390
        Y data either as ``unp.uarray`` or ``np.array`` or ``list``
391
    function : func,optional
392
        Fit function with parameters: ``x``, ``params``
393
    **kwargs : optional
394
        see :func:`plot_kwargs`.
395
    Returns
396
    -------
397
    array_like
398
        Optimized fit parameters of ``function`` to ``datax`` and ``datay``
399
    """
400
    if "also_fit" not in kwargs:
5✔
401
        kwargs["also_fit"] = False
5✔
402
    kwargs = plot_kwargs(kwargs)
5✔
403
    return fit(function=function, *data, **kwargs)
5✔
404

405

406
# @append_doc(default_kwargs)
407

408

409
def auto(*adata, funcs=None, **kwargs):
5✔
410
    """
411
    Automatically loop over functions and fit the best one.
412

413
    Parameters
414
    ----------
415
    funcs : function array
416
        functions to consider as fit. Default all ``smpl.functions``.
417
    **kwargs : optional
418
        see :func:`plot_kwargs`.
419

420
    Returns
421
    -------
422
    The best fit function and it's parameters. Also a lambda function where the parameters are already applied.
423

424

425

426
    """
427
    if "auto_fit" not in kwargs:
5✔
428
        kwargs["auto_fit"] = True
5✔
429
    kwargs = plot_kwargs(kwargs)
5✔
430
    return fit(function=funcs, *adata, **kwargs)
5✔
431

432

433
def _function(
5✔
434
    func,
435
    xfit,
436
    fmt="-",
437
    label=None,
438
    function_color=None,
439
    sigmas=0.0,
440
    alpha=0.4,
441
    **kwargs,
442
):
443
    # kargs = {}
444
    # if util.has("fmt", kwargs):
445
    #    kargs["fmt"] = kwargs["fmt"]
446
    # if util.has("label", kwargs) and kwargs["label"] != "":
447
    #    kargs["label"] = kwargs["label"]
448
    # if util.has("function_color", kwargs) and kwargs["function_color"] != "":
449
    #    kargs["color"] = kwargs["function_color"]
450
    # if util.has("sigmas", kwargs) and kwargs["sigmas"] != "":
451
    #    kargs["sigmas"] = kwargs["sigmas"]
452
    # if util.has("alpha", kwargs) and kwargs["alpha"] != "":
453
    #    kargs["alpha"] = kwargs["alpha"]
454
    # __function(func, xfit,  **kargs)
455

456
    if label == "":
5✔
457
        label = None
5✔
458
    if function_color == "":
5✔
459
        function_color = None
×
460
    if sigmas == "":
5✔
461
        sigmas = 0.0
×
462
    if alpha == "":
5✔
463
        alpha = 0.4
×
464
    __function(
5✔
465
        func,
466
        xfit,
467
        fmt=fmt,
468
        label=label,
469
        color=function_color,
470
        sigmas=sigmas,
471
        alpha=alpha,
472
        **kwargs,
473
    )
474

475

476
def plt_plt(x, y, fmt, color, label, linestyle, **kwargs):
5✔
477
    if linestyle is None and fmt is not None:
5✔
478
        return plt.plot(x, y, fmt, label=label, color=color, **kwargs)
5✔
479
    if linestyle is not None and fmt is None:
5✔
480
        return plt.plot(x, y, label=label, color=color, linestyle=linestyle, **kwargs)
×
481
    if linestyle is None and fmt is None:
5✔
482
        return plt.plot(x, y, label=label, color=color, **kwargs)
5✔
483
    # should not reach here
484
    raise ValueError(
×
485
        "Either fmt or linestyle must be given, but not both. fmt=%s, linestyle=%s"
486
        % (fmt, linestyle)
487
    )
488

489

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

584
    if isinstance(func(x)[0], uncertainties.UFloat):
5✔
585
        if sigmas > 0:
5✔
586
            (ll,) = plt_plt(
5✔
587
                x,
588
                unv(func(x)),
589
                fmt,
590
                label=None,
591
                color=color,
592
                linestyle=linestyle,
593
                **kwargs,
594
            )
595
            y = func(x)
5✔
596
            plt.fill_between(
5✔
597
                x,
598
                unv(y) - sigmas * usd(y),
599
                unv(y) + sigmas * usd(y),
600
                alpha=alpha,
601
                label=l,
602
                color=ll.get_color(),
603
                hatch=hatch,
604
                **kwargs,
605
            )
606
        else:
607
            (ll,) = plt_plt(
5✔
608
                x,
609
                unv(func(x)),
610
                fmt,
611
                label=l,
612
                color=color,
613
                linestyle=linestyle,
614
                **kwargs,
615
            )
616
    else:
617
        (ll,) = plt_plt(
5✔
618
            x, func(x), fmt, label=l, color=color, linestyle=linestyle, **kwargs
619
        )
620
    return ll
5✔
621

622

623
def function(func, *args, fmt="-", **kwargs):
5✔
624
    """
625
    Plot function ``func`` between ``xmin`` and ``xmax``
626

627
    Parameters
628
    ----------
629
    func : function
630
        Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
631
    *args : optional
632
        arguments for ``func``
633
    **kwargs : optional
634
        see :func:`plot_kwargs`.
635
    """
636
    kwargs["fmt"] = fmt
5✔
637
    if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
5✔
638
        kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
5✔
639
        # raise Exception("xmin or xmax missing.")
640

641
    # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
642
    #    kwargs['lpos'] = -1
643

644
    if "label" not in kwargs:
5✔
645
        kwargs = plot_kwargs(kwargs)
5✔
646
        kwargs["label"] = get_fnc_legend(func, args, **kwargs)
5✔
647
    else:
648
        kwargs = plot_kwargs(kwargs)
5✔
649

650
    xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
5✔
651
    init_plot(kwargs)
5✔
652

653
    # kwargs['lpos'] = 0
654
    # _plot(xfit, func(xfit, *args), **kwargs)
655
    _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
5✔
656
    if kwargs["ss"]:
5✔
657
        save_plot(**kwargs)
5✔
658

659

660
# xaxis="",yaxis="",fit_color=None,save = None,residue_err=True,show=False):
661
def plt_residue(datax, datay, gfunction, rfit, fig, **kwargs):
5✔
662
    function = wrap.get_lambda(gfunction, kwargs["xvar"])
5✔
663
    fig.add_axes((0.1, 0.1, 0.8, 0.2))
5✔
664
    kwargs["ylabel"] = "$\\Delta$" + kwargs["ylabel"]
5✔
665
    kwargs["data_color"] = kwargs["fit_color"]
5✔
666

667
    if kwargs["residue_err"]:
5✔
668
        plt_data(datax, datay - function(datax, *rfit), **kwargs)
5✔
669
    else:
670
        plt_data(unv(datax), unv(datay - function(datax, *rfit)), **kwargs)
5✔
671
    kwargs["lpos"] = -1
5✔
672
    save_plot(**kwargs)
5✔
673

674

675
def plt_ratio(datax, datay, gfunction, rfit, fig, **kwargs):
5✔
676
    function = wrap.get_lambda(gfunction, kwargs["xvar"])
×
677
    fig.add_axes((0.1, 0.1, 0.8, 0.2))
×
678
    kwargs["ylabel"] = "Ratio " + kwargs["ylabel"]
×
679
    kwargs["data_color"] = kwargs["fit_color"]
×
680

681
    fit_values = function(datax, *rfit)
×
682
    if kwargs["ratio_err"]:
×
683
        plt_data(datax, datay / fit_values, **kwargs)
×
684
    else:
685
        plt_data(unv(datax), unv(datay / fit_values), **kwargs)
×
686
    kwargs["lpos"] = -1
×
687
    save_plot(**kwargs)
×
688

689

690
def data_split(datax, datay, **kwargs):
5✔
691
    return ffit.data_split(datax, datay, **kwargs)
5✔
692

693

694
def _fit(datax, datay, function, **kwargs):
5✔
695
    """
696
    Returns a fit like :func:`fit` but does no plotting.
697
    """
698
    return ffit.fit(datax, datay, function, **kwargs)
5✔
699

700

701
def plt_data(datax, datay, **kwargs):
5✔
702
    """
703
    Plot datay vs datax
704
    """
705
    x, y, xerr, yerr = data_split(datax, datay, **kwargs)
5✔
706
    if xerr is not None:
5✔
707
        xerr = xerr * kwargs["data_sigmas"]
5✔
708
    if yerr is not None:
5✔
709
        yerr = yerr * kwargs["data_sigmas"]
5✔
710

711
    ll = None
5✔
712
    if xerr is None and yerr is None:
5✔
713
        if kwargs["fmt"] is None:
5✔
714
            if kwargs["linestyle"] is None:
×
715
                (ll,) = plt.plot(
×
716
                    x, y, label=kwargs["label"], color=kwargs["data_color"]
717
                )
718
            else:
719
                (ll,) = plt.plot(
×
720
                    x,
721
                    y,
722
                    label=kwargs["label"],
723
                    color=kwargs["data_color"],
724
                    linestyle=kwargs["linestyle"],
725
                )
726
        elif kwargs["fmt"] == "step":
5✔
727
            (ll,) = plt.step(
×
728
                x,
729
                y,
730
                where=kwargs["where"],
731
                label=kwargs["label"],
732
                color=kwargs["data_color"],
733
            )
734
        elif kwargs["fmt"] == "hist":
5✔
735
            (ll,) = plt.step(
5✔
736
                x,
737
                y,
738
                where=kwargs["where"],
739
                label=kwargs["label"],
740
                color=kwargs["data_color"],
741
            )
742
            plt.fill_between(x, y, step="mid", color=ll.get_color())
5✔
743
        else:
744
            (ll,) = plt.plot(
5✔
745
                x, y, kwargs["fmt"], label=kwargs["label"], color=kwargs["data_color"]
746
            )
747
    elif kwargs["fmt"] is None:
5✔
748
        if kwargs["linestyle"] is None:
5✔
749
            (
5✔
750
                ll,
751
                _,
752
                _,
753
            ) = plt.errorbar(
754
                x,
755
                y,
756
                yerr=yerr,
757
                xerr=xerr,
758
                fmt=" ",
759
                capsize=kwargs["capsize"],
760
                label=kwargs["label"],
761
                color=kwargs["data_color"],
762
            )
763
        else:
UNCOV
764
            (
1✔
765
                ll,
766
                _,
767
                _,
768
            ) = plt.errorbar(
769
                x,
770
                y,
771
                yerr=yerr,
772
                xerr=xerr,
773
                fmt=" ",
774
                capsize=kwargs["capsize"],
775
                label=kwargs["label"],
776
                color=kwargs["data_color"],
777
                linestyle=kwargs["linestyle"],
778
            )
779
    elif kwargs["fmt"] == "step":
5✔
780
        (ll,) = plt.step(x, y, where=kwargs["where"], color=kwargs["data_color"])
5✔
781
        if xerr is not None:
5✔
782
            for ix, xv in enumerate(x):
5✔
783
                dx = xerr[ix]
5✔
784
                tx = [xv - dx, xv + dx]
5✔
785
                plt.fill_between(
5✔
786
                    tx,
787
                    y[ix] - yerr[ix],
788
                    y[ix] + yerr[ix],
789
                    label=kwargs["label"] if ix == 1 else None,
790
                    alpha=kwargs["alpha"],
791
                    step="pre",
792
                    color=ll.get_color(),
793
                )
794
        else:
795
            plt.fill_between(
5✔
796
                x,
797
                y - yerr,
798
                y + yerr,
799
                label=kwargs["label"],
800
                alpha=kwargs["alpha"],
801
                step="mid",
802
                color=ll.get_color(),
803
            )
804
    elif kwargs["fmt"] == "hist":
5✔
805
        (
5✔
806
            ll,
807
            _,
808
            _,
809
        ) = plt.errorbar(
810
            x,
811
            y,
812
            yerr=yerr,
813
            xerr=xerr,
814
            fmt=" ",
815
            capsize=kwargs["capsize"],
816
            color="black",
817
        )
818
        plt.fill_between(x, y, step="mid", label=kwargs["label"], color=ll.get_color())
5✔
819
    else:
820
        (
5✔
821
            ll,
822
            _,
823
            _,
824
        ) = plt.errorbar(
825
            x,
826
            y,
827
            yerr=yerr,
828
            xerr=xerr,
829
            fmt=kwargs["fmt"],
830
            capsize=kwargs["capsize"],
831
            label=kwargs["label"],
832
            color=kwargs["data_color"],
833
        )
834
    return ll
5✔
835

836

837
def get_fnc_legend(function, rfit, datax=None, datay=None, **kwargs):
5✔
838
    l = wrap.get_latex(function)
5✔
839

840
    vnames = wrap.get_varnames(function, kwargs["xvar"])
5✔
841
    for i in range(1, len(vnames)):
5✔
842
        l = l + ("\n" if not kwargs["fitinline"] or i == 1 else " ")
5✔
843
        l = l + "$" + sympy.latex(sympy.symbols(str(vnames[i]))) + "$="
5✔
844
        if kwargs["units"] is not None and usd(rfit[i - 1]) > 0:
5✔
845
            l = l + "("
5✔
846
        if "number_format" in kwargs:
5✔
847
            l = l + kwargs["number_format"].format(rfit[i - 1])
5✔
848
        else:
849
            l = l + "%s" % (rfit[i - 1])
×
850

851
        if kwargs["units"] is not None and usd(rfit[i - 1]) > 0:
5✔
852
            l = l + ")"
5✔
853
        if kwargs["units"] is not None:
5✔
854
            l = l + " " + kwargs["units"][i - 1]
5✔
855

856
    # Append Chi2, R2, and NDF if requested and data is available
857
    if datax is not None and datay is not None:
5✔
858
        if kwargs.get("append_chi2", False):
5✔
859
            try:
×
860
                chi2_val = ffit.Chi2(datax, datay, function, rfit, **kwargs)
×
861
                l = l + ("\n" if not kwargs["fitinline"] else " ")
×
862
                if "number_format" in kwargs:
×
863
                    l = l + "$\\chi^2$=" + kwargs["number_format"].format(chi2_val)
×
864
                else:
865
                    l = l + "$\\chi^2$=%s" % (chi2_val)
×
866
            except Exception:
×
867
                pass  # Ignore errors in Chi2 calculation
×
868

869
        if kwargs.get("append_r2", False):
5✔
870
            try:
×
871
                r2_val = ffit.R2(datax, datay, function, rfit, **kwargs)
×
872
                l = l + ("\n" if not kwargs["fitinline"] else " ")
×
873
                if "number_format" in kwargs:
×
874
                    l = l + "$R^2$=" + kwargs["number_format"].format(r2_val)
×
875
                else:
876
                    l = l + "$R^2$=%s" % (r2_val)
×
877
            except Exception:
×
878
                pass  # Ignore errors in R2 calculation
×
879

880
        if kwargs.get("append_ndf", False):
5✔
881
            try:
×
NEW
882
                ndf_val = ffit.Ndf(datax, datay, function, rfit, **kwargs)
×
883
                l = l + ("\n" if not kwargs["fitinline"] else " ")
×
884
                if "number_format" in kwargs:
×
NEW
885
                    l = l + "NDF=" + kwargs["number_format"].format(ndf_val)
×
886
                else:
NEW
887
                    l = l + "NDF=%s" % (ndf_val)
×
888
            except Exception:
×
NEW
889
                pass  # Ignore errors in NDF calculation
×
890

891
    return l
5✔
892

893

894
def plt_fit_or_interpolate(
5✔
895
    datax, datay, fitted, l=None, c=None, f=None, ls=None, **kwargs
896
):
897
    # just filter these kwargs out, so they dont get passed down and are replaced by above args
898
    # TODO why not pass label=XXX directly to this?
899
    #      -> probably since there are cases where both e.g. color and replace color are needed
900
    for key in ["color", "label", "fmt", "linestyle", "hatch"]:
5✔
901
        kwargs.pop(key, None)
5✔
902
    if kwargs["prange"] is None:
5✔
903
        x, _, _, _ = ffit.fit_split(datax, datay, **kwargs)
5✔
904
        xfit = kwargs["xspace"](np.min(unv(x)), np.max(unv(x)), kwargs["steps"])
5✔
905
    else:
906
        xfit = kwargs["xspace"](
×
907
            kwargs["prange"][0], kwargs["prange"][1], kwargs["steps"]
908
        )
909
    ll = __function(
5✔
910
        fitted,
911
        xfit,
912
        kwargs["fit_fmt"] if f is not None and ls is None else f,
913
        label=l,
914
        color=kwargs["fit_color"] if c is None else c,
915
        linestyle=ls,
916
        **kwargs,
917
    )
918

919
    if (
5✔
920
        (
921
            (kwargs["frange"] is not None or kwargs["fselector"] is not None)
922
            and util.true("extrapolate", kwargs)
923
        )
924
        or util.has("extrapolate_max", kwargs)
925
        or util.has("extrapolate_min", kwargs)
926
    ):
927
        xxfit = kwargs["xspace"](
5✔
928
            util.get("extrapolate_min", kwargs, np.min(unv(datax))),
929
            util.get("extrapolate_max", kwargs, np.max(unv(datax))),
930
            kwargs["steps"],
931
        )
932
        for pmin, pmax in [
5✔
933
            (np.min(xxfit), np.min(xfit)),
934
            (np.max(xfit), np.max(xxfit)),
935
        ]:
936
            __function(
5✔
937
                fitted,
938
                kwargs["xspace"](pmin, pmax, kwargs["steps"]),
939
                util.get("extrapolate_fmt", kwargs, "--"),
940
                color=ll.get_color(),
941
                hatch=util.get("extrapolate_hatch", kwargs, r"||"),
942
                **kwargs,
943
            )
944
    return ll.get_color(), xfit, fitted(xfit)
5✔
945

946

947
def plt_interpolate(datax, datay, icolor=None, **kwargs):
5✔
948
    """
949
    Interpolate and Plot that Interpolation.
950
    """
951
    inter = interpolate.interpolate(datax, datay, **kwargs)
5✔
952
    kargs = {}
5✔
953
    if isinstance(kwargs["interpolate_fmt"], tuple):
5✔
954
        kargs["ls"] = kwargs["interpolate_fmt"]
×
955
    else:
956
        kargs["f"] = kwargs["interpolate_fmt"]
5✔
957
    if kwargs["interpolate_label"] is not None:
5✔
958
        kargs["l"] = kwargs["interpolate_label"]
5✔
959
    # l = None so that no label
960
    return (
5✔
961
        inter,
962
        *plt_fit_or_interpolate(datax, datay, inter, c=icolor, **kargs, **kwargs),
963
    )
964

965

966
def plt_fit(datax, datay, gfunction, **kwargs):
5✔
967
    """
968
    Fit and Plot that Fit.
969
    """
970
    func = wrap.get_lambda(gfunction, kwargs["xvar"])
5✔
971
    rfit = _fit(datax, datay, gfunction, **kwargs)
5✔
972

973
    def fitted(x):
5✔
974
        return func(x, *rfit)
5✔
975

976
    vnames = wrap.get_varnames(gfunction, kwargs["xvar"])
5✔
977

978
    l = get_fnc_legend(gfunction, rfit, datax, datay, **kwargs)
5✔
979
    for v in vnames[1:]:  # remove fixed parameters from kwargs # APN TODO why?
5✔
980
        kwargs.pop(v, None)
5✔
981

982
    return (rfit, *plt_fit_or_interpolate(datax, datay, fitted, l, **kwargs))
5✔
983

984

985
def init_plot(kwargs):
5✔
986
    fig = None
5✔
987
    if util.has("axes", kwargs) and kwargs["axes"] is not None:
5✔
988
        plt.sca(kwargs["axes"])
5✔
989
        fig = kwargs["axes"].get_figure()
5✔
990
    if kwargs["init"] or util.true("residue", kwargs) or util.true("ratio", kwargs):
5✔
991
        if kwargs["size"] is None:
5✔
992
            fig = plt.figure()
5✔
993
        else:
994
            fig = plt.figure(figsize=kwargs["size"])
×
995
        if kwargs["residue"] or kwargs["ratio"]:
5✔
996
            fig.add_axes((0.1, 0.3, 0.8, 0.6))
5✔
997
    if util.has("next_color", kwargs) and not kwargs["next_color"]:
5✔
998
        lines = plt.gca()._get_lines
5✔
999
        tmp_color = lines._cycler_items[lines._idx]["color"]
5✔
1000

1001
        if kwargs["data_color"] is None:
5✔
1002
            kwargs["data_color"] = tmp_color
5✔
1003
        if kwargs["fit_color"] is None:
5✔
1004
            kwargs["fit_color"] = tmp_color
5✔
1005
        if kwargs["function_color"] is None:
5✔
1006
            kwargs["function_color"] = tmp_color
5✔
1007
    return fig
5✔
1008

1009

1010
def save_plot(**kwargs):
5✔
1011
    """
1012
    save plot
1013
    """
1014
    smplr.style_plot1d(**kwargs)
5✔
1015
    if "lpos" in kwargs and kwargs["lpos"] >= 0:
5✔
1016
        if util.has("bbox_to_anchor", kwargs):
5✔
1017
            if util.has("ncol", kwargs):
5✔
1018
                plt.legend(
5✔
1019
                    loc=kwargs["lpos"],
1020
                    bbox_to_anchor=kwargs["bbox_to_anchor"],
1021
                    ncol=kwargs["ncol"],
1022
                    borderaxespad=0,
1023
                )
1024
            else:
1025
                plt.legend(loc=kwargs["lpos"], bbox_to_anchor=kwargs["bbox_to_anchor"])
5✔
1026
        else:
1027
            plt.legend(loc=kwargs["lpos"])
5✔
1028
    if "save" in kwargs and kwargs["save"] is not None:
5✔
1029
        io.mkdirs(kwargs["save"])
×
1030
        plt.savefig(kwargs["save"] + ".pdf")
×
1031
    if kwargs.get("show"):
5✔
1032
        show(**kwargs)
5✔
1033

1034

1035
def show(**kwargs):
5✔
1036
    kwargs = plot_kwargs(kwargs)
5✔
1037

1038
    smplr.style_plot1d(**kwargs)
5✔
1039
    plt.show()
5✔
1040

1041

1042
from smpl.plot2d import plot2d as _plot2d
5✔
1043
from smpl.plot2d import plot2d_kwargs as _plot2d_kwargs
5✔
1044

1045

1046
def plot2d(*args, **kwargs):
5✔
1047
    _plot2d(*args, **kwargs)
5✔
1048

1049

1050
def plot2d_kwargs(*args, **kwargs):
5✔
1051
    _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