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

APN-Pucky / smpl / 15236750782

25 May 2025 10:07AM UTC coverage: 87.186% (-0.3%) from 87.5%
15236750782

push

github

web-flow
Update README.md

973 of 1116 relevant lines covered (87.19%)

4.36 hits per line

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

93.52
/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
    "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
    function = func
5✔
276
    if "function" in kwargs:
5✔
277
        function = kwargs["function"]
5✔
278
        del kwargs["function"]
5✔
279
        adata = [func, *adata]
5✔
280
    # Fix parameter order if necessary
281
    elif isinstance(function, (list, tuple, np.ndarray)):
5✔
282
        adata = [adata[-1], function, *adata[:-1]]
5✔
283
        function = adata[0]
5✔
284
        adata = adata[1:]
5✔
285
    if util.true("bins", kwargs):
5✔
286
        # yvalue will be overwritten
287
        ndata = [*adata, *adata]
5✔
288
        for i, o in enumerate(adata):
5✔
289
            ndata[2 * i] = o
5✔
290
            ndata[2 * i + 1] = o * 0
5✔
291
        adata = ndata
5✔
292

293
    assert len(adata) % 2 == 0, "data must be pairs of x and y data"
5✔
294
    if len(adata) == 2:
5✔
295
        datax, datay = adata
5✔
296
    else:
297
        rs = []
5✔
298
        for i in range(0, len(adata), 2):
5✔
299
            datax, datay = adata[i], adata[i + 1]
5✔
300
            if util.true("bins", kwargs):
5✔
301
                rs.append(fit(function, datax, **kwargs))
5✔
302
            else:
303
                rs.append(fit(function, datax, datay, **kwargs))
5✔
304
        return rs
5✔
305

306
    kwargs = plot_kwargs(kwargs)
5✔
307

308
    if np.any(np.iscomplex(datay)):
5✔
309
        label = util.get("label", kwargs, "")
5✔
310
        kwargs["label"] = label + "(real)"
5✔
311
        r = fit(datax, datay.real, function=function, **kwargs)
5✔
312
        kwargs["label"] = label + "(imag)"
5✔
313
        i = fit(datax, datay.imag, function=function, **kwargs)
5✔
314
        return r, i
5✔
315
    if kwargs["auto_fit"]:
5✔
316
        best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
5✔
317
        if best_f is not None:
5✔
318
            del kwargs["auto_fit"]
5✔
319
            fit(datax, datay, best_f, **kwargs)
5✔
320
        return best_f, best_ff, lambda_f
5✔
321
    if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
5✔
322
        kwargs["lpos"] = -1
5✔
323
    return _fit_impl(datax, datay, function, **kwargs)
5✔
324

325

326
def _fit_impl(datax, datay, function, **kwargs):
5✔
327
    x = None
5✔
328
    y = None
5✔
329
    rfit = None
5✔
330
    ifit = None
5✔
331
    fig = None
5✔
332
    fig = init_plot(kwargs)
5✔
333
    ll = None
5✔
334
    if kwargs["also_data"]:
5✔
335
        ll = plt_data(datax, datay, **kwargs).get_color()
5✔
336
    if kwargs["interpolate"]:
5✔
337
        ifit, _, x, y = plt_interpolate(datax, datay, icolor=ll, **kwargs)
5✔
338
    if kwargs["also_fit"]:
5✔
339
        assert function is not None, "function must be given"
5✔
340
        rfit, kwargs["fit_color"], _, _ = plt_fit(datax, datay, function, **kwargs)
5✔
341
    if kwargs["ss"]:
5✔
342
        kwargs["oldshow"] = kwargs["show"]
5✔
343
        kwargs["show"] = kwargs["show"] and not kwargs["residue"]
5✔
344
        save_plot(**kwargs)
5✔
345
        kwargs["show"] = kwargs["oldshow"]
5✔
346
    if kwargs["residue"] and fig is not None:
5✔
347
        plt_residue(datax, datay, function, rfit, fig, **kwargs)
5✔
348
    if not kwargs["also_fit"] and kwargs["interpolate"]:
