Skip to content

Commit 59735a7

Browse files
authored
Merge pull request #110 from control-toolbox/109-dev-plot-function
Review of plot
2 parents 3a9ea5b + 085317f commit 59735a7

4 files changed

Lines changed: 82 additions & 35 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CTModels"
22
uuid = "34c4fa32-2049-4079-8329-de33c2a22e2d"
33
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
4-
version = "0.3.5"
4+
version = "0.3.6"
55

66
[deps]
77
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"

ext/plot.jl

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ function __plot_time!(
5353
i::Int,
5454
time::Symbol;
5555
t_label::String,
56-
label::String,
56+
y_label::String="",
5757
kwargs...,
5858
)
5959

6060
# t_label depends if time is normalize or not
6161
t_label = @match time begin
6262
:default => t_label
63-
:normalize => "normalized " * t_label
64-
:normalise => "normalised " * t_label
63+
:normalize => t_label == "" ? "" : t_label * " (normalized)"
64+
:normalise => t_label == "" ? "" : t_label * " (normalised)"
6565
_ => throw(
6666
CTBase.IncorrectArgument(
6767
"Internal error, no such choice for time: $time. Use :default, :normalize or :normalise",
@@ -79,7 +79,7 @@ function __plot_time!(
7979
time;
8080
ylims=:auto,
8181
xlabel=t_label,
82-
label=label,
82+
ylabel=y_label,
8383
linewidth=2,
8484
z_order=:front,
8585
kwargs...,
@@ -181,7 +181,7 @@ function __plot_tree(node::PlotNode, depth::Int=0; kwargs...)
181181
kwargs_plot = depth == 0 ? kwargs : ()
182182
ps = @match node.layout begin
183183
:row => plot(subplots...; layout=(1, size(subplots, 1)), kwargs_plot...)
184-
:column => plot(subplots...; layout=(size(subplots, 1), 1), kwargs_plot...)
184+
:column => plot(subplots...; layout=(size(subplots, 1), 1), leftmargin=3mm, kwargs_plot...)
185185
_ => plot(subplots...; layout=node.layout, kwargs_plot...)
186186
end
187187

@@ -467,7 +467,7 @@ function __plot!(
467467

468468
# add an empty space to the label if the label is not empty
469469
if solution_label != ""
470-
solution_label = " " * solution_label
470+
@warn "Deprecated: `solution_label` keyword argument is replaced by `label`."
471471
end
472472

473473
#
@@ -479,7 +479,8 @@ function __plot!(
479479
t_label = CTModels.time_name(sol)
480480

481481
#
482-
title_font = font(12, Plots.default(:fontfamily))
482+
title_font = font(10, Plots.default(:fontfamily))
483+
label_font_size = 10
483484

484485
# split series attributes
485486
series_attr = __keep_series_attributes(; kwargs...)
@@ -500,7 +501,7 @@ function __plot!(
500501
:state,
501502
time;
502503
t_label=t_label,
503-
labels=x_labels .* solution_label,
504+
labels=x_labels,
504505
title="state",
505506
titlefont=title_font,
506507
lims=:auto,
@@ -520,7 +521,7 @@ function __plot!(
520521
:costate,
521522
time;
522523
t_label=t_label,
523-
labels="p" .* x_labels .* solution_label,
524+
labels="p" .* x_labels,
524525
title="costate",
525526
titlefont=title_font,
526527
lims=:auto,
@@ -542,7 +543,7 @@ function __plot!(
542543
:control,
543544
time;
544545
t_label=t_label,
545-
labels=u_labels .* solution_label,
546+
labels=u_labels,
546547
title="control",
547548
titlefont=title_font,
548549
lims=:auto,
@@ -560,7 +561,7 @@ function __plot!(
560561
-1,
561562
time;
562563
t_label=t_label,
563-
label="" * u_label * "" .* solution_label,
564+
label="" * u_label * "",
564565
title="control norm",
565566
titlefont=title_font,
566567
lims=:auto,
@@ -578,7 +579,7 @@ function __plot!(
578579
:control,
579580
time;
580581
t_label=t_label,
581-
labels=u_labels .* solution_label,
582+
labels=u_labels,
582583
title="control",
583584
titlefont=title_font,
584585
lims=:auto,
@@ -594,7 +595,7 @@ function __plot!(
594595
-1,
595596
time;
596597
t_label=t_label,
597-
label="" * u_label * "" .* solution_label,
598+
label="" * u_label * "",
598599
title="control norm",
599600
titlefont=title_font,
600601
lims=:auto,
@@ -624,6 +625,7 @@ function __plot!(
624625

625626
# state trajectory
626627
for i in 1:n
628+
title = i==1 ? "state" : ""
627629
__plot_time!(
628630
p[icur],
629631
sol,
@@ -632,7 +634,12 @@ function __plot!(
632634
i,
633635
time;
634636
t_label=i==n ? t_label : "",
635-
label=x_labels[i] * solution_label,
637+
xguidefontsize=label_font_size,
638+
y_label=x_labels[i],
639+
yguidefontsize=label_font_size,
640+
label="",
641+
title=title,
642+
titlefont=title_font,
636643
series_attr...,
637644
state_style...,
638645
)
@@ -648,20 +655,20 @@ function __plot!(
648655
[cs[1][i]];
649656
color=4,
650657
linewidth=1,
651-
label=:none,
652658
z_order=:back,
653659
series_attr...,
654660
state_bounds_style...,
661+
label=:none,
655662
) # lower bound
656663
hline!(
657664
p[is + cs[2][i] - 1],
658665
[cs[3][i]];
659666
color=4,
660667
linewidth=1,
661-
label=:none,
662668
z_order=:back,
663669
series_attr...,
664670
state_bounds_style...,
671+
label=:none,
665672
) # upper bound
666673
end
667674
end
@@ -670,6 +677,8 @@ function __plot!(
670677
# costate
671678
if do_plot_costate
672679
for i in 1:n
680+
title = i==1 ? "costate" : ""
681+
y_label = do_plot_state ? "" : "p" * x_labels[i]
673682
__plot_time!(
674683
p[icur],
675684
sol,
@@ -678,7 +687,12 @@ function __plot!(
678687
i,
679688
time;
680689
t_label=i==n ? t_label : "",
681-
label="p" * x_labels[i] * solution_label,
690+
xguidefontsize=label_font_size,
691+
y_label=y_label,
692+
yguidefontsize=label_font_size,
693+
label="",
694+
title=title,
695+
titlefont=title_font,
682696
series_attr...,
683697
costate_style...,
684698
)
@@ -697,6 +711,7 @@ function __plot!(
697711
@match control begin
698712
:components => begin
699713
for i in 1:m
714+
title = i==1 ? "control" : ""
700715
__plot_time!(
701716
p[icur],
702717
sol,
@@ -705,7 +720,12 @@ function __plot!(
705720
i,
706721
time;
707722
t_label=i==m ? t_label : "",
708-
label=u_labels[i] * solution_label,
723+
xguidefontsize=label_font_size,
724+
y_label=u_labels[i],
725+
yguidefontsize=label_font_size,
726+
label="",
727+
title=title,
728+
titlefont=title_font,
709729
series_attr...,
710730
control_style...,
711731
)
@@ -714,6 +734,7 @@ function __plot!(
714734
end
715735
:norm => begin
716736
l = 1
737+
title = "control"
717738
__plot_time!(
718739
p[icur],
719740
sol,
@@ -722,7 +743,12 @@ function __plot!(
722743
-1,
723744
time;
724745
t_label=t_label,
725-
label="" * u_label * "" * solution_label,
746+
xguidefontsize=label_font_size,
747+
y_label="" * u_label * "",
748+
yguidefontsize=label_font_size,
749+
label="",
750+
title=title,
751+
titlefont=title_font,
726752
series_attr...,
727753
control_style...,
728754
)
@@ -731,6 +757,7 @@ function __plot!(
731757
:all => begin
732758
l = m + 1
733759
for i in 1:m
760+
title = i==1 ? "control" : ""
734761
__plot_time!(
735762
p[icur],
736763
sol,
@@ -739,7 +766,11 @@ function __plot!(
739766
i,
740767
time;
741768
t_label="",
742-
label=u_labels[i] * solution_label,
769+
y_label=u_labels[i],
770+
yguidefontsize=label_font_size,
771+
label="",
772+
title=title,
773+
titlefont=title_font,
743774
series_attr...,
744775
control_style...,
745776
)
@@ -753,7 +784,10 @@ function __plot!(
753784
-1,
754785
time;
755786
t_label=t_label,
756-
label="" * u_label * "" * solution_label,
787+
xguidefontsize=label_font_size,
788+
y_label="" * u_label * "",
789+
yguidefontsize=label_font_size,
790+
label="",
757791
series_attr...,
758792
control_style...,
759793
)
@@ -775,20 +809,20 @@ function __plot!(
775809
[cu[1][i]];
776810
color=4,
777811
linewidth=1,
778-
label=:none,
779812
z_order=:back,
780813
series_attr...,
781814
control_bounds_style...,
815+
label=:none,
782816
) # lower bound
783817
hline!(
784818
p[iu + cu[2][i] - 1],
785819
[cu[3][i]];
786820
color=4,
787821
linewidth=1,
788-
label=:none,
789822
z_order=:back,
790823
series_attr...,
791824
control_bounds_style...,
825+
label=:none,
792826
) # upper bound
793827
end
794828
end
@@ -813,6 +847,7 @@ function __plot!(
813847

814848
# path constraints trajectory
815849
for i in 1:nc
850+
title = i==1 ? "path constraints" : ""
816851
__plot_time!(
817852
p[icur],
818853
sol,
@@ -821,7 +856,12 @@ function __plot!(
821856
i,
822857
time;
823858
t_label=i==nc ? t_label : "",
824-
label=string(cp[4][i]) * solution_label,
859+
xguidefontsize=label_font_size,
860+
y_label=string(cp[4][i]),
861+
yguidefontsize=label_font_size,
862+
label="",
863+
title=title,
864+
titlefont=title_font,
825865
series_attr...,
826866
path_style...,
827867
)
@@ -836,20 +876,20 @@ function __plot!(
836876
[cp[1][i]];
837877
color=4,
838878
linewidth=1,
839-
label=:none,
840879
z_order=:back,
841880
series_attr...,
842881
path_bounds_style...,
882+
label=:none,
843883
) # lower bound
844884
hline!(
845885
p[ic + i - 1],
846886
[cp[3][i]];
847887
color=4,
848888
linewidth=1,
849-
label=:none,
850889
z_order=:back,
851890
series_attr...,
852891
path_bounds_style...,
892+
label=:none,
853893
) # upper bound
854894
end
855895
end
@@ -858,6 +898,8 @@ function __plot!(
858898
# dual variables
859899
if do_plot_dual
860900
for i in 1:nc
901+
title = i==1 ? "dual" : ""
902+
y_label = do_plot_path ? "" : "dual " * string(cp[4][i])
861903
__plot_time!(
862904
p[icur],
863905
sol,
@@ -866,7 +908,12 @@ function __plot!(
866908
i,
867909
time;
868910
t_label=i==nc ? t_label : "",
869-
label="dual " * string(cp[4][i]) * solution_label,
911+
xguidefontsize=label_font_size,
912+
y_label=y_label,
913+
yguidefontsize=label_font_size,
914+
label="",
915+
title=title,
916+
titlefont=title_font,
870917
series_attr...,
871918
dual_style...,
872919
)
@@ -902,10 +949,10 @@ function __plot!(
902949
color=:black,
903950
linestyle=:dash,
904951
linewidth=1,
905-
label=:none,
906952
z_order=:back,
907953
series_attr...,
908954
time_style...,
955+
label=:none,
909956
)
910957
end
911958
end

test/extras/plot_duals.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end;
6262
sol = SOL(ocp, t0, tf);
6363

6464
# from description
65-
plt = plot(sol, ocp)
65+
plt = plot(sol, ocp; label="tata")
6666
plt = plot(sol, ocp; layout=:group)
6767
plt = plot(sol, ocp, :state)
6868
plt = plot(sol, ocp, :state, :costate)
@@ -87,7 +87,7 @@ plot(sol, ocp; layout=:split, path_style=:none, control_style=:none)
8787
plot(sol, ocp; layout=:split, dual_style=:none, control_style=:none)
8888

8989
# no decorations
90-
plot(sol, ocp; layout=:split, time_style=:none)
90+
plot(sol, ocp; layout=:split, time_style=:none, label="toto")
9191
plot(sol, ocp; layout=:split, state_bounds_style=:none)
9292
plot(sol, ocp; layout=:split, control_bounds_style=:none)
9393
plot(sol, ocp; layout=:split, path_bounds_style=:none)

0 commit comments

Comments
 (0)