Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to plot contourlines when the geometry is irregular #2715

Open
crazyfireji opened this issue Feb 27, 2023 · 3 comments · May be fixed by #4795
Open

How to plot contourlines when the geometry is irregular #2715

crazyfireji opened this issue Feb 27, 2023 · 3 comments · May be fixed by #4795
Labels
feature request Makie Backend independent issues (Makie core) plot Related to plot object (tri)contour(f) not 3D contour, that's volume

Comments

@crazyfireji
Copy link

crazyfireji commented Feb 27, 2023

Like this picture, the contour! don't work when the geometry irregular.

function Contour_2D_Axis_temp(x, y, f)
    fig = Figure(backgroundcolor=:white, 
    resolution = (1200, 300))
    ax = Axis(fig[1,1],
        xlabel = L"$ x $",
        ylabel = L"$ y $",
        xlabelsize= 40,
        ylabelsize= 40,
        rightspinevisible = true,
        xticklabelsize = 25,
        yticklabelsize = 25,
        # yticks = [0, 1, 2]
    )
    
    nx0, ny0 = size(f)
    plt1 = surface!(ax, x, y, fill(0,(nx0,ny0)), colormap = c,
        color = f, shading = false)
    contour!(ax, x, y, f, levels = [1]);
    Colorbar(fig[1,2], plt1);
    xlims!(ax, [-0.3, 1.2]);
    ylims!(ax, [-0.2, 0.5]);
    tightlimits!(ax);
    display(fig)
end

mmexport1677485677898

@jkrumbiegel
Copy link
Member

We have tricontourf but not tricontour currently. For anyone motivated, tricontour might be relatively straightforward to implement given most of it was done already for tricontourf.

@ffreyer ffreyer added Makie Backend independent issues (Makie core) plot Related to plot object feature request (tri)contour(f) not 3D contour, that's volume labels Aug 24, 2024
@mikeliux
Copy link

mikeliux commented Feb 14, 2025

We have tricontourf but not tricontour currently. For anyone motivated, tricontour might be relatively straightforward to implement given most of it was done already for tricontourf.

I hope a tricontour plot is soon added to Makie. If it is of any use, I would like to share the links to the source code and suggest where changes are needed:

The Makie recipe for tricontourf can be found here:
Makie.jl / src / basic_recipes /tricontourf.jl

In a new file named tricontour.jl, a recipe for Tricontour should be defined. It can be based on the analogous tricontourf.jl file, which defines a recipe for Tricontourf . These functions need minimal change or no change:

  • Makie.used_attributes
  • Makie.used_attributes
  • Makie.convert_arguments
  • compute_contourf_colormap
  • compute_lowcolor
  • compute_highcolor
  • compute_triangulation

Now, the function tricontourf is used to plot filled contours, which are polygons, not lines. In the source code, we can see the function calculate_polys, which ultimatetly creates a TriplotBase.FilledContour struct and calls an external function named TriplotBase.generate_filled_contours.

In the source code of the file Makie.jl / src / Makie.jl, we can see the line:
import TriplotBase

Now, TriplotBase.jl is a backend package that is used for computing both contours and filled contours. This means that, to implement tricontour in Makie, we can use methods from:
TriplotBase.jl / src / tricontour.jl

In this file, you will find the required struct TriplotBase.Contour and function TriplotBase.generate_unfilled_contours

Finally, discourse contains a minimal working example on how to plot contour lines in Makie using TriplotBase, which you can check here
In fact, the existing code for tricontourf was inspired by this example.

@mikeliux
Copy link

mikeliux commented Feb 15, 2025

contours.jl uses a vector of GeometryBasics.Point elements intercalated with NaNs to represent lines. Another approach would be to use GeometryBasics.LineString to represent lines. Which would be more appropriate for Makie's rendering pipeline?

Update: If calling lines!() to plot, it is seems to be best to use points directly.

Explanation: I was storing each contour line as a GeometryBasics.LineString, with a single color value per line. This raised an when the lines! function was called to plot the contour lines. I tracked this error to the function project_transformed_line_points in CairoMakie\src\utils.jl. At this level, two things happen:

  • The LineString has been converted to a vector of points.
  • The code expects either a single color value or an array with color values per point, as can be seen from this line:
    per_point_colors = colors isa AbstractArray

So, either we have two options:

  • Store contours as a vector LineString objects and call lines!() multiple times, using a single color value per line.
  • Store contours as a flat vector of Point2f objects, where lines are separated by NaN32 values. In this case, we should use a color value per point, and we can call lines!() once. This is the approach in contours.jl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Makie Backend independent issues (Makie core) plot Related to plot object (tri)contour(f) not 3D contour, that's volume
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

4 participants