\Python: Scatter Plot (y = x²) — Matplotlib & Seaborn\
\What you’ll learn:\ how to plot a simple scatter chart, label axes, and set a title.
\Tip:\ Run either block as-is; they’re independent.
\A) Using Matplotlib\
\
Code: Select all
# Scatter plot: y = x^2 (Matplotlib)
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9]
y = [1,4,9,16,25,36,49,64,81]
plt.scatter(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
Code: Select all
# Scatter plot: y = x^2 (Seaborn)
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8,9])
y = np.array([1,4,9,16,25,36,49,64,81])
df = pd.DataFrame({'X': x, 'Y': y})
sns.scatterplot(x='X', y='Y', data=df)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.show()
\
- \
- Do points rise smoothly? (Yes—positive correlation.)
\ - Does changing X change Y predictably? (Yes—quadratic growth.)
\[\*]Forward-looking tweak: try non-linear fits or log scales when Y grows fast.
\