Skip to content

Commit

Permalink
optional latex in plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
andgoldschmidt committed Nov 11, 2024
1 parent f688055 commit c645a44
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function plot(
ignored_labels::Union{Symbol, Vector{Symbol}, Tuple{Vararg{Symbol}}}=(),
ignore_timestep::Bool=true,
merge_components::Bool=false,
use_latex::Bool=true,

# ---------------------------------------------------------------------------
# transformation keyword arguments
Expand Down Expand Up @@ -107,6 +108,9 @@ function plot(
# ---------------------------------------------------------------------------
kwargs...
)
# set up parse function
parse = use_latex ? latexstring : string

# convert single symbol to vector: comps
if comps isa Symbol
comps = [comps]
Expand Down Expand Up @@ -183,11 +187,16 @@ function plot(
end
end

if isnothing(transformation_titles)
title = parse(name, "(t)", "\\text{ transformation } $j")

Check warning on line 191 in src/plotting.jl

View check run for this annotation

Codecov / codecov/patch

src/plotting.jl#L190-L191

Added lines #L190 - L191 were not covered by tests
else
title = transformation_titles[name][j]

Check warning on line 193 in src/plotting.jl

View check run for this annotation

Codecov / codecov/patch

src/plotting.jl#L193

Added line #L193 was not covered by tests
end

# create axis for transformed data
ax = Axis(
fig[ax_count + 1, 1];

title= isnothing(transformation_titles) ? latexstring(name, "(t)", "\\text{ transformation } $j") : transformation_titles[name][j],
title=title,
titlesize=titlesize,
xlabel=L"t",
limits =(xlims, ylims)
Expand All @@ -200,7 +209,7 @@ function plot(
labels = string.(1:size(transformed_data, 2))
else
labels = [
latexstring(transformation_labels[name][j], "_{$i}")
parse(transformation_labels[name][j], "_{$i}")
for i = 1:size(transformed_data, 2)
]
end
Expand Down Expand Up @@ -237,10 +246,10 @@ function plot(
transformed_data = mapslices(f, data; dims=1)

if isnothing(transformation_titles)
title = latexstring(name, "(t)", "\\text{ transformation}")
title = parse(name, "(t)", "\\text{ transformation}")

Check warning on line 249 in src/plotting.jl

View check run for this annotation

Codecov / codecov/patch

src/plotting.jl#L249

Added line #L249 was not covered by tests
else
if isnothing(transformation_titles[name])
title = latexstring(name, "(t)", "\\text{ transformation}")
title = parse(name, "(t)", "\\text{ transformation}")

Check warning on line 252 in src/plotting.jl

View check run for this annotation

Codecov / codecov/patch

src/plotting.jl#L252

Added line #L252 was not covered by tests
else
title = transformation_titles[name]
end
Expand All @@ -255,11 +264,16 @@ function plot(
end
end

if isnothing(transformation_titles)
title = parse(name, "(t)", "\\text{ transformation}")

Check warning on line 268 in src/plotting.jl

View check run for this annotation

Codecov / codecov/patch

src/plotting.jl#L267-L268

Added lines #L267 - L268 were not covered by tests
else
title = transformation_titles[name]

Check warning on line 270 in src/plotting.jl

View check run for this annotation

Codecov / codecov/patch

src/plotting.jl#L270

Added line #L270 was not covered by tests
end

# create axis for transformed data
ax = Axis(
fig[ax_count + 1, :];
title = isnothing(transformation_titles)
? latexstring(name, "(t)", "\\text{ transformation}") : transformation_titles[name],
title=title,
titlesize=titlesize,
xlabel=L"t",
limits=(xlims, ylims)
Expand All @@ -270,12 +284,12 @@ function plot(

if isnothing(transformation_labels[name])
labels = [
latexstring(name, "_{$i}")
parse(name, "_{$i}")
for i = 1:size(transformed_data, 2)
]
else
labels = [
latexstring(transformation_labels[name], "_{$i}")
parse(transformation_labels[name], "_{$i}")
for i = 1:size(transformed_data, 2)
]
end
Expand Down Expand Up @@ -314,7 +328,7 @@ function plot(
end
ax = Axis(
fig[ax_count + 1, 1];
title=latexstring("Trajectory"),
title=parse("Trajectory"),
titlesize=titlesize,
xlabel=L"t",
limits=(xlims, ylims)
Expand Down Expand Up @@ -344,7 +358,7 @@ function plot(

ax = Axis(
fig[ax_count + 1, 1];
title=latexstring(name, "(t)"),
title=parse(name, "(t)"),
titlesize=titlesize,
xlabel=L"t",
limits =(xlims, ylims)
Expand All @@ -355,7 +369,7 @@ function plot(
if name ignored_labels
labels = nothing
else
labels = [latexstring(name, "_{$i}") for i = 1:size(data, 1)]
labels = [parse(name, "_{$i}") for i = 1:size(data, 1)]
end

# plot data
Expand Down Expand Up @@ -416,4 +430,12 @@ end
@test plot(traj, ylims=(0, 5)) isa Figure
end

@testitem "LaTeX strings" begin
using CairoMakie

# weird names
traj = NamedTrajectory((α_β_2=rand(2, 5), y_1_2=rand(1,5)), controls=:y_1_2, timestep=1.0)
@test plot(traj, use_latex=false) isa Figure
end

end

0 comments on commit c645a44

Please sign in to comment.