actually there is a specific method for this, 'clip':
import numpy as np
my_array = np.array([[100, 200], [300, 400]],np.uint16)
my_array.clip(0,255) # clip(min, max)
output:
array([[100, 200],
[255, 255]], dtype=uint16)
Answer from Andrea Zonca on Stack OverflowNumPy
numpy.org › doc › stable › reference › generated › numpy.clip.html
numpy.clip — NumPy v2.5 Manual
Minimum and maximum value. If None, clipping is not performed on the corresponding edge. If both a_min and a_max are None, the elements of the returned array stay the same.
Educative
educative.io › answers › what-is-the-clip-function-for-a-2-d-array-in-python
What is the clip() function for a 2-D array in Python?
The numpy.clip() function is used to clip a limit value in an input array.
Can NumPy clip use array bounds?
Yes. Lower and upper bounds can be array-like when they broadcast against the input array.
pythonpool.com
pythonpool.com › home › numpy › numpy clip(): limit array values with bounds
NumPy clip(): Limit Array Values with Bounds
How do I limit values in a NumPy array?
Call np.clip(array, a_min, a_max) to return values bounded by inclusive lower and upper limits.
pythonpool.com
pythonpool.com › home › numpy › numpy clip(): limit array values with bounds
NumPy clip(): Limit Array Values with Bounds
Does np.clip change the original array?
Not by default; it returns a result. Pass out=array only when intentional in-place mutation is safe.
pythonpool.com
pythonpool.com › home › numpy › numpy clip(): limit array values with bounds
NumPy clip(): Limit Array Values with Bounds
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.clip.html
numpy.clip — NumPy v2.1 Manual
Minimum and maximum value. If None, clipping is not performed on the corresponding edge. If both a_min and a_max are None, the elements of the returned array stay the same.
GeeksforGeeks
geeksforgeeks.org › numpy-clip-in-python
numpy.clip() in Python - GeeksforGeeks
November 29, 2018 - Syntax : numpy.clip(a, a_min, a_max, out=None) Parameters : a : Array containing elements to clip. a_min : Minimum value. --> If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None. a_max : Maximum value. --> If None, clipping is not performed on upper interval edge.
Top answer 1 of 3
22
actually there is a specific method for this, 'clip':
import numpy as np
my_array = np.array([[100, 200], [300, 400]],np.uint16)
my_array.clip(0,255) # clip(min, max)
output:
array([[100, 200],
[255, 255]], dtype=uint16)
2 of 3
7
import numpy as np
my_array = np.array([[100, 200], [300, 400]],np.uint16)
my_array[my_array > 255] = 255
the output will be
array([[100, 200],
[255, 255]], dtype=uint16)
Programiz
programiz.com › python-programming › numpy › methods › clip
NumPy clip() (With Examples)
The clip() function is used to limit the values in an array to a specified range. The clip() function is used to limit the values in an array to a specified range. Example import numpy as np array1 = np.array([1, 2, 3, 4, 5]) # clip values in array1 between 2 and 4 using clip() clipped_array ...
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.clip.html
numpy.clip — NumPy v2.0 Manual
Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1. Equivalent to but faster than np.minimum(a_max, np.maximum(a, a_min)). No check is performed to ensure a_min < a_max. ... Array containing elements to clip.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.clip.html
numpy.clip — NumPy v2.2 Manual
Minimum and maximum value. If None, clipping is not performed on the corresponding edge. If both a_min and a_max are None, the elements of the returned array stay the same.
Scaler
scaler.com › home › topics › what is numpy.clip()?
What is numpy.clip()? - Scaler Topics
May 4, 2023 - The clip() function is used to clip (limit) the values of an array between two numbers.
Codecademy
codecademy.com › docs › python:numpy › ndarray › .clip()
Python:NumPy | ndarray | .clip() | Codecademy
November 1, 2025 - Numpy’s .clip() method limits the values in an array to a specified range by replacing values below a minimum or above a maximum with those boundary values.
NumPy
numpy.org › devdocs › reference › generated › numpy.clip.html
numpy.clip — NumPy v2.6.dev0 Manual
Minimum and maximum value. If None, clipping is not performed on the corresponding edge. If both a_min and a_max are None, the elements of the returned array stay the same.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matrix.clip.html
numpy.matrix.clip — NumPy v2.1 Manual
Return an array whose values are limited to [min, max]. One of max or min must be given. Refer to numpy.clip for full documentation.
Stack Overflow
stackoverflow.com › questions › 16787485 › numpy-clip-cut-2d-masked-array
python - Numpy : clip/cut 2d masked array - Stack Overflow
From a masked 2d array like this: (x = --) x x x x x 5 6 x x x x x x x 9 x How can I get: (confining the edges as much as possible until reaching a number) 5 6 x x x 9 Thanks.
Top answer 1 of 2
1
You can simply use np.clip as mozway suggested.
The clip function
First, we import the required libraries.
import numpy as np
import matplotlib.pyplot as plt
And then, simply define the clip function that takes the minimum and the maximum values from the user's input.
def clip(array):
min_val, max_val = [
float(input(i))
for i in ["Minimum value: ", "Maximum value: "]
]
return np.clip(array, min_val, max_val)
Output
We will test our clip function on a sample array.
>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> clip(a)
Minimum value: 3
Maximum value: 8
array([3, 3, 3, 4, 5, 6, 7, 8, 8, 8])
Plotting
We will define an arbitrary array with np.random.randint.
a = np.random.randint(0, 100, 100)
x_values = np.arange(len(a))
Finally, we clip and plot the two arrays as follows.
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(9, 3),
tight_layout=True, dpi=144)
ax1.plot(x, a)
ax1.set_title("Unclipped Array")
ax2.plot(x, clip(a))
ax2.set_title("Clipped Array")
plt.show()
Minimum value: 25
Maximum value: 75

The above plot is our final result.
2 of 2
0
Based on what your "clipping" should do, here's some idea with "native python" i.e no imports (can be done otherwise using e.g numpy or pandas.Series):
#Remove all elements outside [mi,ma]
a = [1,2,3,4,5,6,7,8,9,10]
mi = 3 #min
ma = 7 #max
list(filter(lambda x: mi<x<ma,a)) # [4,5,6]
#Set elements greater than 7 to 7 and all elements less than 3 to three
def clip_to_min_max(x,mi,max):
if x<mi: #Number is less than "mi" set it to "mi"
return mi
if x>ma: #Number is greater han "max", set it to "ma"
return mx
return x #It is between "mi" and "ma" - do nothing
[clip_to_min_max(x,3,7) for x in a] #[3,3,3,4,5,6,7,7,7,7]
NumPy
numpy.org › doc › stable › reference › generated › numpy.matrix.clip.html
numpy.matrix.clip — NumPy v2.5 Manual
Return an array whose values are limited to [min, max]. One of max or min must be given. Refer to numpy.clip for full documentation.