Lesson 35 of 60 · python
Advanced Plotting: Histograms, Boxplots, and Violin Plots
Duration: 20 minutes
Advanced Plot Types
These plots are essential for summarizing distributions.
Histogram (frequency distribution)
data = np.random.randn(1000)
plt.hist(data, bins=30, edgecolor='black', alpha=0.7)
plt.title('Histogram of Normal Distribution')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
Boxplot (quartiles and outliers)
# Compare two groups
group1 = np.random.normal(0, 1, 200)
group2 = np.random.normal(1, 1.5, 200)
plt.boxplot([group1, group2], labels=['G1', 'G2'], notch=True)
plt.title('Boxplot Comparison')
plt.ylabel('Value')
plt.show()
Violin plot (kernel density + boxplot)
import seaborn as sns
sns.violinplot(data=[group1, group2], palette='muted')
plt.title('Violin Plot')
plt.show()
KDE (kernel density estimate)
sns.kdeplot(data, shade=True, color='green')
plt.title('KDE Plot')
plt.show()
When to use which?
- Histogram: quick view of frequency; easy to interpret.
- Boxplot: median, quartiles, outliers.
- Violin: detailed distribution shape.