Context:FIT1043_MOC · render the chart types in Python · matplotlib + pandas .plot · often after a groupby · lab: 30_Projects/FIT1043_Labs/Week4-Wrangling-Viz-Solution.pdfProblem it solves: turn a DataFrame column (or two) into the chart appropriate for its data type.
Quick Revision
🎯 Trigger: need a chart ➔ import matplotlib.pyplot as plt; call plt. or df.plot. matched to the data type.
⚡ Key Constraint: match plot to type — bar/pie for categorical, hist/boxplot/scatter for numeric.
🔧 Minimal Working Example
import matplotlib.pyplot as pltdf = pd.DataFrame({'Class': ['First','Second','Third'], 'Passengers': [194,177,450]})plt.bar(df['Class'], df['Passengers']) # categorical → bar chartplt.show()
Expected output: a bar chart of passengers per class.
Bar (categorical) ➔ plt.bar(df['Class'], df['Passengers']).
Pie (categorical proportions) ➔ df.plot.pie(y='Average Age', labels=df['Class']).
Set the pie index ➔ build the DataFrame with index=['First','Second','Third'] so df.plot.pie(y='Average Age') labels sectors automatically.
Control histogram detail ➔ raise/lower bins= (few = coarse shape, many = ragged).
Encode a 3rd/4th variable on a scatter ➔ colour by a column and size the points: plt.scatter(df['HR'], df['SBP'], c=df['DBP'], s=40, cmap='hot') — c= colours, cmap= the palette, s= the marker size.
✍️ Practice
Practice 1: For numeric column price, draw a histogram with 20 bins and a boxplot to inspect its spread/outliers.
Reference solution
df['price'].hist(bins=20) # distribution shapedf.boxplot(column='price') # spread + outliers (IQR rule)
Key move: histogram + boxplot are the numeric-continuous pair; both need a numeric column.
⚠️ Common Mistakes
💡 Wrong chart for the type ➔ a bar chart is for categorical counts; use a histogram for continuous data (it bins the values).
💡 plt.show() renders it ➔ in scripts the figure won’t display until plt.show() (notebooks may auto-render).