• 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

61.74
/smpl/plot2d.py
1
import matplotlib.pyplot as plt
5✔
2
import numpy as np
5✔
3
import smplr
5✔
4
from matplotlib import colors
5✔
5
from matplotlib.image import NonUniformImage
5✔
6

7
from smpl import doc, util
5✔
8
from smpl import plot as splot
5✔
9

10
default = {
5✔
11
    "title": [None, "Plot title"],
12
    "xlabel": [None, "."],
13
    "ylabel": [None, "."],
14
    "zlabel": [None, "."],
15
    "logz": [True, "Colorbar in logarithmic scale."],
16
    "style": [
17
        "pcolormesh",
18
        "Plot via an image ('image') or scatter ('scatter') or mesh ('pcolormesh').",
19
    ],
20
    "interpolation": [
21
        "nearest",
22
        "Only 'nearest' or 'bilinear' for nonuniformimage. Check https://matplotlib.org/stable/gallery/images_contours_and_fields/interpolation_methods.html#interpolations-for-imshow",
23
    ],
24
    "cmap": [
25
        "viridis",
26
        "Good default color map for missing datapoints since it does not include white.",
27
    ],
28
    # 'zscale' : [None,"Rescale z values."],
29
}
30

31
# @doc.insert_str("\tDefault kwargs\n\n\t")
32

33

34
@doc.append_str(doc.array_table(default, init=False))
5✔
35
@doc.append_str(
5✔
36
    doc.array_table({"plot2d_kwargs": ["default", "description"]}, bottom=False)
37
)
38
def plot2d_kwargs(kwargs):
5✔
39
    """Set default plot2d_kwargs if not set."""
40
    for k, v in default.items():
5✔
41
        if k not in kwargs:
5✔
42
            kwargs[k] = v[0]
5✔
43
    return kwargs
5✔
44

45

46
def plot2d(datax, datay, dataz, **kwargs):
5✔
47
    """
48
    Creates a 2D-Plot.
49

50
    Parameters
51
    ----------
52
    **kwargs : optional
53
        see :func:`plot2d_kwargs`.
54
    """
55
    kwargs = plot2d_kwargs(kwargs)
5✔
56
    if "xaxis" in kwargs and ("xlabel" not in kwargs or not kwargs["xlabel"]):
5✔
57
        # warnings.warn("xaxis is deprecated. Use xlabel instead.", DeprecationWarning, 2)
UNCOV
58
        kwargs["xlabel"] = kwargs["xaxis"]
×
59
    if "yaxis" in kwargs and ("ylabel" not in kwargs or not kwargs["ylabel"]):
5✔
60
        # warnings.warn("yaxis is deprecated. Use ylabel instead.", DeprecationWarning, 2)
UNCOV
61
        kwargs["ylabel"] = kwargs["yaxis"]
×
62
    if "zaxis" in kwargs and ("zlabel" not in kwargs or not kwargs["zlabel"]):
5✔
63
        # warnings.warn("zaxis is deprecated. Use zlabel instead.", DeprecationWarning, 2)
UNCOV
64
        kwargs["zlabel"] = kwargs["zaxis"]
×
65

66
    if util.has("axes", kwargs) and kwargs["axes"] is not None:
5✔
67
        plt.sca(kwargs["axes"])
×
68
    if kwargs["style"] == "pcolormesh":
5✔
69
        pcolormesh_vplot(datax, datay, dataz, **kwargs)
5✔
70
    elif kwargs["style"] == "image":
5✔
71
        map_vplot(datax, datay, dataz, **kwargs)
×
72
    elif kwargs["style"] == "scatter":
5✔
73
        scatter_vplot(datax, datay, dataz, **kwargs)
5✔
74

75

76
def sort_xyz(x, y, z):
5✔
77
    p1 = x.argsort(kind="stable")
5✔
78
    x = np.copy(x[p1])
5✔
79
    y = np.copy(y[p1])
5✔
80
    z = np.copy(z[p1])
5✔
81
    p2 = y.argsort(kind="stable")
5✔
82
    x = x[p2]
5✔
83
    y = y[p2]
5✔
84
    z = z[p2]
5✔
85
    return x, y, z
5✔
86

87

88
def pcolormesh_vplot(
5✔
89
    tvx,
90
    tvy,
91
    tvz,
92
    xlabel=None,
93
    ylabel=None,
94
    zlabel=None,
95
    logz=True,
96
    zscale=1.0,
97
    **kwargs,
98
):
99
    """
100
    Advantage over matplotlibs pcolor(mesh) is that does not require a meshgrid. Instead it uses the data points directly in three lists.
101
    """
102
    vx = np.copy(tvx)
5✔
103
    vy = np.copy(tvy)
5✔
104
    vz = np.copy(tvz)
5✔
105
    assert vx.shape == vy.shape == vz.shape
5✔
106

107
    if len(vz.shape) < 2:
5✔
108
        mesh = np.meshgrid(np.unique(vx), np.unique(vy))
5✔
109
        X, Y = mesh
5✔
110
        # set Z to values of vz on the meshgrid
111
        Z = np.empty(mesh[0].shape)
5✔
112
        Z[:] = np.nan
5✔
113
        for i, _ in enumerate(vx):
5✔
114
            Z[(mesh[0] == vx[i]) & (mesh[1] == vy[i])] = splot.unv(vz[i])
5✔
115
        Z[:] *= zscale
5✔
116
    else:
117
        X = vx
5✔
118
        Y = vy
5✔
119
        Z = vz * zscale
5✔
120

