0
0
Matplotlibdata~10 mins

Vertical bar chart with plt.bar 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 vertical bar chart using plt.bar.

Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values = [5, 7, 3]

plt.[1](categories, values)
plt.show()
Drag options to blanks, or click blank then click option'
Ascatter
Bplot
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot instead of plt.bar
Using plt.scatter which creates scatter plots
Using plt.hist which creates histograms
2fill in blank
medium

Complete the code to add labels to the x-axis of the bar chart.

Matplotlib
import matplotlib.pyplot as plt

categories = ['X', 'Y', 'Z']
values = [4, 6, 8]

plt.bar(categories, values)
plt.[1]('Categories')
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Bylabel
Clegend
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.ylabel which labels the y-axis
Using plt.title which sets the chart title
Using plt.legend which adds a legend
3fill in blank
hard

Fix the error in the code to correctly display the bar chart with custom colors.

Matplotlib
import matplotlib.pyplot as plt

categories = ['P', 'Q', 'R']
values = [10, 15, 7]
colors = ['red', 'green', 'blue']

plt.bar(categories, values, [1]=colors)
plt.show()
Drag options to blanks, or click blank then click option'
Acolor
Bcolors
Ccol
Dcolour
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'colors' instead of 'color' parameter
Using British spelling 'colour'
Using 'col' which is not a valid parameter
4fill in blank
hard

Fill both blanks to create a bar chart with a title and y-axis label.

Matplotlib
import matplotlib.pyplot as plt

categories = ['Jan', 'Feb', 'Mar']
values = [20, 25, 30]

plt.bar(categories, values)
plt.[1]('Monthly Sales')
plt.[2]('Sales')
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Bxlabel
Cylabel
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using xlabel instead of ylabel for y-axis label
Using legend instead of title or ylabel
5fill in blank
hard

Fill all three blanks to create a bar chart with custom colors, x-axis label, and title.

Matplotlib
import matplotlib.pyplot as plt

categories = ['Red', 'Green', 'Blue']
values = [7, 3, 5]
colors = ['#FF0000', '#00FF00', '#0000FF']

plt.bar(categories, values, [1]=colors)
plt.[2]('Colors')
plt.[3]('Color Counts')
plt.show()
Drag options to blanks, or click blank then click option'
Acolor
Bxlabel
Ctitle
Dylabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'colors' instead of 'color' parameter
Mixing up xlabel and ylabel
Forgetting to add the title