Skip to content

Add Makie contour,contourf, contour! and improve multi-variable plot layout#3088

Draft
vincmarks wants to merge 7 commits into
trixi-framework:mainfrom
vincmarks:vm/contour_Makie
Draft

Add Makie contour,contourf, contour! and improve multi-variable plot layout#3088
vincmarks wants to merge 7 commits into
trixi-framework:mainfrom
vincmarks:vm/contour_Makie

Conversation

@vincmarks

@vincmarks vincmarks commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the Makie.jl part of #3075

Added/updated

  • Makie.contour and Makie.contourf for all mesh types
  • Makie.contour! adds colored isolines on top of an existing plot
  • Improved multi-variable plot(pd) layout

Making plot(pd) prettier:

I didn't notice, that for plotting solutions with many variables (e.g examples/tree_2d_dgsem/elixir_acoustics_gauss.jl) using plots(pd), plots can currently look like this (Makie.jl)
before

or like this (Plots.jl)
download-5

I tried to make this more pretty in something like this (Makie.jl)

after

Adding Makie.contour, Makie.contourf and Makie.contour!

Here are some visual results

trixi_include(joinpath(examples_dir(), "unstructured_2d_dgsem",
                       "elixir_acoustics_gauss_wall.jl"))
pd = PlotData2D(sol)

contourf(pd["v1_prime"], plot_mesh = true)
download-2
trixi_include(joinpath(examples_dir(), "tree_2d_dgsem", "elixir_acoustics_gauss.jl"))

pd = PlotData2D(sol)

plot(pd["v1_prime"])
contour!(pd["v1_prime"], labels = true) 
download-6
trixi_include(joinpath(examples_dir(), "tree_2d_dgsem", "elixir_acoustics_gauss.jl"))

pd = PlotData2D(sol)

contourf(pd["v1_prime"])
download-4

@github-actions

Copy link
Copy Markdown
Contributor

Review checklist

This checklist is meant to assist creators of PRs (to let them know what reviewers will typically look for) and reviewers (to guide them in a structured review process). Items do not need to be checked explicitly for a PR to be eligible for merging.

Purpose and scope

  • The PR has a single goal that is clear from the PR title and/or description.
  • All code changes represent a single set of modifications that logically belong together.
  • No more than 500 lines of code are changed or there is no obvious way to split the PR into multiple PRs.

Code quality

  • The code can be understood easily.
  • Newly introduced names for variables etc. are self-descriptive and consistent with existing naming conventions.
  • There are no redundancies that can be removed by simple modularization/refactoring.
  • There are no leftover debug statements or commented code sections.
  • The code adheres to our conventions and style guide, and to the Julia guidelines.

Documentation

  • New functions and types are documented with a docstring or top-level comment.
  • Relevant publications are referenced in docstrings (see example for formatting).
  • Inline comments are used to document longer or unusual code sections.
  • Comments describe intent ("why?") and not just functionality ("what?").
  • If the PR introduces a significant change or new feature, it is documented in NEWS.md with its PR number.

Testing

  • The PR passes all tests.
  • New or modified lines of code are covered by tests.
  • New or modified tests run in less then 10 seconds.

Performance

  • There are no type instabilities or memory allocations in performance-critical parts.
  • If the PR intent is to improve performance, before/after time measurements are posted in the PR.

Verification

  • The correctness of the code was verified using appropriate tests.
  • If new equations/methods are added, a convergence test has been run and the results
    are posted in the PR.

Created with ❤️ by the Trixi.jl community.

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.90%. Comparing base (b6b173e) to head (3761949).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3088      +/-   ##
==========================================
+ Coverage   96.88%   96.90%   +0.02%     
==========================================
  Files         647      647              
  Lines       50026    50325     +299     
==========================================
+ Hits        48466    48766     +300     
+ Misses       1560     1559       -1     
Flag Coverage Δ
unittests 96.90% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vincmarks vincmarks marked this pull request as ready for review June 19, 2026 11:26
@vincmarks

