Plot line and hatched fill handles on top of each other in py_add_legend

This commit is contained in:
Pearl Li 2021-08-25 12:36:31 -07:00
parent 7f594413af
commit b0cf3ba498

View File

@ -1488,30 +1488,47 @@ function py_add_legend(plt::Plot, sp::Subplot, ax)
if should_add_to_legend(series) if should_add_to_legend(series)
clims = get_clims(sp, series) clims = get_clims(sp, series)
# add a line/marker and a label # add a line/marker and a label
push!(
handles,
if series[:seriestype] == :shape || series[:fillrange] !== nothing if series[:seriestype] == :shape || series[:fillrange] !== nothing
fs = get_fillstyle(series)
has_fs = !isnothing(fs)
lc = get_linecolor(series, clims) lc = get_linecolor(series, clims)
la = get_linealpha(series) la = get_linealpha(series)
ls = get_linestyle(series)
fc = get_fillcolor(series, clims) fc = get_fillcolor(series, clims)
fa = get_fillalpha(series) fa = get_fillalpha(series)
fs = get_fillstyle(series)
has_fs = !isnothing(fs)
pypatches."Patch"( # line (and potentially solid fill)
# hatch color/alpha controlled by edge (not face) color/alpha line_handle = pypatches."Patch"(
# if has_fs, set edge color/alpha <- fill color/alpha and face alpha <- 0 edgecolor = py_color(single_color(lc), la),
edgecolor = has_fs ? py_color(single_color(fc), fa) : py_color(single_color(lc), la),
facecolor = py_color(single_color(fc), has_fs ? 0 : fa), facecolor = py_color(single_color(fc), has_fs ? 0 : fa),
hatch = py_fillstyle(fs),
linewidth = py_thickness_scale(plt, clamp(get_linewidth(series), 0, 5)), linewidth = py_thickness_scale(plt, clamp(get_linewidth(series), 0, 5)),
linestyle = py_linestyle(series[:seriestype], get_linestyle(series)), linestyle = py_linestyle(series[:seriestype], ls),
capstyle = "butt" capstyle = "butt",
) )
# hatched fill
# hatch color/alpha are controlled by edge (not face) color/alpha
if has_fs
fill_handle = pypatches."Patch"(
edgecolor = py_color(single_color(fc), fa),
facecolor = py_color(single_color(fc), 0), # don't fill with solid background
hatch = py_fillstyle(fs),
linewidth = 0, # don't replot shape outline (doesn't affect hatch linewidth)
linestyle = py_linestyle(series[:seriestype], ls),
capstyle = "butt",
)
# plot two handles on top of each other by passing in a tuple
# https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html
push!(handles, (line_handle, fill_handle))
else
# plot line handle (which includes solid fill) only
push!(handles, line_handle)
end
elseif series[:seriestype] in elseif series[:seriestype] in
(:path, :straightline, :scatter, :steppre, :stepmid, :steppost) (:path, :straightline, :scatter, :steppre, :stepmid, :steppost)
hasline = get_linewidth(series) > 0 hasline = get_linewidth(series) > 0
PyPlot.plt."Line2D"( handle = PyPlot.plt."Line2D"(
(0, 1), (0, 1),
(0, 0), (0, 0),
color = py_color( color = py_color(
@ -1543,10 +1560,10 @@ function py_add_legend(plt::Plot, sp::Subplot, ax)
first(series[:markersize]), first(series[:markersize]),
), # retain the markersize/markerstroke ratio from the markers on the plot ), # retain the markersize/markerstroke ratio from the markers on the plot
) )
push!(handles, handle)
else else
series[:serieshandle][1] push!(handles, series[:serieshandle][1])
end, end
)
push!(labels, series[:label]) push!(labels, series[:label])
end end
end end