Challenge - 5 Problems
Scatter Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a basic scatter plot code
What will be the output of this code snippet that creates a scatter plot using pandas and matplotlib?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'x': [1, 2, 3, 4], 'y': [10, 20, 25, 30]} df = pd.DataFrame(data) df.plot.scatter(x='x', y='y') plt.show()
Attempts:
2 left
💡 Hint
Remember that df.plot.scatter creates a scatter plot, not a line or bar chart.
✗ Incorrect
The code uses pandas DataFrame's plot.scatter method which creates a scatter plot with the given x and y columns. plt.show() displays the plot.
❓ data_output
intermediateNumber of points in scatter plot
Given this DataFrame and scatter plot code, how many points will appear in the scatter plot?
Pandas
import pandas as pd data = {'height': [150, 160, 170, 180, 190], 'weight': [50, 60, 65, 80, 90]} df = pd.DataFrame(data) df.plot.scatter(x='height', y='weight')
Attempts:
2 left
💡 Hint
Each row in the DataFrame corresponds to one point in the scatter plot.
✗ Incorrect
The DataFrame has 5 rows, so the scatter plot will have 5 points.
🔧 Debug
advancedIdentify the error in scatter plot code
What error will this code raise when trying to create a scatter plot?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'a': [1, 2, 3], 'b': [4, 5, 6]} df = pd.DataFrame(data) df.plot.scatter(x='x', y='b') plt.show()
Attempts:
2 left
💡 Hint
Check if the DataFrame has a column named 'x'.
✗ Incorrect
The DataFrame does not have a column named 'x', so trying to plot with x='x' raises a KeyError.
❓ visualization
advancedEffect of color parameter in scatter plot
What will be the color of points in the scatter plot created by this code?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'x': [1, 2, 3], 'y': [4, 5, 6]} df = pd.DataFrame(data) df.plot.scatter(x='x', y='y', c='red') plt.show()
Attempts:
2 left
💡 Hint
The color parameter sets the color of all points.
✗ Incorrect
The color='red' argument sets all scatter plot points to red color.
🚀 Application
expertInterpreting scatter plot with categorical color grouping
Given this DataFrame and scatter plot code, what does the color grouping represent in the plot?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'age': [25, 30, 22, 40, 35], 'income': [50000, 60000, 45000, 80000, 70000], 'gender': ['M', 'F', 'F', 'M', 'F']} df = pd.DataFrame(data) df.plot.scatter(x='age', y='income', c=df['gender'].map({'M': 'blue', 'F': 'pink'})) plt.show()
Attempts:
2 left
💡 Hint
Look at how the 'gender' column is mapped to colors.
✗ Incorrect
The code maps 'M' to blue and 'F' to pink, so blue points are males and pink points are females.