121
    plt.pcolormesh(
5✔
122
        X, Y, Z, norm=colors.LogNorm() if logz else None, cmap=kwargs["cmap"]
123
    )
124

125
    # ax.set_xlim(xl, xm)
126
    # ax.set_ylim(yl, ym)
127

128
    cb = plt.colorbar()
5✔
129
    cb.set_label(zlabel)
5✔
130
    smplr.style_plot2d(xlabel=xlabel, ylabel=ylabel, **kwargs)
5✔
131

132

133
def map_vplot(
5✔
134
    tvx,
135
    tvy,
136
    tvz,
137
    xlabel=None,
138
    ylabel=None,
139
    zlabel=None,
140
    logz=True,
141
    sort=True,
142
    fill_missing=True,
143
    zscale=1.0,
144
    **kwargs,
145
):
146
    """ """
147
    vx = np.copy(tvx)
×
148
    vy = np.copy(tvy)
×
149
    vz = np.copy(tvz)
×
150
    if fill_missing:
×
151
        # TODO speed up
152
        for x in vx:
×
153
            for y in vy:
×
154
                ex = np.any(np.logical_and((vx == x), (vy == y)))
×
155
                if not ex:
×
156
                    vx = np.append(vx, x)
×
157
                    vy = np.append(vy, y)
×
158
                    vz = np.append(vz, 0)
×
159
    if sort:
×
160
        vx, vy, vz = sort_xyz(vx, vy, vz)
×
161

162
    s = 1
×
163
    while vy[s] == vy[s - 1]:
×
164
        s = s + 1
×
165
    if s == 1:
×
166
        # print("flipped x y ")
167
        while vx[s] == vx[s - 1]:
×
168
            s = s + 1
×
169
        if s == 1:
×
170
            print("error too small map")
×
171
            return
×
172
        # x, y = y, x
173
        xlabel, ylabel = ylabel, xlabel
×
174
        vx, vy = vy, vx
×
175

176
    grid = splot.unv(vz).reshape((int(np.rint(np.size(vx) / s)), s)) * zscale
×
177

178
    _, ax = plt.subplots(nrows=1, ncols=1, constrained_layout=True)
×
179
    im = None
×
180
    xl = vx.min() + (vx.min() / 2) - vx[vx != vx.min()].min() / 2
×
181
    xm = vx.max() + (vx.max() / 2) - vx[vx != vx.max()].max() / 2
×
182
    yl = vy.min() + (vy.min() / 2) - vy[vy != vy.min()].min() / 2
×
183
    ym = vy.max() + (vy.max() / 2) - vy[vy != vy.max()].max() / 2
×
184
    im = NonUniformImage(
×
185
        ax,
186
        origin="lower",
187
        cmap=kwargs["cmap"],
188
        interpolation=kwargs["interpolation"],
189
        extent=(xl, xm, yl, ym),
190
        norm=colors.LogNorm() if logz else None,
191
    )
192

193
    im.set_data(np.unique(vx), np.unique(vy), grid)
×
194
    ax.add_image(im)
×
195
    # ax.images.append(im)
196
    ax.set_xlim(xl, xm)
×
197
    ax.set_ylim(yl, ym)
×
198

199
    cb = plt.colorbar(im)
×
200
    cb.set_label(zlabel)
×
201
    smplr.style_plot2d(xlabel=xlabel, ylabel=ylabel, **kwargs)
×
202

203

204
def scatter_vplot(
5✔
205
    vx,
206
    vy,
207
    vz,
208
    xlabel=None,
209
    ylabel=None,
210
    zlabel=None,
211
    logz=True,
212
    sort=True,
213
    **kwargs,
214
):
215
    if sort:
5✔
216
        vx, vy, vz = sort_xyz(vx, vy, vz)
5✔
217

218
    _, ax = plt.subplots(nrows=1, ncols=1, constrained_layout=True)
5✔
219
    xl = vx.min() + (vx.min() / 2) - vx[vx != vx.min()].min() / 2
5✔
220
    xm = vx.max() + (vx.max() / 2) - vx[vx != vx.max()].max() / 2
5✔
221
    yl = vy.min() + (vy.min() / 2) - vy[vy != vy.min()].min() / 2
5✔
222
    ym = vy.max() + (vy.max() / 2) - vy[vy != vy.max()].max() / 2
5✔
223

224
    s = plt.scatter(
5✔
225
        np.concatenate((vx, vx, vx)),
226
        np.concatenate((vy, vy, vy)),
227
        c=np.concatenate(
228
            (
229
                splot.unv(vz) + splot.usd(vz),
230
                splot.unv(vz) - splot.usd(vz),
231
                splot.unv(vz),
232
            )
233
        ),
234
        s=np.concatenate(
235
            (
236
                [(3 * plt.rcParams["lines.markersize"]) ** 2 for i in range(len(vx))],
237
                [(2 * plt.rcParams["lines.markersize"]) ** 2 for i in range(len(vx))],
238
                [(plt.rcParams["lines.markersize"]) ** 2 for i in range(len(vx))],
239
            )
240
        ),
241
        norm=colors.LogNorm() if logz else None,
242
        cmap=kwargs["cmap"],
243
    )
244

245
    ax.set_xlim(xl, xm)
5✔
246
    ax.set_ylim(yl, ym)
5✔
247

248
    cb = plt.colorbar(s)
5✔
249
    cb.set_label(zlabel)
5✔
250
    smplr.style_plot2d(xlabel=xlabel, ylabel=ylabel, **kwargs)
5✔
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