0
0
Pandasdata~10 mins

Bar plots in Pandas - 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 bar plot of the 'sales' column.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
df['sales'].[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Aplot.line
Bplot.bar
Cplot.scatter
Dplot.hist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plot.line instead of plot.bar
Forgetting to call the plot method
Using plot.hist which creates a histogram
2fill in blank
medium

Complete the code to set the title of the bar plot to 'Sales Data'.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
ax = df['sales'].plot.bar()
ax.[1]('Sales Data')
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Bxlabel
Cylabel
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using xlabel or ylabel instead of title
Trying to set title on plt instead of ax
Forgetting to call the method
3fill in blank
hard

Fix the error in the code to correctly plot a bar chart with product names as x-axis labels.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
df.plot.bar(x=[1], y='sales')
plt.show()
Drag options to blanks, or click blank then click option'
A'product'
B'sales'
C'index'
D'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sales' as x-axis labels
Using 'index' which is default row numbers
Using a non-existent column name
4fill in blank
hard

Fill both blanks to create a horizontal bar plot with 'product' on y-axis and 'sales' on x-axis.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
df.plot.barh(x=[1], y=[2])
plt.show()
Drag options to blanks, or click blank then click option'
A'product'
B'sales'
C'index'
D'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y parameters
Using 'index' instead of 'product'
Using non-existent columns
5fill in blank
hard

Fill all three blanks to create a bar plot with 'product' as x-axis, 'sales' as y-axis, and set the color to 'green'.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
ax = df.plot.bar(x=[1], y=[2], color=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A'product'
B'sales'
C'green'
D'blue'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names for x or y
Using color names not supported
Forgetting to put quotes around strings