Ferrite-FEM / FerriteViz.jl

Plot your Ferrite.jl data
https://ferrite-fem.github.io/FerriteViz.jl/
MIT License
31 stars 9 forks source link

Use Makie.automatic for colorrange #120

Closed KnutAM closed 7 months ago

KnutAM commented 9 months ago

supersedes #119, thanks for the tip @termi-official on Makie.automatic (haven't checked if this introduces new compat requirements)

Reproducer

using Ferrite, FerriteViz
import GLMakie as Plt

grid = generate_grid(Quadrilateral, (2, 2))

dh = DofHandler(grid)
add!(dh, :u, 1, Lagrange{2,RefCube,1}())
close!(dh)

a = zeros(ndofs(dh))

apply_analytical!(a, dh, :u, x -> rand() - 0.5) # ∈ [-0.5, 0.5]

plotter = MakiePlotter(dh, a)

fig = Plt.Figure()
ax = Plt.Axis(fig[1,1])
sp = solutionplot!(ax, plotter; colorrange=(-1.0, 1.0))
Plt.Colorbar(fig[1,2], sp)
fig

master (colorrange [-1, 1] not respected): master

pr (colorrange [-1, 1] respected) pr

koehlerson commented 9 months ago

Can we add this for all plot types?

I think it's just CellPlot that needs to be changed too https://github.com/Ferrite-FEM/FerriteViz.jl/blob/24a611238873a67d2d8918de98f41887a1b3f6a8/src/makieplotting.jl#L118

termi-official commented 9 months ago

Does this work if min approx max and min or max approx zero? We introduced the fix back then, because the automatic colorrange failed to display at all in these cases.

koehlerson commented 9 months ago

ah misread the PR example, I thought it display $[-1,-1]$ and thought that it can handle this now. But yeah this would be good to test

KnutAM commented 9 months ago

Does this work if min approx max and min or max approx zero? We introduced the fix back then, because the automatic colorrange failed to display at all in these cases.

It seems to work, but gives the warning (4 times)

┌ Warning: No strict ticks found
└ @ PlotUtils C:\Users\meyer\.julia\packages\PlotUtils\jEGKP\src\ticks.jl:191
using Ferrite, FerriteViz
import GLMakie as Plt

grid = generate_grid(Quadrilateral, (2, 2))

dh = DofHandler(grid)
add!(dh, :u, 1, Lagrange{2,RefCube,1}())
close!(dh)

a = zeros(ndofs(dh))

apply_analytical!(a, dh, :u, x -> rand() - 0.5) # ∈ [-0.5, 0.5]

plotter = MakiePlotter(dh, a)

fig = Plt.Figure()
ax = Plt.Axis(fig[1,1])
sp = solutionplot!(ax, plotter; colorrange=(-1.0, 1.0))
Plt.Colorbar(fig[1,2], sp)
fig

# Case with zero amplitude of data
apply_analytical!(a, dh, :u, Returns(0.0))
p2 = MakiePlotter(dh, a)
ax2 = Plt.Axis(fig[2,1])
sp2 = solutionplot!(ax2, p2)
Plt.Colorbar(fig[2,2], sp2)

# Case with almost zero amplitude of data (works but warns)
apply_analytical!(a, dh, :u, x -> 1e-20*rand())
p3 = MakiePlotter(dh, a)
ax3 = Plt.Axis(fig[3,1])
sp3 = solutionplot!(ax3, p2)
Plt.Colorbar(fig[3,2], sp3)

fig

mwe

termi-official commented 9 months ago

Does this work if min approx max and min or max approx zero? We introduced the fix back then, because the automatic colorrange failed to display at all in these cases.

using Ferrite, FerriteViz
import GLMakie as Plt

grid = generate_grid(Quadrilateral, (2, 2))

dh = DofHandler(grid)
add!(dh, :u, 1, Lagrange{2,RefCube,1}())
close!(dh)

a = zeros(ndofs(dh))

plotter = MakiePlotter(dh, a)

fig = Plt.Figure()
ax = Plt.Axis(fig[1,1])
sp = solutionplot!(ax, plotter)
Plt.Colorbar(fig[1,2], sp)
fig
using Ferrite, FerriteViz
import GLMakie as Plt

grid = generate_grid(Quadrilateral, (2, 2))

dh = DofHandler(grid)
add!(dh, :u, 1, Lagrange{2,RefCube,1}())
close!(dh)

a = ones(ndofs(dh))

plotter = MakiePlotter(dh, a)

fig = Plt.Figure()
ax = Plt.Axis(fig[1,1])
sp = solutionplot!(ax, plotter)
Plt.Colorbar(fig[1,2], sp)
fig
using Ferrite, FerriteViz
import GLMakie as Plt

grid = generate_grid(Quadrilateral, (2, 2))

dh = DofHandler(grid)
add!(dh, :u, 1, Lagrange{2,RefCube,1}())
close!(dh)

a = rand(ndofs(dh))

plotter = MakiePlotter(dh, a)

fig = Plt.Figure()
ax = Plt.Axis(fig[1,1])
sp = solutionplot!(ax, plotter)
Plt.Colorbar(fig[1,2], sp)
fig

All work now with the automatic keyword (at least on the latest Makie release). :rocket:

Can you add the corresponding change also to CellPlot as Max proposed. And add the information to the docstrings, because it seems this option is missing. Thanks!

termi-official commented 9 months ago

I have locally a fix to recover the NaN handling. If @koehlerson is fine with the changes I will push, we make another round of review and merge.