Menu

Exploring Relationships and Distributions with Seaborn

Exploring Relationships and Distributions with Seaborn

Seaborn includes specialized charts for understanding numerical distributions and relationships between variables.

Visualizing Correlation with a Heatmap

A heatmap represents numerical values using color intensity. It is often used to explore correlations between numerical columns.

df = pd.DataFrame({

"Advertising": [1000, 1500, 2000, 2500, 3000],

"Website_Visits": [200, 250, 310, 370, 430],

"Sales": [10000, 12000, 15000, 18000, 22000]

})

correlation = df.corr(numeric_only=True)

sns.heatmap(

correlation,

annot=True,

cmap="coolwarm",

vmin=-1,

vmax=1

)

plt.title("Correlation Heatmap")

plt.show()

The annot=True parameter displays correlation values inside the cells. The scale from -1 to +1 makes the direction and strength of linear relationships easier to compare.

Remember that correlation does not prove causation, and a heatmap alone cannot establish why variables are related.

Comparing Distributions with KDE Plots

A Kernel Density Estimate (KDE) plot provides a smoothed representation of a numerical distribution.

scores = [55, 60, 62, 65, 67, 70, 72, 75, 78, 80, 85, 90]

sns.kdeplot(x=scores, fill=True)

plt.title("Distribution of Scores")

plt.xlabel("Score")

plt.show()

KDE plots can help compare distribution shapes, but their appearance depends on the smoothing bandwidth. They are not ideal for every dataset, particularly very small samples.

Using Pair Plots

A pair plot compares relationships between multiple numerical variables and shows individual distributions along the diagonal.

df = pd.DataFrame({

"Study_Hours": [1, 2, 3, 4, 5, 6],

"Attendance": [60, 65, 72, 80, 85, 92],

"Score": [45, 50, 60, 68, 78, 90]

})

sns.pairplot(df)

plt.show()

Pair plots are useful during EDA when you want to examine several relationships at once. For datasets with many columns or rows, consider selecting a smaller sample of relevant variables to keep the chart readable.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details