📊 Python Data Visualization with Matplotlib and Seaborn

This guide covers basic to advanced visualizations using Matplotlib and Seaborn. Each snippet is explained clearly with expected output behavior.


📦 Import Libraries

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline

🧮 Generate Sample Data

x = np.linspace(0, 5, 11)
y = x ** 2x  # view xy  # view y

📈 Basic Line Plot with Labels and Title

plt.plot(x, y, 'r')
plt.xlabel('x axis title')
plt.ylabel('y axis title')
plt.title('First visual')
plt.show()

🪞 Subplots: Side-by-Side Line Plots

plt.subplot(1, 2, 1)
plt.plot(x, y, 'r--')
plt.subplot(1, 2, 2)
plt.plot(x, y, 'g*-')
plt.show()

🏷️ Add Legends

plt.plot(x, x**2, label='x**2')
plt.plot(x, x**3, label='x**3')
plt.legend()
plt.show()

📚 Load Built-in Dataset

tips = sns.load_dataset('tips')
tips.head()