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

Adding arbitrary legend_columns for python-backends #4678

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ const _gr_attr = merge_with_base_supported([
:titlefontvalign,
:titlefontrotation,
:titlefontcolor,
:legend_column,
:legend_font_family,
:legend_font_pointsize,
:legend_font_halign,
Expand Down Expand Up @@ -869,6 +870,7 @@ const _pyplot_attr = merge_with_base_supported([
:titlefontfamily,
:titlefontsize,
:titlefontcolor,
:legend_column,
:legend_font_family,
:legend_font_pointsize,
:legend_font_color,
Expand Down
12 changes: 7 additions & 5 deletions src/backends/deprecated/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1527,14 +1527,16 @@ function py_add_legend(plt::Plot, sp::Subplot, ax)
# if anything was added, call ax.legend and set the colors
if !isempty(handles)
leg = legend_angle(leg)
ncol = if (lc = sp[:legend_column]) < 0
ncol = if (lc = sp[:legend_column]) == -1
nseries
elseif lc > 1
lc == nseries ||
@warn "n° of legend_column=$lc is not compatible with n° of series=$nseries"
elseif lc > nseries
@warn "n° of legend_column=$lc is larger than n° of series=$nseries"
nseries
else
elseif lc == 0 || lc < -1
@warn "n° of legend_column=$lc has undefined behaviour. Assuming vertical layout."
1
else
lc
end
leg = ax."legend"(
handles,
Expand Down
10 changes: 6 additions & 4 deletions src/backends/gr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,12 @@ function gr_add_legend(sp, leg, viewport_area)
if vertical
ypos -= leg.dy
else
# println(string(series[:label]), " ", nentry, " ", nentry % legend_cols)
# row-major sorting
xpos += nentry % legend_cols == 0 ? -(legend_cols - 1) * leg.dx : leg.dx
ypos -= nentry % legend_cols == 0 ? leg.dy : 0
# column-major sorting
# xpos += nentry % legend_rows == 0 ? leg.dx : 0
# ypos -= nentry % legend_rows == 0 ? -(legend_rows - 1) * leg.dy : leg.dy
nentry += 1
end
end
Expand Down Expand Up @@ -1222,14 +1225,13 @@ function gr_get_legend_geometry(vp, sp)
elseif legend_column > nseries && nseries != 0 # catch plot_title here
@warn "n° of legend_column=$legend_column is larger than n° of series=$nseries"
(1 + has_title, nseries)
elseif legend_column == 0
@warn "n° of legend_column=$legend_column. Assuming vertical layout."
elseif legend_column == 0 || legend_column < -1
@warn "n° of legend_column=$legend_column has undefined behaviour. Assuming vertical layout."
vertical = true
(has_title + nseries, 1)
else
(ceil(Int64, nseries / legend_column) + has_title, legend_column)
end
#println(column_layout)

base_factor = width(vp) / 45 # determines legend box base width (arbitrarily based on `width`)

Expand Down
17 changes: 11 additions & 6 deletions src/backends/pythonplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1426,14 +1426,19 @@ function _py_add_legend(plt::Plot, sp::Subplot, ax)
isempty(handles) && return

leg = legend_angle(leg)
ncol = if (lc = sp[:legend_column]) < 0
ncol = if (lc = sp[:legend_column]) == -1
nseries
elseif lc > 1
lc == nseries ||
@warn "n° of legend_column=$lc is not compatible with n° of series=$nseries"
elseif lc > nseries
#lc == nseries ||
# @warn "n° of legend_column=$lc is not compatible with n° of series=$nseries"
#nseries
@warn "n° of legend_column=$lc is larger than n° of series=$nseries"
nseries
else
elseif lc == 0 || lc < -1
@warn "n° of legend_column=$lc has undefined behaviour. Assuming vertical layout."
1
else
lc
end
leg = ax.legend(
handles,
Expand All @@ -1447,7 +1452,7 @@ function _py_add_legend(plt::Plot, sp::Subplot, ax)
framealpha = alpha(plot_color(sp[:legend_background_color])),
fancybox = false, # makes the legend box square
# borderpad = 0.8, # to match GR legendbox
ncol,
ncols = ncol,
)
leg.get_frame().set_linewidth(_py_thickness_scale(plt, 1))
leg.set_zorder(1_000)
Expand Down