Python ML Plot Your Data
A plot turns a column of numbers into a picture. The picture shows the spread and the groups faster than a table can.
A scatter plot shows the link
Longer sepals go with longer petals. The two numbers rise together, and the link measures 0.87.
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
x = df["sepal length (cm)"]
y = df["petal length (cm)"]
print(round(float(np.corrcoef(x, y)[0, 1]), 2))
plt.scatter(x, y)
plt.savefig("sepal_petal_scatter.png")
print("Saved: sepal_petal_scatter.png")A histogram counts the ranges
Five bars cover sepal lengths from 4.3 to 7.9. The middle bar holds 42 flowers.
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
counts, edges = np.histogram(df["sepal length (cm)"], bins=5)
print(counts.tolist())
print([round(float(e), 1) for e in edges])
plt.hist(df["sepal length (cm)"], bins=5)
plt.savefig("sepal_histogram.png")
print("Saved: sepal_histogram.png")A bar chart counts the groups
Each species gets one bar. All three bars reach 50.
import pandas as pd
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print(df["species"].value_counts().to_dict())
plt.bar(df["species"].value_counts().index.tolist(), df["species"].value_counts().tolist())
plt.savefig("species_bar.png")
print("Saved: species_bar.png")A heatmap shows every pair at once
Petal length and petal width move together at 0.96. Sepal width moves against petal length at -0.43.
import pandas as pd
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import seaborn as sns
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
corr = df[list(iris.feature_names)].corr().round(2)
print(corr.values.tolist())
sns.heatmap(corr)
plt.savefig("iris_heatmap.png")
print("Saved: iris_heatmap.png")Example
import pandas as pd
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print("Species counts:", df["species"].value_counts().to_dict())
print("Mean petal length by species:", df.groupby("species")["petal length (cm)"].mean().round(2).to_dict())
plt.scatter(df["sepal length (cm)"], df["petal length (cm)"])
plt.savefig("iris_scatter.png")
print("Saved: iris_scatter.png")Key points
- Matplotlib: the library that draws plots in Python
- Seaborn: a library that draws neat statistical plots on top of Matplotlib
- Scatter plot: one dot per row, with two measurements on the two axes
- Histogram: bars that count how many values fall in each range
- Bar chart: one bar per group, with the height showing the count
- Heatmap: a coloured grid that shows how strongly numbers move together
- Correlation: a number from -1 to 1 that says how strongly two numbers move together
In this example, the three species counts are 50 each. The mean petal lengths are 1.46, 4.26, and 5.55, so the species differ most in petal length. The scatter plot is saved as iris_scatter.png. A plot turns the same numbers into a picture, and the picture shows the groups at a glance.
Note
Save every figure with savefig(). A plot on screen alone leaves nothing to check later.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does a histogram show?