0
0
Matplotlibdata~10 mins

Histogram vs bar chart distinction in Matplotlib - Interactive Practice

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

Complete the code to create a histogram of the data list using matplotlib.

Matplotlib
import matplotlib.pyplot as plt

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
plt.[1](data)
plt.show()
Drag options to blanks, or click blank then click option'
Ahist
Bbar
Cplot
Dscatter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bar' instead of 'hist' will create a bar chart, not a histogram.
Using 'plot' or 'scatter' won't group data into bins.
2fill in blank
medium

Complete the code to create a bar chart showing counts of categories using matplotlib.

Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
counts = [5, 7, 3]
plt.[1](categories, counts)
plt.show()
Drag options to blanks, or click blank then click option'
Ascatter
Bhist
Cbar
Dplot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hist' will try to bin numeric data, not suitable for categories.
Using 'scatter' or 'plot' won't create bars.
3fill in blank
hard

Fix the error in the code to correctly plot a histogram of the data.

Matplotlib
import matplotlib.pyplot as plt

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
plt.hist(data, bins=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A5
BNone
C[5]
D'5'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing bins as a string like '5' causes a TypeError.
Passing bins as a list like [5] is invalid.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that counts how many times each number appears in the list.

Matplotlib
data = [1, 2, 2, 3, 3, 3]
counts = {num: data.[1](num) for num in [2](data)}
Drag options to blanks, or click blank then click option'
Acount
Blen
Cset
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using len instead of count causes errors.
Using list instead of set causes duplicate keys.
5fill in blank
hard

Fill all three blanks to create a bar chart showing counts of unique items from data.

Matplotlib
import matplotlib.pyplot as plt

data = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']
counts = {item: data.count(item) for item in [1](data)}
plt.bar(counts.[2](), counts.[3]())
plt.show()
Drag options to blanks, or click blank then click option'
Aset
Bvalues
Ckeys
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of set causes duplicate bars.
Swapping keys() and values() causes wrong axes.