-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatplotlib.py
More file actions
52 lines (40 loc) · 1.33 KB
/
matplotlib.py
File metadata and controls
52 lines (40 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
bar-basic: Basic Bar Chart
Library: matplotlib
"""
import matplotlib.pyplot as plt
# Data - Product sales by category
categories = ["Electronics", "Clothing", "Home & Garden", "Sports", "Books", "Toys"]
values = [45200, 32800, 28500, 21300, 18900, 15600]
# Create plot
fig, ax = plt.subplots(figsize=(16, 9))
# Bar chart with Python Blue color
bars = ax.bar(categories, values, color="#306998", width=0.6, edgecolor="white", linewidth=1)
# Add value labels on top of bars
for bar, value in zip(bars, values, strict=True):
height = bar.get_height()
ax.annotate(
f"${value:,}",
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 8),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=14,
)
# Labels and styling
ax.set_xlabel("Product Category", fontsize=20)
ax.set_ylabel("Sales ($)", fontsize=20)
ax.set_title("Product Sales by Category", fontsize=20)
# Style tick labels
ax.tick_params(axis="both", labelsize=16)
# Subtle grid on y-axis only
ax.yaxis.grid(True, alpha=0.3, linestyle="-")
ax.set_axisbelow(True)
# Remove top and right spines for cleaner look
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
# Set y-axis to start at 0
ax.set_ylim(bottom=0)
plt.tight_layout()
plt.savefig("plot.png", dpi=300, bbox_inches="tight")