5✔
349
        return (ifit, x, y)
5✔
350
        # return ifit
351
    return rfit
5✔
352

353

354
# @append_doc(default_kwargs)
355

356

357
def data(*data, function=None, **kwargs):
5✔
358
    """
359
    Plot datay against datax via :func:`fit`
360

361
    Parameters
362
    ----------
363
    datax : array_like
364
        X data either as ``unp.uarray`` or ``np.array`` or ``list``
365
    datay : array_like
366
        Y data either as ``unp.uarray`` or ``np.array`` or ``list``
367
    function : func,optional
368
        Fit function with parameters: ``x``, ``params``
369
    **kwargs : optional
370
        see :func:`plot_kwargs`.
371
    Returns
372
    -------
373
    array_like
374
        Optimized fit parameters of ``function`` to ``datax`` and ``datay``
375
    """
376
    if "also_fit" not in kwargs:
5✔
377
        kwargs["also_fit"] = False
5✔
378
    kwargs = plot_kwargs(kwargs)
5✔
379
    return fit(function=function, *data, **kwargs)
5✔
380

381

382
# @append_doc(default_kwargs)
383

384

385
def auto(*adata, funcs=None, **kwargs):
5✔
386
    """
387
    Automatically loop over functions and fit the best one.
388

389
    Parameters
390
    ----------
391
    funcs : function array
392
        functions to consider as fit. Default all ``smpl.functions``.
393
    **kwargs : optional
394
        see :func:`plot_kwargs`.
395

396
    Returns
397
    -------
398
    The best fit function and it's parameters. Also a lambda function where the parameters are already applied.
399

400

401

402
    """
403
    if "auto_fit" not in kwargs:
5✔
404
        kwargs["auto_fit"] = True
5✔
405
    kwargs = plot_kwargs(kwargs)
5✔
406
    return fit(function=funcs, *adata, **kwargs)
5✔
407

408

409
def _function(
5✔
410
    func,
411
    xfit,
412
    fmt="-",
413
    label=None,
414
    function_color=None,
415
    sigmas=0.0,
416
    alpha=0.4,
417
    **kwargs,
418
):
419
    # kargs = {}
420
    # if util.has("fmt", kwargs):
421
    #    kargs["fmt"] = kwargs["fmt"]
422
    # if util.has("label", kwargs) and kwargs["label"] != "":
423
    #    kargs["label"] = kwargs["label"]
424
    # if util.has("function_color", kwargs) and kwargs["function_color"] != "":
425
    #    kargs["color"] = kwargs["function_color"]
426
    # if util.has("sigmas", kwargs) and kwargs["sigmas"] != "":
427
    #    kargs["sigmas"] = kwargs["sigmas"]
428
    # if util.has("alpha", kwargs) and kwargs["alpha"] != "":
429
    #    kargs["alpha"] = kwargs["alpha"]
430
    # __function(func, xfit,  **kargs)
431

432
    if label == "":
5✔
433
        label = None
×
434
    if function_color == "":
5✔
435
        function_color = None
×
436
    if sigmas == "":
5✔
437
        sigmas = 0.0
×
438
    if alpha == "":
5✔
439
        alpha = 0.4
×
440
    __function(
5✔
441
        func,
442
        xfit,
443
        fmt=fmt,
444
        label=label,
445
        color=function_color,
446
        sigmas=sigmas,
447
        alpha=alpha,
448
        **kwargs,
449
    )
450

451

452
def plt_plt(x, y, fmt, color, label, linestyle, **kwargs):
5✔
453
    if linestyle is None and fmt is not None:
5✔
454
        return plt.plot(x, y, fmt, label=label, color=color, **kwargs)
5✔
455
    if linestyle is not None and fmt is None:
5✔
456
        return plt.plot(x, y, label=label, color=color, linestyle=linestyle, **kwargs)
×
457
    if linestyle is None and fmt is None:
5✔
458
        return plt.plot(x, y, label=label, color=color, **kwargs)
5✔
459

460

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

547
    if isinstance(func(x)[0], uncertainties.UFloat):
5✔
548
        if sigmas > 0:
