From 0a79d4cdfbfee8214dca938472029144a35f6b23 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Mon, 7 Mar 2022 12:24:47 +0100 Subject: [PATCH 1/5] `bin`: adding number of samples per bin to output --- .github/workflows/ci.yml | 1 + GooseMPL/__init__.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19d7686..40e22d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,7 @@ jobs: with: environment-file: environment.yaml environment-name: myenv + cache-env: true - name: Install library run: python -m pip install . diff --git a/GooseMPL/__init__.py b/GooseMPL/__init__.py index adfa216..38af19f 100644 --- a/GooseMPL/__init__.py +++ b/GooseMPL/__init__.py @@ -2153,6 +2153,9 @@ def bin(x: ArrayLike, y: ArrayLike, bin_edges: ArrayLike | int, use_median: bool yerr: std(y) for each bin. """ + x = np.array(x) + y = np.array(y) + j = np.digitize(x, bin_edges) - 1 n = bin_edges.size - 1 @@ -2161,6 +2164,7 @@ def bin(x: ArrayLike, y: ArrayLike, bin_edges: ArrayLike | int, use_median: bool "y": np.NaN * np.ones(n, dtype=float), "xerr": np.NaN * np.ones(n, dtype=float), "yerr": np.NaN * np.ones(n, dtype=float), + "n": np.zeros(n, dtype=int), } assert np.max(j) < ret["x"].size @@ -2171,6 +2175,7 @@ def bin(x: ArrayLike, y: ArrayLike, bin_edges: ArrayLike | int, use_median: bool continue sel = j == i + ret["n"][i] = np.sum(sel) if not use_median: ret["x"][i] = np.mean(x[sel]) From 0446c702c9242b44ae2f87123303e47ed070bb67 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Mon, 7 Mar 2022 12:30:44 +0100 Subject: [PATCH 2/5] Updating pre-commit --- .pre-commit-config.yaml | 20 ++--- .readthedocs.yml | 6 +- GooseMPL/__init__.py | 79 +++++++++++-------- docs/examples/goosempl/fit_powerlaw.py | 2 +- docs/examples/goosempl/fit_powerlaw_yerr.py | 2 +- docs/examples/goosempl/histogram.py | 2 +- docs/examples/goosempl/histogram_powerlaw.py | 2 +- docs/examples/goosempl/ticks_log.py | 2 +- docs/examples/pyplot/image.py | 2 +- docs/examples/pyplot/image_subplots.py | 2 +- docs/examples/pyplot/image_subplots_bottom.py | 2 +- docs/examples/pyplot/legend_background.py | 2 +- docs/examples/pyplot/plot-cmap.py | 2 +- docs/examples/pyplot/plot-cycler.py | 2 +- docs/examples/pyplot/tick-formatter.py | 2 +- test/main.py | 6 +- 16 files changed, 75 insertions(+), 60 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7cf9ce9..f644f05 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,32 +1,32 @@ repos: -# - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks -# rev: v2.1.0 -# hooks: -# - id: pretty-format-yaml -# args: [--autofix, --indent, '2'] - repo: https://github.com/psf/black - rev: 21.12b0 + rev: 22.1.0 hooks: - id: black args: [--safe, --quiet, --line-length=100] - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.1.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: debug-statements +- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks + rev: v2.3.0 + hooks: + - id: pretty-format-yaml + args: [--autofix, --indent, '2', --preserve-quotes] - repo: https://github.com/humitos/mirrors-autoflake.git rev: v1.1 hooks: - id: autoflake args: [--in-place, --remove-unused-variable] - repo: https://github.com/asottile/reorder_python_imports - rev: v2.6.0 + rev: v2.7.1 hooks: - id: reorder-python-imports - repo: https://github.com/asottile/pyupgrade - rev: v2.29.1 + rev: v2.31.0 hooks: - id: pyupgrade args: [--py36-plus] @@ -34,7 +34,7 @@ repos: rev: 4.0.1 hooks: - id: flake8 - args: ["--ignore=E722", --max-line-length=110, "--per-file-ignores=*/__init__.py:F401"] + args: [--max-line-length=100, "--ignore=E722", "--per-file-ignores=*/__init__.py:F401"] - repo: https://github.com/asottile/setup-cfg-fmt rev: v1.20.0 hooks: diff --git a/.readthedocs.yml b/.readthedocs.yml index 2d6875a..90a2357 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -3,7 +3,7 @@ # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details sphinx: - configuration: docs/conf.py + configuration: docs/conf.py version: 2 @@ -12,6 +12,6 @@ conda: python: install: - - method: pip - path: . + - method: pip + path: . system_packages: true diff --git a/GooseMPL/__init__.py b/GooseMPL/__init__.py index 38af19f..b9354fc 100644 --- a/GooseMPL/__init__.py +++ b/GooseMPL/__init__.py @@ -194,9 +194,9 @@ def latex_float(number, fmt="{0:.2g}"): if "e" in float_str: base, exponent = float_str.split("e") if base == "1": - return fr"10^{{{int(exponent)}}}" + return rf"10^{{{int(exponent)}}}" else: - return fr"{base} \times 10^{{{int(exponent)}}}" + return rf"{base} \times 10^{{{int(exponent)}}}" return float_str @@ -254,7 +254,7 @@ def log_ticks( exp_lower, exp_upper = lim ticks = np.logspace(exp_lower, exp_upper, exp_upper - exp_lower + 1, base=base) - labels = [fr"${base}^{{{np.log10(i):.0f}}}$" for i in ticks] + labels = [rf"${base}^{{{np.log10(i):.0f}}}$" for i in ticks] if keep is not None: keep = np.array(keep) @@ -356,7 +356,7 @@ def log_minorticks( for i in range(exp_lower, exp_upper): - t = (10 ** i) * np.arange(2, 10, dtype=float) + t = (10**i) * np.arange(2, 10, dtype=float) if i < 0: t = np.around(t, -i) @@ -743,9 +743,10 @@ def plot(x, y, units="absolute", axis=None, **kwargs): :options: **units** ([``'absolute'``] | ``'relative'``) - The type of units in which the coordinates are specified. Relative coordinates correspond - to a fraction of the relevant axis. If you use relative coordinates, be sure to set the - limits and scale before calling this function! + The type of units in which the coordinates are specified. + Relative coordinates correspond to a fraction of the relevant axis. + If you use relative coordinates, be sure to set the limits and scale before calling + this function! ... Any ``plt.plot(...)`` option. @@ -783,9 +784,10 @@ def text(x, y, text, units="absolute", axis=None, **kwargs): :options: **units** ([``'absolute'``] | ``'relative'``) - The type of units in which the coordinates are specified. Relative coordinates correspond - to a fraction of the relevant axis. If you use relative coordinates, be sure to set the - limits and scale before calling this function! + The type of units in which the coordinates are specified. + Relative coordinates correspond to a fraction of the relevant axis. + If you use relative coordinates, be sure to set the limits and scale before calling + this function! ... Any ``plt.text(...)`` option. @@ -820,7 +822,8 @@ def diagonal_powerlaw( The power-law exponent. **ll, lr, tl, tr** (````) - Coordinates of the lower-left, or the lower-right, or the top-left, or the top-right corner. + Coordinates of the lower-left, or the lower-right, or the top-left, + or the top-right corner. **width, height** (````) Width or the height. @@ -912,9 +915,10 @@ def annotate_powerlaw(text, exp, startx, starty, width=None, rx=0.5, ry=0.5, **k E.g. ``rx = 0.5, ry = 0.5`` corresponds to the middle of the line. **units** ([``'relative'``] | ``'absolute'``) - The type of units in which the coordinates are specified. Relative coordinates correspond - to a fraction of the relevant axis. If you use relative coordinates, be sure to set the - limits and scale before calling this function! + The type of units in which the coordinates are specified. + Relative coordinates correspond to a fraction of the relevant axis. + If you use relative coordinates, be sure to set the limits and scale before calling + this function! **axis** ([``plt.gca()``] | ...) Specify the axis to which to apply the limits. @@ -965,11 +969,11 @@ def annotate_powerlaw(text, exp, startx, starty, width=None, rx=0.5, ry=0.5, **k [starty, endy] = rel2abs_y([starty, endy], axis) # determine multiplication constant - const = starty / (startx ** exp) + const = starty / (startx**exp) # get end x/y-coordinate if endx is not None: - endy = const * endx ** exp + endy = const * endx**exp else: endx = (endy / const) ** (1 / exp) @@ -999,9 +1003,10 @@ def plot_powerlaw(exp, startx, starty, width=None, **kwargs): Definition of the end coordinate (only on of these options is needed). **units** ([``'relative'``] | ``'absolute'``) - The type of units in which the coordinates are specified. Relative coordinates correspond - to a fraction of the relevant axis. If you use relative coordinates, be sure to set the - limits and scale before calling this function! + The type of units in which the coordinates are specified. + Relative coordinates correspond to a fraction of the relevant axis. + If you use relative coordinates, be sure to set the limits and scale before calling + this function! **axis** ([``plt.gca()``] | ...) Specify the axis to which to apply the limits. @@ -1057,11 +1062,11 @@ def plot_powerlaw(exp, startx, starty, width=None, **kwargs): [starty, endy] = rel2abs_y([starty, endy], axis) # determine multiplication constant - const = starty / (startx ** exp) + const = starty / (startx**exp) # get end x/y-coordinate if endx is not None: - endy = const * endx ** exp + endy = const * endx**exp else: endx = (endy / const) ** (1 / exp) @@ -1075,8 +1080,8 @@ def plot_powerlaw(exp, startx, starty, width=None, **kwargs): def grid_powerlaw(exp, insert=0, skip=0, end=-1, step=0, axis=None, **kwargs): r""" - Draw a power-law grid: a grid that respects a certain power-law exponent. The grid-lines start from - the positions of the ticks. + Draw a power-law grid: a grid that respects a certain power-law exponent. + The grid-lines start from the positions of the ticks. :arguments: @@ -1399,9 +1404,9 @@ def fit_powerlaw( xl = np.logspace(np.log10(xl[0]), np.log10(xl[-1]), 1000) xu = np.logspace(np.log10(xu[0]), np.log10(xu[-1]), 1000) - yp = prefactor * xp ** exponent - yl = prefactor * xl ** exponent - yu = prefactor * xu ** exponent + yp = prefactor * xp**exponent + yl = prefactor * xl**exponent + yu = prefactor * xu**exponent details["handle"] = axis.plot(xp, yp, **kwargs) @@ -2016,7 +2021,8 @@ def histogram(data, return_edges=True, **kwargs): def histogram_cumulative(data, **kwargs): r""" Compute cumulative histogram. - See `numpy.histrogram `__ + See `numpy.histrogram + `__ :extra options: @@ -2204,8 +2210,16 @@ def patch(*args, **kwargs): fig,ax = plt.subplots() - p = gplt.patch(coor=coor+disp,conn=conn,axis=ax,cindex=stress,cmap='YlOrRd',edgecolor=None) - _ = gplt.patch(coor=coor ,conn=conn,axis=ax) + p = gplt.patch( + coor=coor + disp, + conn=conn, + axis=ax, + cindex=stress, + cmap='YlOrRd', + edgecolor=None + ) + + _ = gplt.patch(coor=coor, conn=conn, axis=ax) cbar = fig.colorbar(p,axis=ax,aspect=10) @@ -2222,7 +2236,8 @@ def patch(*args, **kwargs): Matrix with on each row the coordinates (positions) of each node. **conn** (```` | ```` (nested)) - Matrix with on each row the number numbers (rows in ``coor``) which form an element (patch). + Matrix with on each row the number numbers (rows in ``coor``) + which form an element (patch). :options: @@ -2233,8 +2248,8 @@ def patch(*args, **kwargs): Specify an axis to include to plot in. By default the current axis is used. **autoscale** ([``True``] | ``False``) - Automatically update the limits of the plot (currently automatic limits of Collections are - not supported by matplotlib). + Automatically update the limits of the plot + (currently automatic limits of Collections are not supported by matplotlib). :recommended options: diff --git a/docs/examples/goosempl/fit_powerlaw.py b/docs/examples/goosempl/fit_powerlaw.py index 04845b4..ea3542a 100644 --- a/docs/examples/goosempl/fit_powerlaw.py +++ b/docs/examples/goosempl/fit_powerlaw.py @@ -15,7 +15,7 @@ ax.set_ylim([1e-2, 1e4]) x = np.logspace(1, 3, 1000) -y = 0.001 * x ** 2.0 +y = 0.001 * x**2.0 y = y + 0.1 * np.random.random(x.shape) - 0.05 ax.plot(x, y) diff --git a/docs/examples/goosempl/fit_powerlaw_yerr.py b/docs/examples/goosempl/fit_powerlaw_yerr.py index c71ca5b..bbbd2d1 100644 --- a/docs/examples/goosempl/fit_powerlaw_yerr.py +++ b/docs/examples/goosempl/fit_powerlaw_yerr.py @@ -15,7 +15,7 @@ ax.set_ylim([1e-2, 1e4]) x = np.logspace(1, 3, 10) -y = 0.001 * x ** 2.0 +y = 0.001 * x**2.0 yerr = 0.1 * np.random.random(x.shape) * x sign = np.random.random(x.shape) sign = sign / np.abs(sign) diff --git a/docs/examples/goosempl/histogram.py b/docs/examples/goosempl/histogram.py index a648419..09331db 100644 --- a/docs/examples/goosempl/histogram.py +++ b/docs/examples/goosempl/histogram.py @@ -10,7 +10,7 @@ def distribution(a=100, b=3, g=-0.3, size=10000): r = np.random.random(size=size) - return (a ** g + (b ** g - a ** g) * r) ** (1.0 / g) + return (a**g + (b**g - a**g) * r) ** (1.0 / g) data = distribution() diff --git a/docs/examples/goosempl/histogram_powerlaw.py b/docs/examples/goosempl/histogram_powerlaw.py index 720b2b2..7b882bb 100644 --- a/docs/examples/goosempl/histogram_powerlaw.py +++ b/docs/examples/goosempl/histogram_powerlaw.py @@ -10,7 +10,7 @@ def distribution(a=100, b=3, g=-0.3, size=10000): r = np.random.random(size=size) - return (a ** g + (b ** g - a ** g) * r) ** (1.0 / g) + return (a**g + (b**g - a**g) * r) ** (1.0 / g) data = distribution() diff --git a/docs/examples/goosempl/ticks_log.py b/docs/examples/goosempl/ticks_log.py index 3b6c013..549c244 100644 --- a/docs/examples/goosempl/ticks_log.py +++ b/docs/examples/goosempl/ticks_log.py @@ -9,7 +9,7 @@ fig, ax = plt.subplots() x = np.logspace(1, 3, 1000) -y = 0.001 * x ** 2.0 +y = 0.001 * x**2.0 ax.plot(x, y) diff --git a/docs/examples/pyplot/image.py b/docs/examples/pyplot/image.py index 1f69edc..80eb768 100644 --- a/docs/examples/pyplot/image.py +++ b/docs/examples/pyplot/image.py @@ -4,7 +4,7 @@ plt.style.use(["goose", "goose-latex"]) x, y = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100)) -d = np.sqrt(x ** 2 + y ** 2) +d = np.sqrt(x**2 + y**2) fig, ax = plt.subplots() diff --git a/docs/examples/pyplot/image_subplots.py b/docs/examples/pyplot/image_subplots.py index c2d1d10..8ca145a 100644 --- a/docs/examples/pyplot/image_subplots.py +++ b/docs/examples/pyplot/image_subplots.py @@ -7,7 +7,7 @@ # --- some data ---- a, b = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100)) -d = np.sqrt(a ** 2 + b ** 2) +d = np.sqrt(a**2 + b**2) # --- open figure with three axes --- diff --git a/docs/examples/pyplot/image_subplots_bottom.py b/docs/examples/pyplot/image_subplots_bottom.py index a465a93..fb24088 100644 --- a/docs/examples/pyplot/image_subplots_bottom.py +++ b/docs/examples/pyplot/image_subplots_bottom.py @@ -7,7 +7,7 @@ # --- some data ---- a, b = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100)) -d = np.sqrt(a ** 2 + b ** 2) +d = np.sqrt(a**2 + b**2) # --- open figure with three axes --- diff --git a/docs/examples/pyplot/legend_background.py b/docs/examples/pyplot/legend_background.py index 9d6248e..fd02dd2 100644 --- a/docs/examples/pyplot/legend_background.py +++ b/docs/examples/pyplot/legend_background.py @@ -7,7 +7,7 @@ for i in range(10): x = [0, 1] y = [i / 10, (i + 10) / 10] - ax.plot(x, y, label=fr"$i = {i:d}$") + ax.plot(x, y, label=rf"$i = {i:d}$") ax.legend(loc="center right", facecolor="white", framealpha=1) diff --git a/docs/examples/pyplot/plot-cmap.py b/docs/examples/pyplot/plot-cmap.py index b3935e0..ff5c493 100644 --- a/docs/examples/pyplot/plot-cmap.py +++ b/docs/examples/pyplot/plot-cmap.py @@ -11,7 +11,7 @@ fig, ax = plt.subplots() for i, n in enumerate(np.linspace(0, 2, N)): - y = np.sin(x) * x ** n + y = np.sin(x) * x**n ax.plot(x, y, color=cmap(i)) ax.set_xlabel(r"$x$") diff --git a/docs/examples/pyplot/plot-cycler.py b/docs/examples/pyplot/plot-cycler.py index dc12b0e..86a9ee9 100644 --- a/docs/examples/pyplot/plot-cycler.py +++ b/docs/examples/pyplot/plot-cycler.py @@ -13,7 +13,7 @@ fig, ax = plt.subplots() for n in np.linspace(0, 2, N): - y = np.sin(x) * x ** n + y = np.sin(x) * x**n ax.plot(x, y) ax.set_xlabel(r"$x$") diff --git a/docs/examples/pyplot/tick-formatter.py b/docs/examples/pyplot/tick-formatter.py index c6ba15f..d0b07aa 100644 --- a/docs/examples/pyplot/tick-formatter.py +++ b/docs/examples/pyplot/tick-formatter.py @@ -5,7 +5,7 @@ plt.style.use(["goose", "goose-latex"]) x = np.linspace(0, 10, 101) -y = x ** 2.0 +y = x**2.0 fig, ax = plt.subplots() diff --git a/test/main.py b/test/main.py index acd7ed6..30110fe 100644 --- a/test/main.py +++ b/test/main.py @@ -130,7 +130,7 @@ class Test_fit_powerlaw(unittest.TestCase): def test_prefactor_exponent(self): x = np.linspace(0, 1, 1000) - y = 1.2 * x ** 3.4 + y = 1.2 * x**3.4 prefactor, exponent, _ = gplt.fit_powerlaw(x, y) self.assertTrue(np.isclose(prefactor, 1.2)) self.assertTrue(np.isclose(exponent, 3.4)) @@ -138,7 +138,7 @@ def test_prefactor_exponent(self): def test_prefactor(self): x = np.linspace(0, 1, 1000) - y = 1.2 * x ** 3.4 + y = 1.2 * x**3.4 prefactor, exponent, _ = gplt.fit_powerlaw(x, y, exponent=3.4) self.assertTrue(np.isclose(prefactor, 1.2)) self.assertTrue(np.isclose(exponent, 3.4)) @@ -146,7 +146,7 @@ def test_prefactor(self): def test_exponent(self): x = np.linspace(0, 1, 1000) - y = 1.2 * x ** 3.4 + y = 1.2 * x**3.4 prefactor, exponent, _ = gplt.fit_powerlaw(x, y, prefactor=1.2) self.assertTrue(np.isclose(prefactor, 1.2)) self.assertTrue(np.isclose(exponent, 3.4)) From 7cea5d7ad8ade36df5878c7933c3787519d03ab3 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Mon, 7 Mar 2022 13:48:47 +0100 Subject: [PATCH 3/5] Adding `fit_log` --- GooseMPL/__init__.py | 75 ++++++++++++++++++++++++++++++++++++++++++-- test/main.py | 38 ++++++++++++++++++++++ 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/GooseMPL/__init__.py b/GooseMPL/__init__.py index b9354fc..53b3bc7 100644 --- a/GooseMPL/__init__.py +++ b/GooseMPL/__init__.py @@ -1319,7 +1319,7 @@ def fit_powerlaw( with ``x`` replaced with the specified string. :param extrapolate: - Plot the powerlaw on the full range of ``axis.get_xlim()``. + Plot the function on the full range of ``axis.get_xlim()``. Instead of ``True``, one can specify plot options for the extrapolated line, e.g. ``..., extrapolate=dict(ls="--", c="r"), ...``. @@ -1471,7 +1471,7 @@ def fit_exp( with ``x`` replaced with the specified string. :param extrapolate: - Plot the powerlaw on the full range of ``axis.get_xlim()``. + Plot the function on the full range of ``axis.get_xlim()``. Instead of ``True'', one can specify plot options for the extrapolated line, e.g. ``..., extrapolate=dict(ls="--", c="r"), ...``. @@ -1570,6 +1570,75 @@ def fit_exp( return (prefactor, exponent, details) +def fit_log( + xdata: ArrayLike, + ydata: ArrayLike, + yerr: ArrayLike = None, + **kwargs, +) -> (float, float, dict): + r""" + Fit a logarithm :math:`y = a + b \ln x`. + See documentation of :py:func:`fit_linear`. + """ + + xdata = np.array(xdata) + ydata = np.array(ydata) + + i = xdata > 0 + logx = np.log(xdata[i]) + y = ydata[i] + + j = np.isnan(logx) + logx = logx[~j] + y = y[~j] + + axis = kwargs.pop("axis", None) + offset, slope, details = fit_linear(logx, y, **kwargs) + auto_fmt = kwargs.pop("auto_fmt", None) + extrapolate = kwargs.pop("extrapolate", False) + kwargs.pop("sigma", None) + kwargs.pop("absolute_sigma", None) + kwargs.pop("offset", None) + kwargs.pop("slope", None) + kwargs.pop("fmt", None) + + if auto_fmt: + n = 1 + len(auto_fmt) + details["label"] = details["label"][:-n] + auto_fmt + r"$" + + if axis is None: + return (offset, slope, details) + + if "label" in details: + kwargs["label"] = details["label"] + + xp = np.array([np.min(np.exp(logx)), np.max(np.exp(logx))]) + xl = np.array([axis.get_xlim()[0], xp[0]]) + xu = np.array([xp[1], axis.get_xlim()[1]]) + + if isinstance(extrapolate, bool) and extrapolate: + xp = np.array(axis.get_xlim()) + if xp[0] == 0: + xp[0] = np.finfo(np.float64).eps + + if axis.get_xscale() != "log": + xp = np.logspace(np.log10(xp[0]), np.log10(xp[-1]), 1000) + xl = np.logspace(np.log10(xl[0]), np.log10(xl[-1]), 1000) + xu = np.logspace(np.log10(xu[0]), np.log10(xu[-1]), 1000) + + yp = offset + slope * np.log(xp) + yl = offset + slope * np.log(xl) + yu = offset + slope * np.log(xu) + + details["handle"] = axis.plot(xp, yp, **kwargs) + + if isinstance(extrapolate, dict): + details["handle_lower"] = axis.plot(xl, yl, **extrapolate) + details["handle_upper"] = axis.plot(xu, yu, **extrapolate) + + return (offset, slope, details) + + def fit_linear( xdata: ArrayLike, ydata: ArrayLike, @@ -1605,7 +1674,7 @@ def fit_linear( with ``x`` replaced with the specified string. :param extrapolate: - Plot the powerlaw on the full range of ``axis.get_xlim()``. + Plot the function on the full range of ``axis.get_xlim()``. Instead of ``True``, one can specify plot options for the extrapolated line, e.g. ``..., extrapolate=dict(ls="--", c="r"), ...``. diff --git a/test/main.py b/test/main.py index 30110fe..fd3b5f4 100644 --- a/test/main.py +++ b/test/main.py @@ -190,6 +190,44 @@ def test_exponent(self): self.assertTrue(np.isclose(exponent, 3.4)) +class Test_fit_log(unittest.TestCase): + """ + Fit an logarithmic function. + """ + + def test_prefactor_exponent(self): + + x = np.linspace(0, 1, 1000)[1:] + y = 1.2 + 3.4 * np.log(x) + offset, prefactor, _ = gplt.fit_log(x, y) + self.assertTrue(np.isclose(offset, 1.2)) + self.assertTrue(np.isclose(prefactor, 3.4)) + + def test_prefactor_negative_prefactor(self): + + x = np.linspace(0, 1, 1000)[1:] + y = 1.2 - 3.4 * np.log(x) + offset, prefactor, _ = gplt.fit_log(x, y) + self.assertTrue(np.isclose(offset, 1.2)) + self.assertTrue(np.isclose(prefactor, -3.4)) + + def test_prefactor(self): + + x = np.linspace(0, 1, 1000)[1:] + y = 1.2 + 3.4 * np.log(x) + offset, prefactor, _ = gplt.fit_log(x, y, prefactor=3.4) + self.assertTrue(np.isclose(offset, 1.2)) + self.assertTrue(np.isclose(prefactor, 3.4)) + + def test_exponent(self): + + x = np.linspace(0, 1, 1000)[1:] + y = 1.2 + 3.4 * np.log(x) + offset, prefactor, _ = gplt.fit_log(x, y, offset=1.2) + self.assertTrue(np.isclose(offset, 1.2)) + self.assertTrue(np.isclose(prefactor, 3.4)) + + class Test_fit_linear(unittest.TestCase): """ Fit a linear. From ffda9ed9d88ac695e560341d88d9f63e5d9ed2db Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Mon, 7 Mar 2022 15:27:24 +0100 Subject: [PATCH 4/5] Fixing warning when LaTex is not found --- GooseMPL/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/GooseMPL/__init__.py b/GooseMPL/__init__.py index 53b3bc7..9b1c00c 100644 --- a/GooseMPL/__init__.py +++ b/GooseMPL/__init__.py @@ -150,7 +150,7 @@ def copy_style(): warnings.warn(message, Warning) - styles["goose-latex.mplstyle"] = {} + styles["goose-latex.mplstyle"] = None # write style definitions # ----------------------- @@ -165,7 +165,8 @@ def copy_style(): # write all styles for fname, style in styles.items(): with open(os.path.join(dirname, fname), "w") as file: - yaml.dump(style, file) + if style is not None: + yaml.dump(style, file) def latex_float(number, fmt="{0:.2g}"): From 2bb04deaf3ae0fec2f9d8d9c3c6539c29aed0a52 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Mon, 7 Mar 2022 17:02:26 +0100 Subject: [PATCH 5/5] Fixing warning in examples --- docs/examples/pyplot/plot.py | 2 +- docs/examples/pyplot/subplot.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/pyplot/plot.py b/docs/examples/pyplot/plot.py index fdc3ec5..d2b001b 100644 --- a/docs/examples/pyplot/plot.py +++ b/docs/examples/pyplot/plot.py @@ -12,8 +12,8 @@ ax.set_title("Simple plot") -ax.xaxis.set_ticklabels(["0", r"$\pi$", r"$2\pi$"]) ax.xaxis.set_ticks([0, np.pi, 2 * np.pi]) +ax.xaxis.set_ticklabels(["0", r"$\pi$", r"$2\pi$"]) ax.yaxis.set_ticks([-1, 0, 1]) ax.legend(loc="upper right") diff --git a/docs/examples/pyplot/subplot.py b/docs/examples/pyplot/subplot.py index fcf5a65..c5dd37f 100644 --- a/docs/examples/pyplot/subplot.py +++ b/docs/examples/pyplot/subplot.py @@ -21,8 +21,8 @@ ax1.set_title("First subplot") -ax1.xaxis.set_ticklabels(["0", r"$\pi$", r"$2\pi$"]) ax1.xaxis.set_ticks([0, np.pi, 2 * np.pi]) +ax1.xaxis.set_ticklabels(["0", r"$\pi$", r"$2\pi$"]) ax1.yaxis.set_ticks([-1, 0, 1]) ax1.legend(loc="upper right") @@ -39,8 +39,8 @@ ax2.set_title("Second subplot") -ax2.xaxis.set_ticklabels(["0", r"$\pi$", r"$2\pi$"]) ax2.xaxis.set_ticks([0, np.pi, 2 * np.pi]) +ax2.xaxis.set_ticklabels(["0", r"$\pi$", r"$2\pi$"]) ax2.yaxis.set_ticks([-1, 0, 1]) ax2.legend(loc="upper center")