Plotting grids
Create x-coordinates for plotting a curve over a fixed domain.
NumPy · Function
Generates a chosen number of evenly spaced NumPy samples between two boundaries. The upper boundary is included by default and can be excluded.
linspace constructs an array containing a requested number of evenly spaced samples between start and stop. By default the interval is closed, but endpoint=False excludes stop; retstep=True changes the result to a pair containing the samples and spacing. Array-valued boundaries are supported, with axis controlling the placement of the generated dimension.
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, *, device=None)| Name | Type | Description |
|---|---|---|
start | array_like | The starting value of the sequence. |
stop | array_like | The end value of the sequence, unless endpoint is set to False. |
num | int, optional | Number of samples to generate. Defaults to 50 and must be non-negative. |
endpoint | bool, optional | If True, stop is the last sample; otherwise it is not included. |
retstep | bool, optional | If True, return samples together with the spacing. |
dtype | dtype, optional | The type of the output array; otherwise inferred from start and stop. |
axis | int, optional | The axis in the result along which the samples are stored. |
device | str, optional | The device on which to place the array. |
An ndarray of evenly spaced samples; when retstep=True, a pair containing the samples and the spacing.
import numpy as np
samples, step = np.linspace(2.0, 3.0, num=5, retstep=True)
print(samples)
print(step)Creates five evenly spaced values including both endpoints and also returns their spacing.
Create x-coordinates for plotting a curve over a fixed domain.
Evaluate a model at uniformly spaced parameter values.
Build regularly spaced inputs for simulations or numerical integration.
It returns num evenly spaced samples over the interval from start to stop. With the default endpoint=True, both boundaries are included; endpoint=False makes the interval half-open by excluding stop.
Follow-up: How does retstep=True affect the result? It returns a two-item result containing the sample array and the spacing between samples.
num specifies how many samples to create and defaults to 50. It must be non-negative, while start and stop define the interval boundaries.
Follow-up: What happens when num is zero? The function produces an empty sample array; the requested count still satisfies the non-negative requirement.
For array-like start and stop, linspace generates samples along a new axis. The axis argument selects where that generated dimension is inserted, and dtype can specify the output type.
Follow-up: Why might axis matter in multidimensional calculations? It determines how the generated sequences align with existing array dimensions for broadcasting and downstream operations.
numpy.arangenumpy.geomspacenumpy.logspace