Skip to content

Commit 9e7e533

Browse files
committed
Improve barplots (#48)
* Improved barplots * Formatting Added Gantt chart Support tikz
1 parent 13f43cc commit 9e7e533

5 files changed

Lines changed: 320 additions & 0 deletions

File tree

examples/gantt_matplotlib.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import numpy as np
2+
3+
from maxplotlib import Canvas
4+
5+
6+
def main() -> None:
7+
# Define project tasks
8+
tasks = [
9+
"Planning",
10+
"Design",
11+
"Development",
12+
"Testing",
13+
"Deployment",
14+
"Documentation",
15+
]
16+
17+
# Start times (in days from project start)
18+
start_times = np.array([0, 5, 10, 25, 35, 30])
19+
20+
# Duration of each task (in days)
21+
durations = np.array([5, 5, 15, 10, 5, 10])
22+
23+
# Create canvas
24+
canvas = Canvas(width="14cm", ratio=0.6, dpi=150)
25+
26+
# Add gantt chart
27+
canvas.gantt(
28+
tasks=tasks,
29+
start_times=start_times,
30+
durations=durations,
31+
color="steelblue",
32+
alpha=0.7,
33+
edgecolor="black",
34+
label="Project Tasks"
35+
)
36+
37+
# Configure plot
38+
canvas.set_title("Project Timeline - Gantt Chart")
39+
canvas.set_xlabel("Days from Project Start")
40+
canvas.set_ylabel("Tasks")
41+
canvas.set_grid(True)
42+
canvas.set_xlim(0, 45)
43+
44+
# Save figure
45+
canvas.savefig("gantt_matplotlib.png", backend="matplotlib")
46+
print("Gantt chart saved as gantt_matplotlib.png")
47+
48+
49+
if __name__ == "__main__":
50+
main()

examples/gantt_plotext.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
3+
from maxplotlib import Canvas
4+
5+
6+
def main() -> None:
7+
# Define project tasks
8+
tasks = [
9+
"Planning",
10+
"Design",
11+
"Development",
12+
"Testing",
13+
"Deployment",
14+
"Documentation",
15+
]
16+
17+
# Start times (in days from project start)
18+
start_times = np.array([0, 5, 10, 25, 35, 30])
19+
20+
# Duration of each task (in days)
21+
durations = np.array([5, 5, 15, 10, 5, 10])
22+
23+
# Create canvas
24+
canvas = Canvas(width="14cm", ratio=0.6)
25+
26+
# Add gantt chart
27+
canvas.gantt(
28+
tasks=tasks,
29+
start_times=start_times,
30+
durations=durations,
31+
color="cyan",
32+
label="Project Tasks"
33+
)
34+
35+
# Configure plot
36+
canvas.set_title("Project Timeline - Gantt Chart (Plotext)")
37+
canvas.set_xlabel("Days from Project Start")
38+
canvas.set_ylabel("Tasks")
39+
canvas.set_grid(True)
40+
canvas.set_xlim(0, 45)
41+
42+
# Save figure (plotext renders to terminal)
43+
canvas.savefig("gantt_plotext.txt", backend="plotext")
44+
print("Gantt chart saved as gantt_plotext.txt")
45+
46+
47+
if __name__ == "__main__":
48+
main()

examples/gantt_plotly.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import numpy as np
2+
3+
from maxplotlib import Canvas
4+
5+
6+
def main() -> None:
7+
# Define project tasks
8+
tasks = [
9+
"Planning",
10+
"Design",
11+
"Development",
12+
"Testing",
13+
"Deployment",
14+
"Documentation",
15+
]
16+
17+
# Start times (in days from project start)
18+
start_times = np.array([0, 5, 10, 25, 35, 30])
19+
20+
# Duration of each task (in days)
21+
durations = np.array([5, 5, 15, 10, 5, 10])
22+
23+
# Create canvas
24+
canvas = Canvas(width="14cm", ratio=0.6)
25+
26+
# Add gantt chart
27+
canvas.gantt(
28+
tasks=tasks,
29+
start_times=start_times,
30+
durations=durations,
31+
color="steelblue",
32+
alpha=0.7,
33+
label="Project Tasks"
34+
)
35+
36+
# Configure plot
37+
canvas.set_title("Project Timeline - Gantt Chart (Plotly)")
38+
canvas.set_xlabel("Days from Project Start")
39+
canvas.set_ylabel("Tasks")
40+
canvas.set_grid(True)
41+
canvas.set_xlim(0, 45)
42+
43+
# Save figure
44+
canvas.savefig("gantt_plotly.html", backend="plotly")
45+
print("Gantt chart saved as gantt_plotly.html")
46+
47+
48+
if __name__ == "__main__":
49+
main()

src/maxplotlib/canvas/canvas.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,30 @@ def bar(
430430
sp = self._get_or_create_subplot(row, col)
431431
sp.bar(x, height, layer=layer, **kwargs)
432432

433+
def gantt(
434+
self,
435+
tasks,
436+
start_times,
437+
durations,
438+
layer=0,
439+
row: int | None = None,
440+
col: int | None = None,
441+
**kwargs,
442+
):
443+
"""
444+
Add a Gantt chart to the canvas (matplotlib-style convenience method).
445+
446+
Parameters:
447+
tasks (array-like): Task names or labels (y-axis).
448+
start_times (array-like): Start times for each task (x-axis).
449+
durations (array-like): Duration of each task.
450+
layer (int): Layer index (default 0).
451+
row, col (int): Subplot position (default top-left).
452+
**kwargs: Forwarded to the backend (e.g., color, alpha, edgecolor, label).
453+
"""
454+
sp = self._get_or_create_subplot(row, col)
455+
sp.gantt(tasks, start_times, durations, layer=layer, **kwargs)
456+
433457
def set_xlabel(self, label: str, row: int | None = None, col: int | None = None):
434458
"""Set the x-axis label for a subplot (default top-left)."""
435459
self._get_or_create_subplot(row, col).set_xlabel(label)
@@ -840,6 +864,15 @@ def savefig(
840864
self._save_plotly(fig, full_filepath)
841865
if verbose:
842866
print(f"Saved {full_filepath}")
867+
elif backend == "tikzfigure":
868+
if layers is not None:
869+
raise NotImplementedError(
870+
"Layer-by-layer rendering is not supported for tikzfigure backend"
871+
)
872+
fig = self.plot(backend="tikzfigure", savefig=False)
873+
fig.savefig(filename)
874+
if verbose:
875+
print(f"Saved {filename}")
843876

844877
def plot(
845878
self,

0 commit comments

Comments
 (0)