Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix grouping of zero-height bar in barplot #3058

Merged
merged 11 commits into from
Oct 12, 2023
6 changes: 5 additions & 1 deletion src/basic_recipes/barplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ function stack_grouped_from_to(i_stack, y, grp)
from = Array{Float64}(undef, length(y))
to = Array{Float64}(undef, length(y))

groupby = StructArray((; grp..., is_pos = y .> 0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell just changing this to .>= works too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid not. Given two stacked bars of heights [-6, 0], the second bar would stacked on the first one as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. You do remind me that we should determine the is_pos only based other bars inside of the same group, instead of globally. I will append a commit later. Thanks.

Copy link
Contributor Author

@ctarn ctarn Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed update. I have been too busy recently :(. A new commit has been added. You can test it using:

import Makie, CairoMakie

fig = Makie.Figure()

ax = Makie.Axis(fig[1, 1])
xs = [1, 1, 1, 1]
ys = [0, 1, -1, -1]
stack = [1, 2, 1, 2]
dodge = [1, 1, 2, 2]
color = Makie.cgrad(:Set2_8)[1:4]
Makie.barplot!(ax, xs, ys; stack, dodge, color, bar_labels=string.(ys))

display(fig)

last_pos = any(y .> 0) || all(y .== 0)
is_pos = map(y) do v
last_pos = iszero(v) ? last_pos : v > 0
end
groupby = StructArray((; grp..., is_pos))

grps = StructArrays.finduniquesorted(groupby)

Expand Down