Coverage for chebpy/core/plotting.py: 100%
20 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-07 10:30 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-07 10:30 +0000
1"""Plotting utilities for visualizing functions and their properties.
3This module provides functions for plotting functions, their coefficients, and
4other visualizations useful for understanding function approximations. It uses
5matplotlib for the actual plotting, but is designed to gracefully handle cases
6where matplotlib is not available.
8The main functions are:
9- plotfun: Plot a function over a specified interval
10- plotfuncoeffs: Plot the coefficients of a function on a semilogy scale
12These functions are typically used by higher-level classes like Chebfun and
13Chebtech to provide plotting capabilities.
14"""
16import matplotlib.pyplot as plt
17import numpy as np
19from .settings import _preferences as prefs
22def plotfun(fun: callable, support: tuple, ax=None, n: int = None, **kwds) -> object:
23 """Plot a function over a specified support interval.
25 This function plots a callable object over a specified interval using
26 matplotlib. For complex-valued functions, it plots the real part against
27 the imaginary part.
29 Args:
30 fun (callable): The function to plot. Must be callable and have an
31 'iscomplex' attribute.
32 support (tuple): A tuple specifying the interval [a, b] over which to plot.
33 ax (matplotlib.axes.Axes, optional): The axes on which to plot. If None,
34 a new axes will be created. Defaults to None.
35 n (int, optional): Number of points to use for plotting. If None, uses
36 the value from preferences. Defaults to None.
37 **kwds: Additional keyword arguments to pass to matplotlib's plot function.
39 Returns:
40 matplotlib.axes.Axes: The axes on which the plot was created.
41 """
42 ax = ax or plt.gca()
43 n = n if n is not None else prefs.N_plot
44 xx = np.linspace(*support, n)
45 ff = fun(xx)
46 if fun.iscomplex:
47 ax.plot(np.real(ff), np.imag(ff), **kwds)
48 ax.set_xlabel(kwds.pop("ylabel", "real"))
49 ax.set_ylabel(kwds.pop("xlabel", "imag"))
50 else:
51 ax.plot(xx, ff, **kwds)
52 return ax
55def plotfuncoeffs(abscoeffs: np.ndarray, ax=None, **kwds) -> object:
56 """Plot the absolute values of function coefficients on a semilogy scale.
58 This function creates a semilogy plot of the absolute values of function
59 coefficients, which is useful for visualizing the decay of coefficients
60 in a Chebyshev series.
62 Args:
63 abscoeffs (numpy.ndarray): Array of absolute coefficient values to plot.
64 ax (matplotlib.axes.Axes, optional): The axes on which to plot. If None,
65 a new axes will be created. Defaults to None.
66 **kwds: Additional keyword arguments to pass to matplotlib's semilogy function.
68 Returns:
69 matplotlib.axes.Axes: The axes on which the plot was created.
70 """
71 ax = ax or plt.gca()
72 ax.set_ylabel(kwds.pop("xlabel", "coefficient magnitude"))
73 ax.set_xlabel(kwds.pop("ylabel", "polynomial degree"))
74 ax.semilogy(abscoeffs, **kwds)
75 return ax