Skip to content

Commit bc4a548

Browse files
feat(matplotlib): implement bar-3d (#2879)
## Implementation: `bar-3d` - matplotlib Implements the **matplotlib** version of `bar-3d`. **File:** `plots/bar-3d/implementations/matplotlib.py` **Parent Issue:** #2857 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20608477493)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 03e9aa7 commit bc4a548

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
""" pyplots.ai
2+
bar-3d: 3D Bar Chart
3+
Library: matplotlib 3.10.8 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-30
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
10+
11+
# Data - Sales by Product and Quarter
12+
np.random.seed(42)
13+
products = ["Product A", "Product B", "Product C", "Product D", "Product E"]
14+
quarters = ["Q1", "Q2", "Q3", "Q4"]
15+
16+
n_products = len(products)
17+
n_quarters = len(quarters)
18+
19+
# Generate realistic sales data (in thousands)
20+
sales = np.array(
21+
[
22+
[120, 135, 142, 158], # Product A - steady growth
23+
[85, 92, 78, 95], # Product B - fluctuating
24+
[200, 185, 210, 225], # Product C - highest performer
25+
[65, 70, 82, 88], # Product D - moderate growth
26+
[150, 145, 160, 142], # Product E - variable
27+
]
28+
)
29+
30+
# Create figure with 3D projection
31+
fig = plt.figure(figsize=(16, 9))
32+
ax = fig.add_subplot(111, projection="3d")
33+
34+
# Set up grid positions
35+
xpos = np.arange(n_products)
36+
ypos = np.arange(n_quarters)
37+
xpos_mesh, ypos_mesh = np.meshgrid(xpos, ypos, indexing="ij")
38+
39+
xpos_flat = xpos_mesh.flatten()
40+
ypos_flat = ypos_mesh.flatten()
41+
zpos_flat = np.zeros_like(xpos_flat)
42+
43+
# Bar dimensions
44+
dx = 0.6
45+
dy = 0.6
46+
dz = sales.flatten()
47+
48+
# Create color gradient based on height for better depth perception
49+
colors = plt.cm.viridis((dz - dz.min()) / (dz.max() - dz.min()))
50+
51+
# Plot 3D bars with transparency
52+
ax.bar3d(xpos_flat, ypos_flat, zpos_flat, dx, dy, dz, color=colors, alpha=0.85, edgecolor="#333333", linewidth=0.5)
53+
54+
# Styling
55+
ax.set_xlabel("Product", fontsize=20, labelpad=15)
56+
ax.set_ylabel("Quarter", fontsize=20, labelpad=15)
57+
ax.set_zlabel("Sales (thousands $)", fontsize=20, labelpad=15)
58+
ax.set_title("bar-3d · matplotlib · pyplots.ai", fontsize=24, pad=20)
59+
60+
# Set tick labels
61+
ax.set_xticks(xpos + dx / 2)
62+
ax.set_xticklabels(products, fontsize=14)
63+
ax.set_yticks(ypos + dy / 2)
64+
ax.set_yticklabels(quarters, fontsize=14)
65+
ax.tick_params(axis="z", labelsize=14)
66+
67+
# Set viewing angle for best visibility
68+
ax.view_init(elev=25, azim=45)
69+
70+
# Add colorbar to reinforce z-values
71+
sm = plt.cm.ScalarMappable(cmap="viridis", norm=plt.Normalize(vmin=dz.min(), vmax=dz.max()))
72+
sm.set_array([])
73+
cbar = fig.colorbar(sm, ax=ax, shrink=0.6, aspect=15, pad=0.1)
74+
cbar.set_label("Sales (thousands $)", fontsize=16)
75+
cbar.ax.tick_params(labelsize=14)
76+
77+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: matplotlib
2+
specification_id: bar-3d
3+
created: '2025-12-30T23:54:48Z'
4+
updated: '2025-12-31T00:00:10Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20608477493
7+
issue: 2857
8+
python_version: 3.13.11
9+
library_version: 3.10.8
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-3d/matplotlib/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-3d/matplotlib/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent use of viridis colormap with colorbar for depth perception as spec recommends
17+
- Semi-transparent bars (alpha=0.85) help reveal bars behind taller ones per spec
18+
guidance
19+
- Well-chosen viewing angle (elev=25, azim=45) provides good visibility of all bars
20+
- Professional title format and axis labels with units
21+
- Realistic sales data scenario with meaningful variation between products and quarters
22+
- Clean, well-structured code following KISS principle
23+
weaknesses:
24+
- Grid styling could use alpha parameter for subtler appearance
25+
- Could benefit from slightly more contrast in bar heights (all values in 65-225
26+
range, some near-zero values would show more variation)

0 commit comments

Comments
 (0)