UNIT-III
Data Visualization in Python
Perform following visualizations using matplotlib
Matplotlib is a powerful library in Python for creating visualizations. Here are
some common types of visualizations you can perform using Matplotlib:
1. Line Chart
Used for visualizing trends over time or continuous data.
import [Link] as plt
# Example data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
[Link](x, y, label='Trend', color='blue')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Line Chart')
[Link]()
[Link]()
2. Bar Chart
Ideal for comparing quantities across categories.
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 5, 8]
[Link](categories, values, color='purple')
[Link]('Categories')
[Link]('Values')
[Link]('Bar Chart')
[Link]()
3. Histogram
Used for showing the distribution of data.
data = [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5]
[Link](data, bins=5, color='green', edgecolor='black')
[Link]('Bins')
[Link]('Frequency')
[Link]('Histogram')
[Link]()
4. Scatter Plot
Visualizes relationships between two variables.
x = [5, 7, 8, 9, 10]
y = [10, 14, 15, 19, 21]
[Link](x, y, color='red')
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot')
[Link]()
5. Pie Chart
Displays proportions between categories.
sizes = [20, 30, 25, 25]
labels = ['Category A', 'Category B', 'Category C', 'Category D']
colors = ['gold', 'lightblue', 'lightgreen', 'pink']
[Link](sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
[Link]('Pie Chart')
[Link]()
[Link] Chart and subplots
Let's combine a line chart with subplots to showcase multiple visualizations
within the same figure! Here's an example using Matplotlib:
Code Example:
import [Link] as plt
# Data for Line Charts
x1 = [1, 2, 3, 4, 5]
y1 = [10, 20, 30, 40, 50]
x2 = [1, 2, 3, 4, 5]
y2 = [50, 40, 30, 20, 10]
# Create Subplots
fig, axes = [Link](2, 1, figsize=(8, 6)) # 2 rows, 1 column
# First Line Chart (subplot 1)
axes[0].plot(x1, y1, color='blue', label='Line 1')
axes[0].set_title('Line Chart 1')
axes[0].set_xlabel('X-axis')
axes[0].set_ylabel('Y-axis')
axes[0].legend()
# Second Line Chart (subplot 2)
axes[1].plot(x2, y2, color='red', linestyle='dashed', label='Line 2')
axes[1].set_title('Line Chart 2')
axes[1].set_xlabel('X-axis')
axes[1].set_ylabel('Y-axis')
axes[1].legend()
# Adjust layout
plt.tight_layout()
[Link]()
[Link] Plot:
A box plot (or box-and-whisker plot) is a graphical representation of a dataset
that shows its distribution through five key summary statistics: minimum, first
quartile (Q1), median (Q2), third quartile (Q3), and maximum. It's an excellent
way to visualize the spread, central tendency, and potential outliers in data.
Key Components of a Box Plot:
1. Box: The rectangle in the middle represents the interquartile range (IQR),
which is the distance between Q1 (25th percentile) and Q3 (75th
percentile).
2. Median Line: A line inside the box indicates the median (Q2), which is the
50th percentile.
3. Whiskers: Lines extending from the box to the minimum and maximum
values within 1.5 times the IQR.
4. Outliers: Data points that fall outside the whiskers are plotted as individual
dots or circles.
Example:
import [Link] as plt
# Sample data
data = [7, 8, 9, 10, 12, 14, 15, 18, 20, 22, 28]
# Create a box plot
[Link](data)
# Add labels
[Link]("Box Plot Example")
[Link]("Values")
# Show the plot
[Link]()