Line, Bar and Scatter Plots
Choose the chart type that fits your data: trends, comparisons or relationships.
Different questions need different charts:
| Chart | Best for |
|---|---|
plt.plot | Trends over an ordered axis (time series). |
plt.bar | Comparing categories. |
plt.scatter | Relationship between two numeric variables. |
A scatter plot is your first tool for spotting correlation before modelling.
Example
import matplotlib.pyplot as plt
cities = ['Paris', 'Rome', 'Oslo']
sales = [150, 350, 90]
plt.bar(cities, sales)
plt.title('Sales by city')
plt.ylabel('Total sales')
plt.show()
# Scatter: height vs weight
plt.scatter([160, 170, 180], [60, 72, 85])
plt.show()When to use it
- A data scientist uses a scatter plot of two principal components to visually confirm whether k-means clusters are well separated.
- An analyst uses a bar chart to compare mean sales per product category, making it easy to spot the top-performing category at a glance.
- A researcher plots a line chart of model accuracy vs training-set size to show how performance improves with more data.
More examples
Line chart: accuracy over epochs
Plots train and validation accuracy on the same axes to reveal the divergence that signals overfitting.
import matplotlib.pyplot as plt
epochs = list(range(1, 11))
train_acc = [0.60, 0.68, 0.73, 0.77, 0.80, 0.83, 0.85, 0.86, 0.87, 0.88]
val_acc = [0.58, 0.65, 0.70, 0.74, 0.76, 0.77, 0.76, 0.75, 0.74, 0.73]
plt.plot(epochs, train_acc, label='train')
plt.plot(epochs, val_acc, label='validation', linestyle='--')
plt.xlabel('Epoch'); plt.ylabel('Accuracy')
plt.legend(); plt.title('Learning Curves')
plt.show()Bar chart: per-category comparison
Draws a vertical bar chart comparing revenue across four product categories, making differences immediately visible.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'category': ['Electronics', 'Clothing', 'Books', 'Food'],
'revenue': [42000, 28000, 12000, 35000]})
plt.bar(df['category'], df['revenue'], color='steelblue')
plt.ylabel('Revenue ($)')
plt.title('Revenue by Category')
plt.show()Scatter plot: two features coloured by class
Scatter-plots two Iris features with points coloured by class label, a standard way to visually assess feature separability before classification.
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis', alpha=0.7)
plt.xlabel('Sepal length'); plt.ylabel('Sepal width')
plt.title('Iris classes by sepal dimensions')
plt.colorbar(label='class')
plt.show()
Discussion