|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +cache_root = Path(os.environ.get("FIGURE_PLOT_CACHE_DIR", Path(__file__).resolve().parent / ".cache")) |
| 8 | +os.environ.setdefault("MPLBACKEND", "Agg") |
| 9 | +os.environ.setdefault("MPLCONFIGDIR", str(cache_root / "mplconfig")) |
| 10 | +os.environ.setdefault("XDG_CACHE_HOME", str(cache_root / "xdg-cache")) |
| 11 | +Path(os.environ["MPLCONFIGDIR"]).mkdir(parents=True, exist_ok=True) |
| 12 | +Path(os.environ["XDG_CACHE_HOME"]).mkdir(parents=True, exist_ok=True) |
| 13 | + |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | + |
| 19 | +def main() -> int: |
| 20 | + repo_root = Path(__file__).resolve().parent.parent |
| 21 | + example_dir = repo_root / "examples" |
| 22 | + output_dir = example_dir / "output" |
| 23 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | + csv_path = example_dir / "comparison_results.csv" |
| 26 | + df = pd.read_csv(csv_path) |
| 27 | + |
| 28 | + plt.rcParams.update( |
| 29 | + { |
| 30 | + "font.family": "sans-serif", |
| 31 | + "font.sans-serif": ["Arial", "DejaVu Sans", "Helvetica"], |
| 32 | + "font.size": 9, |
| 33 | + "axes.labelsize": 9, |
| 34 | + "xtick.labelsize": 8, |
| 35 | + "ytick.labelsize": 8, |
| 36 | + "legend.fontsize": 8, |
| 37 | + "lines.linewidth": 1.8, |
| 38 | + "axes.linewidth": 0.8, |
| 39 | + "axes.facecolor": "white", |
| 40 | + "figure.facecolor": "white", |
| 41 | + "axes.grid": True, |
| 42 | + "axes.grid.axis": "y", |
| 43 | + "grid.color": "#e5e7eb", |
| 44 | + "grid.linewidth": 0.6, |
| 45 | + "axes.spines.top": False, |
| 46 | + "axes.spines.right": False, |
| 47 | + "pdf.fonttype": 42, |
| 48 | + "ps.fonttype": 42, |
| 49 | + "savefig.dpi": 300, |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + methods = ["Ours", "BaselineA", "BaselineB"] |
| 54 | + datasets = df["Dataset"].tolist() |
| 55 | + values = df[methods].to_numpy() |
| 56 | + colors = ["#4C6A92", "#7A8F63", "#B5875A"] |
| 57 | + |
| 58 | + fig, ax = plt.subplots(figsize=(3.5, 2.5)) |
| 59 | + x = np.arange(len(datasets)) |
| 60 | + width = 0.22 |
| 61 | + offsets = np.linspace(-(len(methods) - 1) / 2, (len(methods) - 1) / 2, len(methods)) * width |
| 62 | + |
| 63 | + for i, (name, off) in enumerate(zip(methods, offsets)): |
| 64 | + ax.bar(x + off, values[:, i], width=width * 0.92, label=name, color=colors[i], zorder=3) |
| 65 | + |
| 66 | + ax.set_xticks(x) |
| 67 | + ax.set_xticklabels(datasets) |
| 68 | + ax.set_ylabel("Accuracy (%)") |
| 69 | + ax.set_ylim(bottom=70) |
| 70 | + ax.legend(loc="upper right", framealpha=0.9) |
| 71 | + fig.tight_layout(pad=0.4) |
| 72 | + |
| 73 | + png_path = output_dir / "comparison_bar_example.png" |
| 74 | + pdf_path = output_dir / "comparison_bar_example.pdf" |
| 75 | + fig.savefig(pdf_path, bbox_inches="tight") |
| 76 | + fig.savefig(png_path, bbox_inches="tight") |
| 77 | + |
| 78 | + print(f"csv={csv_path}") |
| 79 | + print(f"png={png_path}") |
| 80 | + print(f"pdf={pdf_path}") |
| 81 | + return 0 |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + raise SystemExit(main()) |
0 commit comments