|
| 1 | +""" |
| 2 | +Example: Flame Chart with Matplotlib Backend |
| 3 | +
|
| 4 | +This example demonstrates how to create a flame chart visualization |
| 5 | +using the matplotlib backend. Flame charts are useful for visualizing |
| 6 | +hierarchical profiling data, showing function call stacks and their |
| 7 | +execution times. |
| 8 | +""" |
| 9 | + |
| 10 | +from maxplotlib import Canvas |
| 11 | + |
| 12 | +# Example profiling data: function call hierarchy |
| 13 | +# Each function has: label, parent index (None for root), and duration |
| 14 | +labels = [ |
| 15 | + "main()", # 0 - root |
| 16 | + "process_data()", # 1 - child of main |
| 17 | + "load_file()", # 2 - child of process_data |
| 18 | + "parse_json()", # 3 - child of process_data |
| 19 | + "validate()", # 4 - child of process_data |
| 20 | + "compute()", # 5 - child of main |
| 21 | + "algorithm_a()", # 6 - child of compute |
| 22 | + "algorithm_b()", # 7 - child of compute |
| 23 | + "save_results()", # 8 - child of main |
| 24 | +] |
| 25 | + |
| 26 | +parents = [ |
| 27 | + None, # main() is root |
| 28 | + 0, # process_data() called by main() |
| 29 | + 1, # load_file() called by process_data() |
| 30 | + 1, # parse_json() called by process_data() |
| 31 | + 1, # validate() called by process_data() |
| 32 | + 0, # compute() called by main() |
| 33 | + 5, # algorithm_a() called by compute() |
| 34 | + 5, # algorithm_b() called by compute() |
| 35 | + 0, # save_results() called by main() |
| 36 | +] |
| 37 | + |
| 38 | +# Duration of each function call (in milliseconds) |
| 39 | +values = [100, 40, 10, 15, 15, 50, 25, 25, 10] |
| 40 | + |
| 41 | +# Start times for each function (when they begin execution) |
| 42 | +start_times = [0, 0, 0, 10, 25, 40, 40, 65, 90] |
| 43 | + |
| 44 | +# Create canvas and add flame chart |
| 45 | +canvas = Canvas(nrows=1, ncols=1, figsize=(12, 6)) |
| 46 | +canvas.flame_chart( |
| 47 | + labels=labels, |
| 48 | + parents=parents, |
| 49 | + values=values, |
| 50 | + start_times=start_times, |
| 51 | + colormap="viridis", |
| 52 | + edgecolor="black", |
| 53 | +) |
| 54 | + |
| 55 | +# Configure the plot |
| 56 | +canvas.set_xlabel("Time (ms)") |
| 57 | +canvas.set_ylabel("Stack Depth") |
| 58 | +canvas.set_title("Flame Chart: Function Call Hierarchy") |
| 59 | + |
| 60 | +# Save the figure |
| 61 | +canvas.savefig("flame_matplotlib.png", backend="matplotlib") |
| 62 | +print("Flame chart saved as flame_matplotlib.png") |
0 commit comments