5✔
549
            (ll,) = plt_plt(
5✔
550
                x,
551
                unv(func(x)),
552
                fmt,
553
                label=None,
554
                color=color,
555
                linestyle=linestyle,
556
                **kwargs,
557
            )
558
            y = func(x)
5✔
559
            plt.fill_between(
5✔
560
                x,
561
                unv(y) - sigmas * usd(y),
562
                unv(y) + sigmas * usd(y),
563
                alpha=alpha,
564
                label=l,
565
                color=ll.get_color(),
566
                hatch=hatch,
567
                **kwargs,
568
            )
569
        else:
570
            (ll,) = plt_plt(
5✔
571
                x,
572
                unv(func(x)),
573
                fmt,
574
                label=l,
575
                color=color,
576
                linestyle=linestyle,
577
                **kwargs,
578
            )
579
    else:
580
        (ll,) = plt_plt(
5✔
581
            x, func(x), fmt, label=l, color=color, linestyle=linestyle, **kwargs
582
        )
583
    return ll
5✔
584

585

586
def function(func, *args, **kwargs):
5✔
587
    """
588
    Plot function ``func`` between ``xmin`` and ``xmax``
589

590
    Parameters
591
    ----------
592
    func : function
593
        Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
594
    *args : optional
595
        arguments for ``func``
596
    **kwargs : optional
597
        see :func:`plot_kwargs`.
598
    """
599
    if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
5✔
600
        kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
5✔
601
        # raise Exception("xmin or xmax missing.")
602

603
    # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
604
    #    kwargs['lpos'] = -1
605
    if not util.has("fmt", kwargs):
5✔
606
        kwargs["fmt"] = "-"
5✔
607

608
    if "label" not in kwargs:
5✔
609
        kwargs = plot_kwargs(kwargs)
5✔
610
        kwargs["label"] = get_fnc_legend(func, args, **kwargs)
5✔
611
    else:
612
        kwargs = plot_kwargs(kwargs)
5✔
613

614
    xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
5✔
615
    init_plot(kwargs)
5✔
616

617
    # kwargs['lpos'] = 0
618
    # _plot(xfit, func(xfit, *args), **kwargs)
619
    _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
5✔
620
    if kwargs["ss"]:
5✔
621
        save_plot(**kwargs)
5✔
622

623

624
# xaxis="",yaxis="",fit_color=None,save = None,residue_err=True,show=False):
625
def plt_residue(datax, datay, gfunction, rfit, fig, **kwargs):
5✔
626
    function = wrap.get_lambda(gfunction, kwargs["xvar"])
5✔
627
    fig.add_axes((0.1, 0.1, 0.8, 0.2))
5✔
628
    kwargs["yaxis"] = "$\\Delta$" + kwargs["yaxis"]
5✔
629
    kwargs["data_color"] = kwargs["fit_color"]
5✔
630

631
    if kwargs["residue_err"]:
5✔
632
        plt_data(datax, datay - function(datax, *rfit), **kwargs)
5✔
633
    else:
634
        plt_data(unv(datax), unv(datay - function(datax, *rfit)), **kwargs)
5✔
635
    kwargs["lpos"] = -1
5✔
636
    save_plot(**kwargs)
5✔
637

638

639
def data_split(datax, datay, **kwargs):
5✔
640
    return ffit.data_split(datax, datay, **kwargs)
5✔
641

642

643
def _fit(datax, datay, function, **kwargs):
5✔
644
    """
645
    Returns a fit like :func:`fit` but does no plotting.
646
    """
647
    return ffit.fit(datax, datay, function, **kwargs)
5✔
648

649

650
def plt_data(datax, datay, **kwargs):
5✔
651
    """
652
    Plot datay vs datax
653
    """
654
    x, y, xerr, yerr = data_split(datax, datay, **kwargs)
5✔
655
    if xerr is not None:
5✔
656
        xerr = xerr * kwargs["data_sigmas"]
5✔
657
    if yerr is not None:
5✔
658
        yerr = yerr * kwargs["data_sigmas"]
5✔
659

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

785

786
def get_fnc_legend(function, rfit, **kwargs):
5✔
787
    l = wrap.get_latex(function)
