Skip to content

Commit

Permalink
improve label handling for scalarplot1D for Makie
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu committed Jun 25, 2023
1 parent 85bf8cf commit f29777f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GridVisualize"
uuid = "5eed8a63-0fb0-45eb-886d-8d5a387d12b8"
authors = ["Juergen Fuhrmann <[email protected]>"]
version = "1.1"
version = "1.1.1"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
2 changes: 1 addition & 1 deletion examples/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ end
# switch between gallery view (default) and focused view of only
# one subscene.
function plotting_multiscene!(p)
gridplot!(p[1, 1], grid1d(); title = "1D grid")
gridplot!(p[1, 1], grid1d(); title = "1D grid", legend=:rt)
scalarplot!(
p[2, 1],
grid1d(),
Expand Down
51 changes: 43 additions & 8 deletions src/makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,28 @@ add_scene!(ctx, ax) = ctx[:flayout][ctx[:subplot]...] = ax

# Revealing the visualizer just returns the figure
function reveal(p::GridVisualizer, ::Type{MakieType})
Plotter = p.context[:Plotter]
XMakie = p.context[:Plotter]
layout = p.context[:layout]
# For 1D plots the legend should be rendered only once,
# when all lines+labels are defined
for I in CartesianIndices(layout)
ctx = p.subplots[I]
if ctx[:legend] != :none && haskey(ctx, :scalarplot1d)
if !haskey(ctx, :axislegend)
pos = ctx[:legend] == :best ? :rt : ctx[:legend]
ctx[:axislegend] = XMakie.axislegend(
ctx[:scene];
position = pos,
labelsize = 0.5 * ctx[:fontsize],
bgcolor = RGBA(1.0, 1.0, 1.0, 0.85),
)
end
end

end

if haskey(p.context, :videostream)
Plotter.recordframe!(p.context[:videostream])
XMakie.recordframe!(p.context[:videostream])
else
p.context[:figure]
end
Expand Down Expand Up @@ -328,7 +347,7 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
if ctx[:title] == ""
ctx[:title] = " "
end

ctx[:scalarplot1d] = true
# ... keep this for the case we are unsorted
function polysegs(grid, func)
points = Vector{Point2f}(undef, 0)
Expand All @@ -353,18 +372,23 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
coord = parentgrid[Coordinates]
xlimits = ctx[:xlimits]
ylimits = ctx[:limits]

xmin = coord[1, 1]
xmax = coord[1, end]
xauto = true
yauto = true
if xlimits[1] < xlimits[2]
xmin = xlimits[1]
xmax = xlimits[2]
xauto = false
end



if ylimits[1] < ylimits[2]
ymin = ylimits[1]
ymax = ylimits[2]
yauto = false
else
ext = extrema.(funcs)
(ymin, ymax) = (minimum(first.(ext)), maximum(last.(ext)))
Expand All @@ -388,6 +412,7 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
map(a -> a[1:1], ctx[:lines][newrange[begin]]);
linestyle = ctx[:linestyle],
linewidth = ctx[:linewidth],
markersize = 0.1,
color = RGB(ctx[:color]),
label = ctx[:label],
)
Expand Down Expand Up @@ -433,24 +458,29 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
end
end

if ctx[:legend] != :none
pos = ctx[:legend] == :best ? :rt : ctx[:legend]
XMakie.axislegend(ctx[:scene]; position = pos, labelsize = 0.5 * ctx[:fontsize])
# ,backgroundcolor=:transparent
end
end

if !haskey(ctx, :scene)
ctx[:xtitle] = Observable(ctx[:title])


# Axis
ctx[:scene] = XMakie.Axis(
ctx[:figure];
title = map(a -> a, ctx[:xtitle]),
xscale = ctx[:xscale] == :log ? log10 : identity,
yscale = ctx[:yscale] == :log ? log10 : identity,
xlabel = ctx[:xlabel],
ylabel = ctx[:ylabel],
scenekwargs(ctx)...,
)

if !xauto
XMakie.xlims!(ctx[:scene], xmin, xmax)
end
if !yauto
XMakie.ylims!(ctx[:scene], ymin, ymax)
end
# Plot size
XMakie.scatter!(
ctx[:scene],
Expand All @@ -475,6 +505,7 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
add_scene!(ctx, ctx[:scene])

else

if ctx[:clear]
ctx[:nlines] = nfuncs
else
Expand All @@ -495,12 +526,16 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
end
r1 = length(ctx[:lines])
update_lines(ctx, r0+1:r1)


end


XMakie.reset_limits!(ctx[:scene])

ctx[:xtitle][] = ctx[:title]
end

reveal(ctx, TP)
end

Expand Down

2 comments on commit f29777f

@j-fu
Copy link
Member Author

@j-fu j-fu commented on f29777f Jun 25, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/86236

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.1 -m "<description of version>" f29777f53a78b709f597a290579f191982dc32ae
git push origin v1.1.1

Please sign in to comment.