Day 5 Data Visualisation With Maplotlib and Seaborn
Day 5 Data Visualisation With Maplotlib and Seaborn
Plotting with Matplotlib: Line Plots, Bar Plots, Histograms: Matplotlib is a widely used
Python library for creating static, interactive, and animated visualizations. It supports various
plot types, including line plots, bar plots, histograms, scatter plots, etc.
# Example of creating line plot, bar plot, and histogram using Matplotlib
import matplotlib.pyplot as plt
# Line plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot')
plt.show()
# Bar plot
plt.bar(['A', 'B', 'C', 'D'], [10, 20, 15, 25])
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()
# Histogram
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5)
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
Introduction to Seaborn for Statistical Plots: Seaborn is built on top of Matplotlib and
provides a high-level interface for drawing attractive and informative statistical graphics. It
simplifies the process of creating complex visualizations.
# Box plot
sns.boxplot(x="day", y="total_bill", data=tips)
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.title('Box Plot')
plt.show()
# Violin plot
sns.violinplot(x="day", y="total_bill", data=tips)
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.title('Violin Plot')
plt.show()
Combining Multiple Plots: Matplotlib and Seaborn allow combining multiple plots in a
single figure to create complex visualizations for better data exploration and analysis.
plt.subplot(1, 2, 2)
plt.bar(['A', 'B', 'C', 'D'], [10, 20, 15, 25])
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Subplot 2')
Matplotlib and Seaborn are powerful visualization libraries that play a crucial role in
exploratory data analysis and communicating insights from data.
Day 5: Data Visualization with Matplotlib
and Seaborn
Plotting with Matplotlib: Line Plots, Bar Plots,
Histograms
Matplotlib:
Importing Matplotlib:
python
Copy code
import matplotlib.pyplot as plt
Line Plots:
Example:
python
Copy code
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Line Plot')
plt.show()
Bar Plots:
Example:
python
Copy code
categories = ['A', 'B', 'C']
values = [10, 20, 30]
plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()
Histograms:
Example:
python
Copy code
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5)
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
Importing Seaborn:
python
Copy code
import seaborn as sns
Scatter Plot:
Example:
python
Copy code
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.xlabel('Total Bill')
plt.ylabel('Tip')
plt.title('Scatter Plot')
plt.show()
Box Plot:
Example:
python
Copy code
sns.boxplot(x='day', y='total_bill', data=tips)
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.title('Box Plot')
plt.show()
Pair Plot:
Example:
python
Copy code
sns.pairplot(tips)
plt.show()
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title('Line Plot')
plt.subplot(1, 2, 2)
plt.bar(categories, values)
plt.title('Bar Plot')
plt.tight_layout()
plt.show()
# Sample data
tips = sns.load_dataset('tips')
# Line Plot
plt.subplot(1, 3, 1)
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title('Line Plot')
# Bar Plot
plt.subplot(1, 3, 2)
categories = ['A', 'B', 'C']
values = [10, 20, 30]
plt.bar(categories, values)
plt.title('Bar Plot')
plt.tight_layout()
plt.show()
This concludes the note for Day 5: Data Visualization with Matplotlib and Seaborn.