5✔
788

789
    vnames = wrap.get_varnames(function, kwargs["xvar"])
5✔
790
    for i in range(1, len(vnames)):
5✔
791
        l = l + ("\n" if not kwargs["fitinline"] or i == 1 else " ")
5✔
792
        l = l + "$" + sympy.latex(sympy.symbols(str(vnames[i]))) + "$="
5✔
793
        if kwargs["units"] is not None and usd(rfit[i - 1]) > 0:
5✔
794
            l = l + "("
5✔
795
        if "number_format" in kwargs:
5✔
796
            l = l + kwargs["number_format"].format(rfit[i - 1])
5✔
797
        else:
798
            l = l + "%s" % (rfit[i - 1])
×
799

800
        if kwargs["units"] is not None and usd(rfit[i - 1]) > 0:
5✔
801
            l = l + ")"
5✔
802
        if kwargs["units"] is not None:
5✔
803
            l = l + " " + kwargs["units"][i - 1]
5✔
804
    return l
5✔
805

806

807
def plt_fit_or_interpolate(
5✔
808
    datax, datay, fitted, l=None, c=None, f=None, ls=None, **kwargs
809
):
810
    # just filter these kwargs out, so they dont get passed down and are replaced by above args
811
    # TODO why not pass label=XXX directly to this?
812
    #      -> probably since there are cases where both e.g. color and replace color are needed
813
    for key in ["color", "label", "fmt", "linestyle", "hatch"]:
5✔
814
        kwargs.pop(key, None)
5✔
815
    if kwargs["prange"] is None:
5✔
816
        x, _, _, _ = ffit.fit_split(datax, datay, **kwargs)
5✔
817
        xfit = kwargs["xspace"](np.min(unv(x)), np.max(unv(x)), kwargs["steps"])
5✔
818
    else:
819
        xfit = kwargs["xspace"](
×
820
            kwargs["prange"][0], kwargs["prange"][1], kwargs["steps"]
821
        )
822
    ll = __function(
5✔
823
        fitted,
824
        xfit,
825
        kwargs["fit_fmt"] if f is not None and ls is None else f,
826
        label=l,
827
        color=kwargs["fit_color"] if c is None else c,
828
        linestyle=ls,
829
        **kwargs,
830
    )
831

832
    if (
5✔
833
        (
834
            (kwargs["frange"] is not None or kwargs["fselector"] is not None)
835
            and util.true("extrapolate", kwargs)
836
        )
837
        or util.has("extrapolate_max", kwargs)
838
        or util.has("extrapolate_min", kwargs)
839
    ):
840
        xxfit = kwargs["xspace"](
5✔
841
            util.get("extrapolate_min", kwargs, np.min(unv(datax))),
842
            util.get("extrapolate_max", kwargs, np.max(unv(datax))),
843
            kwargs["steps"],
844
        )
845
        for pmin, pmax in [
5✔
846
            (np.min(xxfit), np.min(xfit)),
847
            (np.max(xfit), np.max(xxfit)),
848
        ]:
849
            __function(
5✔
850
                fitted,
851
                kwargs["xspace"](pmin, pmax, kwargs["steps"]),
852
                util.get("extrapolate_fmt", kwargs, "--"),
853
                color=ll.get_color(),
854
                hatch=util.get("extrapolate_hatch", kwargs, r"||"),
855
                **kwargs,
856
            )
857
    return ll.get_color(), xfit, fitted(xfit)
5✔
858

859

860
def plt_interpolate(datax, datay, icolor=None, **kwargs):
5✔
861
    """
862
    Interpolate and Plot that Interpolation.
863
    """
864
    inter = interpolate.interpolate(datax, datay, **kwargs)
5✔
865
    kargs = {}
5✔
866
    if isinstance(kwargs["interpolate_fmt"], tuple):
5✔
867
        kargs["ls"] = kwargs["interpolate_fmt"]
×
868
    else:
869
        kargs["f"] = kwargs["interpolate_fmt"]
5✔
870
    if kwargs["interpolate_label"] is not None:
