Mathew K Analytics

Lesson 13 · Data analytics zero to hero

Data Visualisation with Matplotlib | Data Analytics #13

Video thirteen of the 30-part series, and the start of the visualization block: line plots, bar charts, histograms, and scatter plots. We're back to the…

What you'll learn

Datasets used in this lesson

Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.

📓 Full notebook

Download .ipynb

Data Analytics Zero to Hero, Video 13: Data Visualization with Matplotlib#

  • Video thirteen of the 30-part series, and the start of the visualization block: line plots, bar charts, histograms, and scatter plots.
  • We're back to the real mtcars dataset, 1974 Motor Trend car road tests, this time to actually see the data.
  • Let's jump straight in.

Before You Start#

  • Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
  • Install Matplotlib if you haven't already: pip install matplotlib.
  • Place mtcars.csv in the same folder as this notebook.
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('mtcars.csv')
print(df.shape)
(32, 12)

Part 1: Line Plots and Bar Charts#

sorted_df = df.sort_values('mpg').reset_index(drop=True)
plt.plot(sorted_df.index, sorted_df['mpg'])
plt.title('Real MPG Values, Sorted')
plt.xlabel('Car Rank')
plt.ylabel('MPG')
plt.show()
No description has been provided for this image
top5 = df.nlargest(5, 'mpg')
plt.bar(top5['model'], top5['mpg'], color='steelblue')
plt.title('Top 5 Most Fuel-Efficient Real Cars')
plt.ylabel('MPG')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()
No description has been provided for this image

Part 2: Scatter Plots#

plt.scatter(df['wt'], df['mpg'])
plt.title('Real Weight vs. MPG')
plt.xlabel('Weight (1000 lbs)')
plt.ylabel('MPG')
plt.show()
No description has been provided for this image
colors = df['cyl'].map({4: 'green', 6: 'orange', 8: 'red'})
plt.scatter(df['wt'], df['mpg'], c=colors)
plt.title('Real Weight vs. MPG, Colored by Cylinder Count')
plt.xlabel('Weight (1000 lbs)')
plt.ylabel('MPG')
plt.show()
No description has been provided for this image

Part 3: Histograms#

plt.hist(df['mpg'], bins=8, color='mediumseagreen', edgecolor='black')
plt.title('Distribution of Real MPG Values')
plt.xlabel('MPG')
plt.ylabel('Number of Cars')
plt.show()
No description has been provided for this image

Part 4: Subplots#

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].scatter(df['wt'], df['mpg'])
axes[0].set_title('Weight vs. MPG')
axes[1].hist(df['hp'], bins=8, color='coral')
axes[1].set_title('Horsepower Distribution')
plt.tight_layout()
plt.show()
No description has been provided for this image

Part 5: Saving a Figure#

plt.scatter(df['wt'], df['mpg'], c=colors)
plt.title('Real Weight vs. MPG, Colored by Cylinder Count')
plt.xlabel('Weight (1000 lbs)')
plt.ylabel('MPG')
plt.savefig('weight_vs_mpg.png', dpi=150, bbox_inches='tight')
plt.show()
No description has been provided for this image

Wrap-Up: What You Learned#

  • Line plots and bar charts, with plot and bar.
  • Scatter plots, including coloring points by a third categorical column.
  • Histograms for visualizing a single column's distribution.
  • Subplots, for comparing multiple charts in one figure.
  • Saving a chart to a real image file with savefig.
  • All of it built on the real mtcars dataset. Video fourteen covers Seaborn, a higher-level charting library built on top of Matplotlib, for faster, more polished statistical charts. Subscribe so it lands automatically see you there.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.