JuliaPlots / Plots.jl

Powerful convenience for Julia visualizations and data analysis
https://docs.juliaplots.org
Other
1.82k stars 350 forks source link

[BUG] Wrong marker and line sizes in legend in GR #4621

Open AsgerHB opened 1 year ago

AsgerHB commented 1 year ago

Details

Manually setting marker and line sizes, and then setting plot size, causes the legend to draw marker at the wrong size. In this example, it becomes impossible to see the marker. (I find myself rescaling them manually for every figure I export)

plot([1:10], rand(1:10, 10),
    size=(300, 300),
    markersize=8,
    marker=:star,
    linewidth=5,
    label="y")

image

Backends

This bug occurs on ( insert x below )

Backend yes no untested
gr (default) x
pythonplot x
plotlyjs x
pgfplotsx x
unicodeplots x
inspectdr x
gaston x

Versions

Plots.jl version: 1.38.0 Backend version (]st -m <backend(s)>): GR v0.71.2 Output of versioninfo():


Commit 36034abf260 (2022-09-29 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, tigerlake)
  Threads: 4 on 8 virtual cores
Environment:
  JULIA_REVISE_WORKER_ONLY = 1```
BeastyBlacksmith commented 1 year ago

see the discussion in https://github.com/JuliaPlots/Plots.jl/issues/2655

JeffFessler commented 1 year ago

I find myself rescaling them manually

How do you rescale manually? I am facing the same issue. I am using Julia for the figures in a textbook and I must increase the legendfontsize, but then the legend marker size shrinks to be far too small. It is quite unexpected that legendfontsize would affect the legend marker size.

using Plots
p1 = plot(1:4, marker=:circle, markersize=9)
p2 = plot(1:4, marker=:circle, markersize=9, legendfontsize=16)
Screenshot 2023-06-16 at 8 50 44 AM Screenshot 2023-06-16 at 8 51 00 AM
AsgerHB commented 1 year ago

I export them in SVG, making sure to always use the svg() function for concistency. (They turn out slightly different when output from a Pluto Notebook.) Then I open the file in Inkscape to make my changes. Optionally I use the Inkscape command line to export the finished svgs to other formats.

fre. d. 16. jun. 2023 14.52 skrev Jeff Fessler @.***>:

I find myself rescaling them manually

How do you rescale manually? I am facing the same issue. I am using Julia for the figures in a textbook and I must increase the legendfontsize, but then the legend marker size shrinks to be far too small. It is quite unexpected that legendfontsize would affect the legend marker size.

using Plots p1 = plot(1:4, marker=:circle, markersize=9) p2 = plot(1:4, marker=:circle, markersize=9, legendfontsize=16)

[image: Screenshot 2023-06-16 at 8 50 44 AM] https://user-images.githubusercontent.com/10170422/246421074-7665ebab-5595-4500-ab25-2f376a6239ff.png [image: Screenshot 2023-06-16 at 8 51 00 AM] https://user-images.githubusercontent.com/10170422/246421107-2bacd406-6641-4775-86cf-ec130d9b65d4.png

— Reply to this email directly, view it on GitHub https://github.com/JuliaPlots/Plots.jl/issues/4621#issuecomment-1594629327, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUBQRWT22D265O4W3QHYLLXLRJKDANCNFSM6AAAAAATQ6O6Q4 . You are receiving this because you authored the thread.Message ID: @.***>

JeffFessler commented 1 year ago

I export them in SVG

Thanks for the tip. I am also making many plots using Literate for online documentation, so there is no chance to export/tweak there. Hopefully someday we will get legendmarkersize per #2655!

AsgerHB commented 1 year ago

I saw that the legend items may have some tags or IDs in common that might be bulk-edited by changing the SVG code. I didn't pursue this, though. By making sure I had the right panels open in Inkscape, I could do an edit in less than 5 seconds per figure by opening them all in separate editors and closing them when the edit is done.

fre. d. 16. jun. 2023 16.45 skrev Jeff Fessler @.***>:

I export them in SVG

Thanks for the tip. I am also making many plots using Literate for online documentation, so there is no chance to export/tweak there. Hopefully someday we will get legendmarkersize per #2655 https://github.com/JuliaPlots/Plots.jl/issues/2655!

— Reply to this email directly, view it on GitHub https://github.com/JuliaPlots/Plots.jl/issues/4621#issuecomment-1594814241, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUBQRSYEJU3LETHTLHGJG3XLRWSPANCNFSM6AAAAAATQ6O6Q4 . You are receiving this because you authored the thread.Message ID: @.***>

LRBaalmann commented 6 months ago

A fairly stupid workaround is to define a custom marker that is a scaled version of the desired marker. Custom markers can be made with the Shape() function from a polygon (see https://github.com/JuliaPlots/Plots.jl/issues/45).

MWE for circles:

using Plots
small = Shape([0.5.*i for i in Plots.partialcircle(0, 2π)])
big = Shape([2.0.*i for i in Plots.partialcircle(0, 2π)])
scatter(rand(10), label="normal", size=(400,300))
scatter!(rand(10), label="big", shape=big)
scatter!(rand(10), label="small", shape=small)
savefig("img/mwe-markersize-legend.png")

mwe-markersize-legend

There's probably a more intelligent way to draw a circle than creating a partial circle that is 0% partial, but, hey, it works.

Rescaling only the symbols in the legend but not in the figure would require manually setting the markersize and markerstrokewidth arguments to compensate for the scaled shape, so that's not ideal. It does work as a workaround as well, though.

JeffFessler commented 6 months ago

It is nice to learn about Shape -- thanks! That approach helps somewhat for scatter, though there is still some mismatch between the size in the plot and the legend and legendfontsize still affects the legend marker size. Sadly using Shape still does not help solve my issue for plot where there is dramatic mismatch.

using Plots
shape = Shape([2 .* i for i in Plots.partialcircle(0, 2π)])
data = 1:4
p1 = scatter(data; shape, title="p1") 
p2 = scatter(data; shape, legendfontsize=16, title="p2")
p3 = plot(data; shape, title="p3", color=:red)
p4 = plot(data; shape, legendfontsize=16, title="p4", color=:red)
plot(p1, p2, p3, p4, size=(600,600))
scatter-plot