0
0
Matplotlibdata~10 mins

Color mapping with colorbar 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 create a scatter plot with colors mapped from the 'values' array.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
y = np.random.rand(10)
values = np.linspace(0, 1, 10)

plt.scatter(x, y, c=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Avalues
By
Cnp.arange(10)
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using x or y instead of the values array for colors.
Not passing any array to the c parameter.
2fill in blank
medium

Complete the code to add a colorbar to the scatter plot.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
y = np.random.rand(10)
values = np.linspace(0, 1, 10)

scatter = plt.scatter(x, y, c=values)
[1]
plt.show()
Drag options to blanks, or click blank then click option'
Aplt.colorbar(scatter)
Bplt.colorbar()
Cplt.colorbar(x)
Dplt.colorbar(y)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling plt.colorbar() without arguments.
Passing x or y arrays to plt.colorbar().
3fill in blank
hard

Fix the error in the code to correctly map colors and add a colorbar.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = np.random.rand(5)
values = np.array([10, 20, 30, 40, 50])

scatter = plt.scatter(x, y, c=[1])
plt.colorbar(scatter)
plt.show()
Drag options to blanks, or click blank then click option'
Ax
Bvalues
Cy
Dnp.arange(5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using y or x instead of the values array for colors.
Not passing any array to the c parameter.
4fill in blank
hard

Fill both blanks to create a scatter plot with a 'viridis' colormap and add a colorbar.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(8)
y = np.random.rand(8)
values = np.linspace(0, 1, 8)

scatter = plt.scatter(x, y, c=values, cmap=[1])
[2]
plt.show()
Drag options to blanks, or click blank then click option'
A'viridis'
Bplt.colorbar()
Cplt.colorbar(scatter)
D'plasma'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong colormap name.
Calling plt.colorbar() without the scatter object.
5fill in blank
hard

Fill all three blanks to create a scatter plot with a 'coolwarm' colormap, set the color limits from 0 to 1, and add a colorbar.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(6)
y = np.random.rand(6)
values = np.linspace(0, 1, 6)

scatter = plt.scatter(x, y, c=values, cmap=[1])
scatter.set_clim([2], [3])
plt.colorbar(scatter)
plt.show()
Drag options to blanks, or click blank then click option'
A'coolwarm'
B0
C1
D'viridis'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong colormap names.
Not setting color limits correctly.
Forgetting to add the colorbar.