Copy link
Copy Markdown
Contributor Author

@ranocha, the PR is ready for review

@ranocha ranocha added enhancement New feature or request visualization labels Jun 19, 2026

@ranocha ranocha left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, looks like this is a nice feature! However, this is also a big PR. I did not review everything, but just have a few first comments. Would it make sense to split this into smaller and independent pieces, e.g., improving the existing layout, Cartesian, and other meshes?

Comment on lines +1193 to +1198
@timed_testset "Makie contour error handling for 1D solutions" begin
@test_trixi_include(joinpath(EXAMPLES_DIR, "tree_1d_dgsem",
"elixir_advection_basic.jl"))
@test_throws ArgumentError Makie.contour(sol)
@test_throws ArgumentError Makie.tricontourf(sol)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about 3D solutions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I forgot to add them. Thanks!

Comment thread ext/TrixiMakieExt.jl
Comment on lines +170 to +187
# Format colorbar tick labels with enough significant figures to distinguish marks,
# avoiding Makie's default scientific notation
function _trixi_colorbar_tickformat(values)
isempty(values) && return String[]
vmin, vmax = extrema(values)
range_val = vmax - vmin
sigfigs = range_val > 0 ? max(3, ceil(Int, -log10(range_val)) + 2) : 4
return [string(round(v; sigdigits = sigfigs)) for v in values]
end

# Return a non-degenerate (umin, umax) for colorbars; expands zero-width ranges
# so CairoMakie does not produce NaN when all data values are identical.
function _trixi_colorbar_limits(umin, umax)
isapprox(umin, umax) || return (umin, umax)
delta = max(one(umin), abs(umin))
return (umin - delta, umax + delta)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these needed, what is wrong with Makie's defaults?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • _trixi_colorbar_tickformat: Makie's default tick formatter switches to scientific notation for small values, which I thought were not nice. And with very narrow value ranges, the default rounding can also make adjacent ticks indistinguishable
  • _trixi_colorbar_limits: This is needed to prevent a crash when all data values are identical (umin == umax). I ran into this with contourf(pd) on elixir_acoustics_gauss_wall.jl, where rho_mean, v1_mean, v2_mean, c_mean are constant (as shown above in the plots). Makie.tricontourf! crashed with "Can't interpolate in a range where cmin == cmax", and similarly CairoMakie produces NaN when constructing a colorbar with a zero-width range.

@ranocha ranocha Jun 19, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like some of them are workarounds for bugs in Makie.jl. Would you mind filing an issue there or creating a PR to fix the bugs there?
Concerning the tick formatter: Could you please post some example where it changes something? What about cases with values varying over several orders of magnitude?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Here are some visual results regarding the tick-formatter:

without tick-formatter:
download-7

with tick-formatter:

download-8

wtithout tick-formatter:
download-9

with tick-formatter:
download-10

If you prefer it without the tick formatter, I can change the code accordingly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look and feel is always a question of taste, but I like your new version for the first example while I prefer the old version for the second example. Is there a discussion in Makie.jl about the first case, where one should arguably not use the scientific notation?

Comment thread ext/TrixiMakieExt.jl
@vincmarks

Copy link
Copy Markdown
Contributor Author

Thanks a lot, looks like this is a nice feature! However, this is also a big PR. I did not review everything, but just have a few first comments. Would it make sense to split this into smaller and independent pieces, e.g., improving the existing layout, Cartesian, and other meshes?

Yes, I agree, sorry for not following the review checklist guideline of max. 500 lines changed here.
Here's how I'd approach it, if that's more convenient for you:

  • I already have a backup branch with everything as it should look in the end (modulo changes from review comments ;) ).
  • I'd scale this PR back to layout improvements only (multi-variable plot(pd)).
  • Once that's merged, I'll open a second PR for Cartesian contour/contourf/contour!
  • After that, I'll continue with contour plotting for other meshes.

Does that work for you?

@ranocha

ranocha commented Jun 19, 2026

Copy link
Copy Markdown
Member

Sounds good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants