@@ -18,6 +18,10 @@ Renaming and reexport of Plot.jl function `plotlyjs()` to define PlotlyJS.jl as
18
18
"""
19
19
plotlyjs_backend = StatsPlots. plotlyjs
20
20
21
+ const _LAST_PLOTS = Ref {Any} (nothing )
22
+
23
+ _store_last_plots! (p) = (_LAST_PLOTS[] = p)
24
+
21
25
22
26
23
27
"""
@@ -53,7 +57,8 @@ If occasionally binding constraints are present in the model, they are not taken
53
57
- $MAX_ELEMENTS_PER_LEGENDS_ROW®
54
58
- $EXTRA_LEGEND_SPACE®
55
59
- $PLOT_ATTRIBUTES®
56
- - `line_labels` [Optional, `Vector{String}`]: legend labels for the shocks.
60
+ - `line_label` [Optional, `String`]: legend label for the lines produced by a
61
+ single call. When omitted, no legend entry is added.
57
62
- $QME®
58
63
- $SYLVESTER®
59
64
- $LYAPUNOV®
@@ -407,6 +412,7 @@ function plot_model_estimates(𝓂::ℳ,
407
412
end
408
413
end
409
414
415
+ _store_last_plots! (return_plots)
410
416
return return_plots
411
417
end
412
418
@@ -499,7 +505,7 @@ function plot_irf(𝓂::ℳ;
499
505
initial_state:: Union{Vector{Vector{Float64}},Vector{Float64}} = [0.0 ],
500
506
ignore_obc:: Bool = false ,
501
507
plot_attributes:: Dict = Dict (),
502
- line_labels :: Union{Nothing,Vector{ String} } = nothing ,
508
+ line_label :: Union{Nothing,String} = nothing ,
503
509
verbose:: Bool = false ,
504
510
tol:: Tolerances = Tolerances (),
505
511
quadratic_matrix_equation_algorithm:: Symbol = :schur ,
@@ -555,18 +561,16 @@ function plot_irf(𝓂::ℳ;
555
561
obc_shocks_included = stochastic_model && obc_model && (intersect ((((shock_idx isa Vector) || (shock_idx isa UnitRange)) && (length (shock_idx) > 0 )) ? 𝓂. timings. exo[shock_idx] : [𝓂. timings. exo[shock_idx]], 𝓂. timings. exo[contains .(string .(𝓂. timings. exo)," ᵒᵇᶜ" )]) != [])
556
562
end
557
563
558
- if line_labels === nothing
559
- if shocks == :simulate
560
- line_labels = [" simulate all" ]
564
+ if line_label === nothing
565
+ if (shocks isa Union{Symbol_input,String_input}) && (length (shock_idx) == 1 )
566
+ line_label = replace_indices_in_symbol (𝓂. timings. exo[shock_idx])
567
+ elseif shocks == :simulate
568
+ line_label = " simulate all"
561
569
elseif shocks == :none
562
- line_labels = [" none" ]
563
- elseif shocks isa Union{Symbol_input,String_input}
564
- line_labels = replace_indices_in_symbol .(𝓂. timings. exo[shock_idx])
570
+ line_label = " none"
565
571
else
566
- line_labels = [ " shock " * string (i) for i in 1 : length (shock_idx)]
572
+ line_label = " "
567
573
end
568
- else
569
- @assert length (line_labels) == length (shock_idx) " line_labels must match number of shocks"
570
574
end
571
575
572
576
if shocks isa KeyedArray{Float64} || shocks isa Matrix{Float64}
@@ -786,6 +790,7 @@ function plot_irf(𝓂::ℳ;
786
790
pp = []
787
791
pane = 1
788
792
plot_count = 1
793
+ label_done = false
789
794
for i in 1 : length (var_idx)
790
795
if all (isapprox .(Y[i,:,shock], 0 , atol = eps (Float32)))
791
796
n_subplots -= 1
@@ -798,11 +803,12 @@ function plot_irf(𝓂::ℳ;
798
803
can_dual_axis = gr_back && all ((Y[i,:,shock] .+ SS) .> eps (Float32)) && (SS > eps (Float32))
799
804
800
805
if ! (all (isapprox .(Y[i,:,shock],0 ,atol = eps (Float32))))
806
+ label_here = (! label_done && line_label != " " ) ? line_label : " "
801
807
push! (pp,begin
802
808
StatsPlots. plot (Y[i,:,shock] .+ SS,
803
809
title = replace_indices_in_symbol (𝓂. timings. var[var_idx[i]]),
804
810
ylabel = " Level" ,
805
- label = line_labels[shock] )
811
+ label = label_here )
806
812
807
813
if can_dual_axis
808
814
StatsPlots. plot! (StatsPlots. twinx (),
@@ -850,6 +856,7 @@ function plot_irf(𝓂::ℳ;
850
856
pane += 1
851
857
852
858
pp = []
859
+ label_done = false
853
860
end
854
861
end
855
862
end
@@ -880,8 +887,10 @@ function plot_irf(𝓂::ℳ;
880
887
if save_plots
881
888
StatsPlots. savefig (p, save_plots_path * " /irf__" * 𝓂. model_name * " __" * shock_name * " __" * string (pane) * " ." * string (save_plots_format))
882
889
end
890
+ label_done = false
883
891
end
884
892
end
893
+ _store_last_plots! (return_plots)
885
894
886
895
return return_plots
887
896
end
@@ -1123,6 +1132,7 @@ function plot_conditional_variance_decomposition(𝓂::ℳ;
1123
1132
StatsPlots. savefig (p, save_plots_path * " /fevd__" * 𝓂. model_name * " __" * string (pane) * " ." * string (save_plots_format))
1124
1133
end
1125
1134
end
1135
+ _store_last_plots! (return_plots)
1126
1136
1127
1137
return return_plots
1128
1138
end
@@ -1494,6 +1504,7 @@ function plot_solution(𝓂::ℳ,
1494
1504
end
1495
1505
end
1496
1506
1507
+ _store_last_plots! (return_plots)
1497
1508
return return_plots
1498
1509
end
1499
1510
@@ -1866,6 +1877,7 @@ function plot_conditional_forecast(𝓂::ℳ,
1866
1877
StatsPlots. savefig (p, save_plots_path * " /conditional_forecast__" * 𝓂. model_name * " __" * string (pane) * " ." * string (save_plots_format))
1867
1878
end
1868
1879
end
1880
+ _store_last_plots! (return_plots)
1869
1881
1870
1882
return return_plots
1871
1883
@@ -1878,8 +1890,8 @@ Add the IRFs produced by [`plot_irf`](@ref) to the existing plot or vector of
1878
1890
plots `p` using `StatsPlots.plot!`.
1879
1891
1880
1892
Calling `plot_irf!(args...; kwargs...)` without providing `p` attempts to add
1881
- the lines to the current plot. Additional pages are appended if required and
1882
- legend labels are taken from the shock names . Subplots are matched by title when
1893
+ the lines to the previous plot. Additional pages are appended if required and
1894
+ legend labels are derived from the `line_label` keyword . Subplots are matched by title when
1883
1895
merging so the order of variables does not matter. Titles from both plots are
1884
1896
collected, combined into a sorted list and used to align the panels.
1885
1897
"""
@@ -1920,13 +1932,17 @@ function plot_irf!(p::Union{StatsPlots.Plot,AbstractVector}, args...; kwargs...)
1920
1932
else
1921
1933
p = _merge_plots_by_title (p, q[1 ])
1922
1934
end
1935
+ _store_last_plots! (p)
1923
1936
return p
1924
1937
end
1925
1938
function plot_irf! (args... ; kwargs... )
1926
- p = try
1927
- StatsPlots. current ()
1928
- catch
1929
- nothing
1939
+ p = _LAST_PLOTS[]
1940
+ if p === nothing
1941
+ p = try
1942
+ StatsPlots. current ()
1943
+ catch
1944
+ nothing
1945
+ end
1930
1946
end
1931
1947
if p === nothing
1932
1948
return plot_irf (args... ; kwargs... )
@@ -1953,14 +1969,18 @@ function plot_model_estimates!(p::Union{StatsPlots.Plot,AbstractVector}, args...
1953
1969
else
1954
1970
StatsPlots. plot! (p, q[1 ])
1955
1971
end
1972
+ _store_last_plots! (p)
1956
1973
return p
1957
1974
end
1958
1975
1959
1976
function plot_model_estimates! (args... ; kwargs... )
1960
- p = try
1961
- StatsPlots. current ()
1962
- catch
1963
- nothing
1977
+ p = _LAST_PLOTS[]
1978
+ if p === nothing
1979
+ p = try
1980
+ StatsPlots. current ()
1981
+ catch
1982
+ nothing
1983
+ end
1964
1984
end
1965
1985
if p === nothing
1966
1986
return plot_model_estimates (args... ; kwargs... )
@@ -1988,14 +2008,18 @@ function plot_conditional_variance_decomposition!(p::Union{StatsPlots.Plot,Abstr
1988
2008
else
1989
2009
StatsPlots. plot! (p, q[1 ])
1990
2010
end
2011
+ _store_last_plots! (p)
1991
2012
return p
1992
2013
end
1993
2014
1994
2015
function plot_conditional_variance_decomposition! (args... ; kwargs... )
1995
- p = try
1996
- StatsPlots. current ()
1997
- catch
1998
- nothing
2016
+ p = _LAST_PLOTS[]
2017
+ if p === nothing
2018
+ p = try
2019
+ StatsPlots. current ()
2020
+ catch
2021
+ nothing
2022
+ end
1999
2023
end
2000
2024
if p === nothing
2001
2025
return plot_conditional_variance_decomposition (args... ; kwargs... )
@@ -2020,14 +2044,18 @@ function plot_solution!(p::Union{StatsPlots.Plot,AbstractVector}, args...; kwarg
2020
2044
else
2021
2045
StatsPlots. plot! (p, q[1 ])
2022
2046
end
2047
+ _store_last_plots! (p)
2023
2048
return p
2024
2049
end
2025
2050
2026
2051
function plot_solution! (args... ; kwargs... )
2027
- p = try
2028
- StatsPlots. current ()
2029
- catch
2030
- nothing
2052
+ p = _LAST_PLOTS[]
2053
+ if p === nothing
2054
+ p = try
2055
+ StatsPlots. current ()
2056
+ catch
2057
+ nothing
2058
+ end
2031
2059
end
2032
2060
if p === nothing
2033
2061
return plot_solution (args... ; kwargs... )
@@ -2053,14 +2081,18 @@ function plot_conditional_forecast!(p::Union{StatsPlots.Plot,AbstractVector}, ar
2053
2081
else
2054
2082
StatsPlots. plot! (p, q[1 ])
2055
2083
end
2084
+ _store_last_plots! (p)
2056
2085
return p
2057
2086
end
2058
2087
2059
2088
function plot_conditional_forecast! (args... ; kwargs... )
2060
- p = try
2061
- StatsPlots. current ()
2062
- catch
2063
- nothing
2089
+ p = _LAST_PLOTS[]
2090
+ if p === nothing
2091
+ p = try
2092
+ StatsPlots. current ()
2093
+ catch
2094
+ nothing
2095
+ end
2064
2096
end
2065
2097
if p === nothing
2066
2098
return plot_conditional_forecast (args... ; kwargs... )
0 commit comments