Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/5-subplot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,56 @@
"source": [
"# TASK: Create a 2x2 subplot layout.\n",
"# Plot a line chart in the first subplot, a bar chart in the second, a scatter plot in the third, and a pie chart in the fourth."
import matplotlib.pyplot as plt
import numpy as np

# Data for plots
x = np.arange(0, 11)
y = x ** 2

bar_categories = ['A', 'B', 'C', 'D']
bar_values = [5, 7, 3, 9]

scatter_x = [1, 2, 3, 4, 5]
scatter_y = [2, 4, 6, 8, 10]

pie_labels = ['Python', 'Java', 'C++', 'JavaScript']
pie_sizes = [40, 25, 20, 15]

# Create 2x2 subplots
fig, axs = plt.subplots(2, 2, figsize=(10, 8))

# Line chart
axs[0, 0].plot(x, y, marker='o')
axs[0, 0].set_title("Line Chart")
axs[0, 0].set_xlabel("X")
axs[0, 0].set_ylabel("Y = X^2")
axs[0, 0].grid(True)

# Bar chart
axs[0, 1].bar(bar_categories, bar_values, color=['red', 'blue', 'green', 'orange'])
axs[0, 1].set_title("Bar Chart")
axs[0, 1].set_xlabel("Category")
axs[0, 1].set_ylabel("Value")

# Scatter plot
axs[1, 0].scatter(scatter_x, scatter_y, color='purple')
axs[1, 0].set_title("Scatter Plot")
axs[1, 0].set_xlabel("X")
axs[1, 0].set_ylabel("Y")
for i in range(len(scatter_x)):
axs[1, 0].annotate(f"({scatter_x[i]},{scatter_y[i]})", (scatter_x[i], scatter_y[i]),
textcoords="offset points", xytext=(5,5), ha='center')
axs[1, 0].grid(True)

# Pie chart
axs[1, 1].pie(pie_sizes, labels=pie_labels, autopct='%1.1f%%', startangle=90)
axs[1, 1].set_title("Pie Chart")
axs[1, 1].axis('equal') # Keeps pie chart circular

# Layout adjustment
plt.tight_layout()
plt.show()
]
}
],
Expand Down