0
0
Matplotlibdata~10 mins

Image interpolation methods in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to display an image using nearest neighbor interpolation.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(5, 5)
plt.imshow(image, interpolation='[1]')
plt.show()
Drag options to blanks, or click blank then click option'
Abicubic
Bbilinear
Cnearest
Dspline16
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bilinear' or 'bicubic' which smooth the image instead of nearest neighbor.
Misspelling the interpolation method name.
2fill in blank
medium

Complete the code to display an image using bilinear interpolation.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(10, 10)
plt.imshow(image, interpolation='[1]')
plt.show()
Drag options to blanks, or click blank then click option'
Abilinear
Bnone
Channing
Dnearest
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'nearest' which does not smooth the image.
Using 'none' which disables interpolation.
3fill in blank
hard

Fix the error in the code to use bicubic interpolation correctly.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(8, 8)
plt.imshow(image, interpolation=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'bilinear'
Bbicubic
C'nearest'
D'bicubic'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the interpolation method name.
Using an unsupported interpolation method.
4fill in blank
hard

Fill both blanks to create a dictionary of images with their interpolation methods.

Matplotlib
images = {'img1': np.random.rand(4, 4), 'img2': np.random.rand(6, 6)}
methods = ['[1]', '[2]']
image_dict = {k: (v, m) for k, v, m in zip(images.keys(), images.values(), methods)}
Drag options to blanks, or click blank then click option'
Anearest
Bbilinear
Cbicubic
Dnone
Attempts:
3 left
💡 Hint
Common Mistakes
Using unsupported interpolation method names.
Mixing up the order of methods.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension filtering images with interpolation 'nearest' and size > 5.

Matplotlib
images = {'a': np.random.rand(3, 3), 'b': np.random.rand(7, 7), 'c': np.random.rand(10, 10)}
methods = {'a': '[1]', 'b': '[2]', 'c': '[3]'}
filtered = {k: v for k, v in images.items() if methods[k] == 'nearest' and v.shape[0] > 5}
Drag options to blanks, or click blank then click option'
Anearest
Bbilinear
Cbicubic
Dnone
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'nearest' to all images.
Not checking image size correctly.