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

Added arbitrary legend_columns for gr #4645

Merged
merged 3 commits into from
Feb 1, 2023

Conversation

briederer
Copy link
Contributor

Since I modified the code for myself I thought it would be nice to also upload so others can use it.
Especially since it is asked quite often e.g. on Discourse and #2423.
For now it is just a quick hacky solution but it works with legend_title and without. Also it differs between legend_column=-1 as a full horizontal version and legend_column=nseries where only the labels are printed horizontally but the legend_title is printed center-aligned above the series.

So maybe even if it is not merged it may help some people or be useful when switching to Plots 2.0

The following code produces the output and the figure below

data = rand(10, 3)
leg_cols = -1:4

leg_plots(; kw...) = begin
  map((leg_col -> begin
      plot(data; marker=:circle, ticks=:none, plot_title=string("legend_columns = ", leg_col), legend_columns=leg_col, kw...)
    end), leg_cols)
end
(w, h) = Plots._plot_defaults[:size]
with(scalefonts=1, size=(w, h)) do
  plot(leg_plots()..., leg_plots(legend_title="Test")...; layout=(4, 3))
end
┌ Warning: n° of legend_column=0. Assuming vertical layout.
└ @ Plots ~/.julia/dev/Plots/src/backends/gr.jl:1223
┌ Warning: n° of legend_column=4 is larger than n° of series=3
└ @ Plots ~/.julia/dev/Plots/src/backends/gr.jl:1220
┌ Warning: n° of legend_column=0. Assuming vertical layout.
└ @ Plots ~/.julia/dev/Plots/src/backends/gr.jl:1223
┌ Warning: n° of legend_column=4 is larger than n° of series=3
└ @ Plots ~/.julia/dev/Plots/src/backends/gr.jl:1220

multi_colums_gr

@codecov
Copy link

codecov bot commented Jan 31, 2023

Codecov Report

Base: 90.80% // Head: 90.79% // Decreases project coverage by -0.02% ⚠️

Coverage data is based on head (2d9ba02) compared to base (843b645).
Patch coverage: 85.71% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4645      +/-   ##
==========================================
- Coverage   90.80%   90.79%   -0.02%     
==========================================
  Files          41       41              
  Lines        8781     8795      +14     
==========================================
+ Hits         7974     7985      +11     
- Misses        807      810       +3     
Impacted Files Coverage Δ
src/backends/gr.jl 91.50% <85.71%> (-0.16%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@BeastyBlacksmith BeastyBlacksmith merged commit 3e40c5d into JuliaPlots:master Feb 1, 2023
@BeastyBlacksmith
Copy link
Member

Thanks!

@lmanzanillas
Copy link

lmanzanillas commented Mar 3, 2023

Hello,
I'm using: Plots v1.38.6, PythonPlot v1.0.2
With:
using Plots; pythonplot()
then trying to plot 4 histograms:

plot(legend=:topleft,legend_column=2)

plot!(h_depE_0d,label="polarization hor (0d)",st=:step,yaxis=:log,xlabel="Energy [keV]",framestyle = :box)

plot!(h_depE_45d,label="polarization 45 degrees",st=:step,yaxis=:log,xlabel="Energy [keV]",framestyle = :box)

plot!(h_depE_135d,label="polarization 135 degrees",st=:step,yaxis=:log,xlabel="Energy [keV]",framestyle = :box)

plot!(h_depE_90d,label="polarization ver (90d)",st=:step,yaxis=:log,xlabel="Energy [keV]",framestyle = :box)

it seems it's not working. I still get the message:
Warning: n° of legend_column=2 is not compatible with n° of series=4
image

When trying to plot 4 histograms. It's working when using gr()
image

And it seems something weird happen with the y axis when using log scale...

Any idea how to fix this?

@briederer
Copy link
Contributor Author

Hey. This PR has implemnted the feature only for the gr backend. In all other backends only legend_columns=-1 or legend_columns=nseries for horizontal legends, and legend_columns=1 for the standard vertical layout, are supported.
So I think this would be a feature request to implement this also for other backends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants