Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ jobs:
run: pixi run test-lfortran
- name: Compare against matplotlib references
run: pixi run compare
- name: Compare PNG output against matplotlib
run: pixi run compare-png
- name: Compare PDF output against matplotlib
run: pixi run compare-pdf
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![CI](https://github.com/certik/fplot/actions/workflows/ci.yml/badge.svg)](https://github.com/certik/fplot/actions/workflows/ci.yml)

Pure Fortran plotting library that emits SVG text, with a pylab-style API.
Pure Fortran plotting library with a pylab-style API, writing SVG, PNG and PDF.
No external graphics library: the PNG rasterizer and its font are part of fplot.

```fortran
use fplot
Expand All @@ -12,7 +13,7 @@ call xlabel("x")
call ylabel("y")
call grid(.true.)
call legend()
call savefig("out.svg") ! file backend
call savefig("out.svg") ! or "out.png", "out.pdf"
call show() ! Jupyter (LFortran) or writes fplot_show.svg

! subplots
Expand Down Expand Up @@ -43,7 +44,8 @@ call suptitle("figure title")
markers `o x . s ^ v < > * + D`
- Optional `label=`, `lw=`, `color=`, `marker=`, `alpha=`
- Title, axis labels, grid, `xlim` / `ylim`, `clf` / `figure(figsize=, dpi=)`
- `savefig(file, transparent=, facecolor=)`
- `savefig(file, transparent=, facecolor=)`; the extension picks the format,
one of `.svg`, `.png` or `.pdf`, and `dpi=` sizes the PNG raster
- `axis("on"|"off"|"equal"|"scaled"|"tight"|"auto")` and `set_aspect`
- `set_xscale` / `set_yscale` with `"linear"`, `"log"` or `"symlog"`
- `tick_params(axis=, direction=, length=, labelsize=, rotation=)` and `spines`
Expand Down Expand Up @@ -80,19 +82,24 @@ pixi run demo-flang
pixi run build-lfortran
pixi run test-lfortran

# Compare against matplotlib reference SVGs
pixi run compare
# Compare against matplotlib references
pixi run compare # SVG, structurally
pixi run compare-png # PNG, pixel by pixel
pixi run compare-pdf # PDF, rasterized by pdftoppm
```

CI (Linux) runs `pixi run test-flang`, `pixi run test-lfortran` and
`pixi run compare`.
The SVG comparison is structural because a viewer, not fplot, decides what an
SVG looks like. PNG and PDF are compared as pixels, since there fplot decides.

CI (Linux) runs `pixi run test-flang` and `pixi run test-lfortran`, then all
three comparisons.

## Layout

```
src/ library modules
examples/ demo.f90
tests/ Fortran test plots, matplotlib refs, compare script
tests/ Fortran test plots, matplotlib refs, compare scripts
scripts/ build_flang.sh, build_lfortran.sh
```

Expand Down
357 changes: 357 additions & 0 deletions pixi.lock

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@ test-lfortran = "bash scripts/build_lfortran.sh && mkdir -p tests/out && ./build
demo-lfortran = "bash scripts/build_lfortran.sh && ./build/lfortran/demo"

mpl-refs = "python tests/gen_mpl_refs.py"
compare = "python tests/gen_mpl_refs.py && bash scripts/build_flang.sh && mkdir -p tests/out && ./build/flang/test_plots && python tests/compare_svgs.py"

# The three comparisons share their setup, so it is expressed once as a
# dependency rather than repeated in each command and run three times.
[tasks.compare]
cmd = "python tests/compare_svgs.py"
depends-on = ["mpl-refs", "test-flang"]

[tasks.compare-png]
cmd = "python tests/compare_pngs.py"
depends-on = ["mpl-refs", "test-flang"]

[tasks.compare-pdf]
cmd = "python tests/compare_pdfs.py"
depends-on = ["mpl-refs", "test-flang"]

[dependencies]
python = ">=3.14.6,<3.15"
matplotlib = ">=3.11.1,<4"
numpy = ">=2.5.1,<3"
cairosvg = ">=2.9.0,<3"
pillow = ">=12.3.0,<13"
# pdftoppm, for rasterizing PDFs in tests/compare_pdfs.py
poppler = "*"
lfortran = ">=0.64.0,<0.65"

[target.linux-64.dependencies]
Expand Down
11 changes: 9 additions & 2 deletions scripts/build_flang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@ cd "$BUILD"
# Compile modules in dependency order
$FLANG -c "$ROOT/src/fplot_colors.f90"
$FLANG -c "$ROOT/src/fplot_style.f90"
$FLANG -c "$ROOT/src/fplot_render.f90"
$FLANG -c "$ROOT/src/fplot_scale.f90"
$FLANG -c "$ROOT/src/fplot_cmap.f90"
$FLANG -c "$ROOT/src/fplot_ticks.f90"
$FLANG -c "$ROOT/src/fplot_contour.f90"
$FLANG -c "$ROOT/src/fplot_svg.f90"
$FLANG -c "$ROOT/src/fplot_backend_svg.f90"
$FLANG -c "$ROOT/src/fplot_glyphs.f90"
$FLANG -c "$ROOT/src/fplot_png.f90"
$FLANG -c "$ROOT/src/fplot_raster.f90"
$FLANG -c "$ROOT/src/fplot_backend_pdf.f90"
$FLANG -c "$ROOT/src/fplot_backend_png.f90"
$FLANG -c "$ROOT/src/fplot.f90"

# Test program
$FLANG -o "$BUILD/test_plots" \
"$ROOT/tests/test_plots.f90" \
fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o
fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o

# Demo
$FLANG -o "$BUILD/demo" \
"$ROOT/examples/demo.f90" \
fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o
fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o

echo "Built: $BUILD/test_plots $BUILD/demo"
11 changes: 9 additions & 2 deletions scripts/build_lfortran.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ cd "$BUILD"
# Prefer a single shared-module build
$LFORTRAN -c "$ROOT/src/fplot_colors.f90"
$LFORTRAN -c "$ROOT/src/fplot_style.f90"
$LFORTRAN -c "$ROOT/src/fplot_render.f90"
$LFORTRAN -c "$ROOT/src/fplot_scale.f90"
$LFORTRAN -c "$ROOT/src/fplot_cmap.f90"
$LFORTRAN -c "$ROOT/src/fplot_ticks.f90"
$LFORTRAN -c "$ROOT/src/fplot_contour.f90"
$LFORTRAN -c "$ROOT/src/fplot_svg.f90"
$LFORTRAN -c "$ROOT/src/fplot_backend_svg.f90"
$LFORTRAN -c "$ROOT/src/fplot_glyphs.f90"
$LFORTRAN -c "$ROOT/src/fplot_png.f90"
$LFORTRAN -c "$ROOT/src/fplot_raster.f90"
$LFORTRAN -c "$ROOT/src/fplot_backend_pdf.f90"
$LFORTRAN -c "$ROOT/src/fplot_backend_png.f90"
$LFORTRAN -c "$ROOT/src/fplot.f90"

$LFORTRAN -o "$BUILD/test_plots" \
"$ROOT/tests/test_plots.f90" \
fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o
fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o

$LFORTRAN -o "$BUILD/demo" \
"$ROOT/examples/demo.f90" \
fplot_colors.o fplot_style.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot.o
fplot_colors.o fplot_style.o fplot_render.o fplot_scale.o fplot_cmap.o fplot_ticks.o fplot_contour.o fplot_svg.o fplot_backend_svg.o fplot_backend_pdf.o fplot_glyphs.o fplot_png.o fplot_raster.o fplot_backend_png.o fplot.o

echo "Built: $BUILD/test_plots $BUILD/demo"
echo "Jupyter: use display_data('image/svg+xml', render_svg()) with lfortran_display"
Loading
Loading