Skip to content

Commit 9cdc8a9

Browse files
committed
Enhance legend labeling logic in _plot_solution_from_container function
1 parent d6c7b86 commit 9cdc8a9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

ext/StatsPlotsExt.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3817,18 +3817,20 @@ function _plot_solution_from_container(;
38173817
if haskey(diffdict, :ignore_obc)
38183818
push!(annotate_diff_input, "Ignore OBC" => reduce(vcat, diffdict[:ignore_obc]))
38193819
end
3820-
3820+
38213821
# Determine legend labels based on what differs
38223822
# If more than one input differs (besides label), use custom labels from diffdict
38233823
len_diff = length(solution_active_plot_container)
38243824

3825+
any_custom_label = any([i != v[:label] for (i,v) in enumerate(solution_active_plot_container)])
3826+
38253827
# Create legend with 2 columns so dynamics and steady state entries are side by side
38263828
legend_plot = StatsPlots.plot(framestyle = :none, legend = :inside, legend_columns = 2)
38273829

38283830
if length(annotate_diff_input) > 2
38293831
# Multiple differences - use custom labels or plot labels
38303832
for (i, container) in enumerate(solution_active_plot_container)
3831-
label_text = container[:label] isa Symbol ? string(container[:label]) : container[:label]
3833+
label_text = container[:label] isa String ? container[:label] : string(container[:label])
38323834

38333835
StatsPlots.plot!([NaN],
38343836
color = pal[mod1(i, length(pal))],
@@ -3841,12 +3843,14 @@ function _plot_solution_from_container(;
38413843
else
38423844
# Single difference (or just labels differ) - use the relevant input difference in legend
38433845
# Get the legend title and labels from the second entry in annotate_diff_input
3844-
legend_title_dynamics = length(annotate_diff_input) > 1 ? annotate_diff_input[2][1] : nothing
3846+
legend_title_dynamics = any_custom_label ? nothing : length(annotate_diff_input) > 1 ? annotate_diff_input[2][1] : nothing
38453847
legend_title_ss = legend_title_dynamics
38463848

38473849
for (i, container) in enumerate(solution_active_plot_container)
38483850
# For single difference, use the value of that difference as the label
3849-
label_text = if length(annotate_diff_input) > 1
3851+
label_text = if any_custom_label
3852+
container[:label] isa String ? container[:label] : string(container[:label])
3853+
elseif length(annotate_diff_input) > 1
38503854
val = annotate_diff_input[2][2][i]
38513855
val isa String ? val : String(Symbol(val))
38523856
else
@@ -3859,7 +3863,9 @@ function _plot_solution_from_container(;
38593863
label = label_text)
38603864

38613865
# For single difference, use the value of that difference as the label
3862-
label_text = if length(annotate_diff_input) > 1
3866+
label_text = if any_custom_label
3867+
container[:label] isa String ? container[:label] : string(container[:label])
3868+
elseif length(annotate_diff_input) > 1
38633869
val = annotate_diff_input[2][2][i]
38643870
val isa String ? val : String(Symbol(val))
38653871
else
@@ -3962,7 +3968,7 @@ function _plot_solution_from_container(;
39623968
layout_heights = [15, length(annotate_diff_input)]
39633969

39643970
# Add relevant input differences table if multiple inputs differ
3965-
if length(annotate_diff_input) > 2
3971+
if length(annotate_diff_input) > 2 || (any_custom_label && len_diff > 1)
39663972
annotate_diff_input_plot = plot_df(annotate_diff_input; fontsize = attributes[:annotationfontsize], title = "Relevant Input Differences")
39673973
ppp_input_diff = StatsPlots.plot(annotate_diff_input_plot; attributes..., framestyle = :box)
39683974
push!(plot_elements, ppp_input_diff)
@@ -4006,7 +4012,7 @@ function _plot_solution_from_container(;
40064012
layout_heights = [15, length(annotate_diff_input)]
40074013

40084014
# Add relevant input differences table if multiple inputs differ
4009-
if length(annotate_diff_input) > 2
4015+
if length(annotate_diff_input) > 2 || (any_custom_label && len_diff > 1)
40104016
annotate_diff_input_plot = plot_df(annotate_diff_input; fontsize = attributes[:annotationfontsize], title = "Relevant Input Differences")
40114017
ppp_input_diff = StatsPlots.plot(annotate_diff_input_plot; attributes..., framestyle = :box)
40124018
push!(plot_elements, ppp_input_diff)

0 commit comments

Comments
 (0)