5✔
871
        kargs["l"] = kwargs["interpolate_label"]
5✔
872
    # l = None so that no label
873
    return (
5✔
874
        inter,
875
        *plt_fit_or_interpolate(datax, datay, inter, c=icolor, **kargs, **kwargs),
876
    )
877

878

879
def plt_fit(datax, datay, gfunction, **kwargs):
5✔
880
    """
881
    Fit and Plot that Fit.
882
    """
883
    func = wrap.get_lambda(gfunction, kwargs["xvar"])
5✔
884
    rfit = _fit(datax, datay, gfunction, **kwargs)
5✔
885

886
    def fitted(x):
5✔
887
        return func(x, *rfit)
5✔
888

889
    vnames = wrap.get_varnames(gfunction, kwargs["xvar"])
5✔
890
    for v in vnames[1:]:  # remove fixed parameters from kwargs
5✔
891
        kwargs.pop(v, None)
5✔
892

893
    l = get_fnc_legend(gfunction, rfit, **kwargs)
5✔
894
    return (rfit, *plt_fit_or_interpolate(datax, datay, fitted, l, **kwargs))
5✔
895

896

897
def init_plot(kwargs):
5✔
898
    fig = None
5✔
899
    if util.has("axes", kwargs) and kwargs["axes"] is not None:
5✔
900
        plt.sca(kwargs["axes"])
5✔
901
        fig = kwargs["axes"].get_figure()
5✔
902
    if kwargs["init"] or util.true("residue", kwargs):
5✔
903
        if kwargs["size"] is None:
5✔
904
            fig = plt.figure()
5✔
905
        else:
906
            fig = plt.figure(figsize=kwargs["size"])
×
907
        if kwargs["residue"]:
5✔
908
            fig.add_axes((0.1, 0.3, 0.8, 0.6))
5✔
909
    if util.has("next_color", kwargs) and not kwargs["next_color"]:
5✔
910
        lines = plt.gca()._get_lines
5✔
911
        tmp_color = lines._cycler_items[lines._idx]["color"]
5✔
912

913
        if kwargs["data_color"] is None:
5✔
914
            kwargs["data_color"] = tmp_color
5✔
915
        if kwargs["fit_color"] is None:
5✔
916
            kwargs["fit_color"] = tmp_color
5✔
917
        if kwargs["function_color"] is None:
5✔
918
            kwargs["function_color"] = tmp_color
5✔
919
    return fig
5✔
920

921

922
def save_plot(**kwargs):
5✔
923
    """
924
    save plot
925
    """
926
    smplr.style_plot1d(**kwargs)
5✔
927
    if "lpos" in kwargs and kwargs["lpos"] >= 0:
5✔
928
        if util.has("bbox_to_anchor", kwargs):
5✔
929
            if util.has("ncol", kwargs):
5✔
930
                plt.legend(
5✔
931
                    loc=kwargs["lpos"],
932
                    bbox_to_anchor=kwargs["bbox_to_anchor"],
933
                    ncol=kwargs["ncol"],
934
                    borderaxespad=0,
935
                )
936
            else:
937
                plt.legend(loc=kwargs["lpos"], bbox_to_anchor=kwargs["bbox_to_anchor"])
5✔
938
        else:
939
            plt.legend(loc=kwargs["lpos"])
5✔
940
    if "save" in kwargs and kwargs["save"] is not None:
5✔
941
        io.mkdirs(kwargs["save"])
×
942
        plt.savefig(kwargs["save"] + ".pdf")
×
943
    if kwargs.get("show"):
5✔
944
        show(**kwargs)
5✔
945

946

947
def show(**kwargs):
5✔
948
    kwargs = plot_kwargs(kwargs)
5✔
949

950
    smplr.style_plot1d(**kwargs)
5✔
951
    plt.show()
5✔
952

953

954
from smpl.plot2d import plot2d as _plot2d
5✔
955
from smpl.plot2d import plot2d_kwargs as _plot2d_kwargs
5✔
956

957

958
def plot2d(*args, **kwargs):
5✔
959
    _plot2d(*args, **kwargs)
5✔
960

961

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