Skip to content

Commit ec1d466

Browse files
authored
Mb/bar plot fix (#140)
1 parent f059531 commit ec1d466

3 files changed

Lines changed: 49 additions & 26 deletions

File tree

ext/plot_recipes.jl

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ function PowerGraphics._dataframe_plots_internal(
7979
plot.axis.title = title
8080
end
8181

82+
# For stacked bar plots CairoMakie's auto-legend extraction fails because a
83+
# single barplot! object with a vector `color` attribute has no per-element
84+
# color→label mapping. We capture the entries here and build the legend
85+
# manually with PolyElement below.
86+
bar_legend_entries = nothing
87+
8288
if bar
8389
plot_data = sum(data; dims = 1) ./ interval
8490

@@ -90,14 +96,16 @@ function PowerGraphics._dataframe_plots_internal(
9096
n = length(labels)
9197
xs = fill(1, n)
9298
heights = vec(plot_data)
99+
bar_colors = collect(seriescolor[1:n])
93100
CairoMakie.barplot!(
94101
plot.axis,
95102
xs,
96103
heights;
97104
stack = collect(1:n),
98-
color = collect(seriescolor[1:n]),
105+
color = bar_colors,
99106
label = string.(labels),
100107
)
108+
bar_legend_entries = (string.(labels), bar_colors)
101109

102110
plot.axis.xticks = ([1], [""])
103111
else
@@ -259,16 +267,41 @@ function PowerGraphics._dataframe_plots_internal(
259267
end
260268

261269
if legend_position == :bottom
262-
CairoMakie.Legend(
263-
plot.figure[2, 1],
264-
plot.axis;
265-
orientation = :horizontal,
266-
tellwidth = false,
267-
tellheight = true,
268-
legend_kwargs...,
269-
)
270+
if !isnothing(bar_legend_entries)
271+
bar_labels, bar_colors = bar_legend_entries
272+
elems = [CairoMakie.PolyElement(; color = c) for c in bar_colors]
273+
CairoMakie.Legend(
274+
plot.figure[2, 1],
275+
elems,
276+
bar_labels;
277+
orientation = :horizontal,
278+
tellwidth = false,
279+
tellheight = true,
280+
legend_kwargs...,
281+
)
282+
else
283+
CairoMakie.Legend(
284+
plot.figure[2, 1],
285+
plot.axis;
286+
orientation = :horizontal,
287+
tellwidth = false,
288+
tellheight = true,
289+
legend_kwargs...,
290+
)
291+
end
270292
else
271-
CairoMakie.Legend(plot.figure[1, 2], plot.axis; legend_kwargs...)
293+
if !isnothing(bar_legend_entries)
294+
bar_labels, bar_colors = bar_legend_entries
295+
elems = [CairoMakie.PolyElement(; color = c) for c in bar_colors]
296+
CairoMakie.Legend(
297+
plot.figure[1, 2],
298+
elems,
299+
bar_labels;
300+
legend_kwargs...,
301+
)
302+
else
303+
CairoMakie.Legend(plot.figure[1, 2], plot.axis; legend_kwargs...)
304+
end
272305
end
273306
plot.has_legend = true
274307
end

src/call_plots.jl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -752,19 +752,6 @@ function _plot_fuel!(p, result::IS.Results, backend; kwargs...)
752752
kwargs[:filter_func] = filter_func
753753

754754
if load
755-
# Net-load line = demand + storage charging, so it coincides with the
756-
# top of the generation stack (charging is drawn as a negative band by
757-
# the sign-aware stacker; only curtailment sits above the line).
758-
charge = nothing
759-
charge_cols = [k for k in keys(fuel) if endswith(k, " In")]
760-
if !isempty(charge_cols)
761-
nrows = length(gen.time)
762-
charge = zeros(nrows)
763-
for k in charge_cols
764-
m = Matrix(PA.no_datetime(fuel[k])) # negative (charging)
765-
charge .+= -vec(sum(m; dims = 2)) # -> positive load
766-
end
767-
end
768755
p = _plot_demand!(
769756
p,
770757
result,
@@ -776,7 +763,6 @@ function _plot_fuel!(p, result::IS.Results, backend; kwargs...)
776763
set_display = false,
777764
stack = stack,
778765
seriescolor = ["black"],
779-
extra_load = charge,
780766
kwargs...,
781767
)
782768
end

test/test_plot_creation.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,12 @@ function test_plots(file_path::String; backend_pkg::String = "cairomakie")
243243
nofill = true,
244244
)
245245

246+
# Use a freshly-built system rather than results_uc.system: PSI no longer
247+
# serializes load time series with simulation results, so the system
248+
# attached to simulation results lacks the forecasts that get_load_data needs.
249+
sys_with_ts = PSB.build_system(PSB.PSISystems, "5_bus_hydro_uc_sys")
246250
p = plot_demand_fn(
247-
results_uc.system;
251+
sys_with_ts;
248252
set_display = set_display,
249253
title = "sysdemand",
250254
save = out_path,
@@ -254,7 +258,7 @@ function test_plots(file_path::String; backend_pkg::String = "cairomakie")
254258
@test plot_length == 1
255259

256260
p = plot_demand_fn(
257-
results_uc.system;
261+
sys_with_ts;
258262
set_display = set_display,
259263
title = "sysdemand_bus",
260264
save = out_path,

0 commit comments

Comments
 (0)