JuliaPlots / Plots.jl

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

[FR] horizontal ribbons #2702

Open briochemc opened 4 years ago

briochemc commented 4 years ago

While vertical ribbons works very nicely like in

julia> plot(1:10, rand(10), ribbon = (1:10, 1:10))

it would be great to be able to do the same when axes are swapped, maybe with a xribbon like we already have xerror like so:

julia> plot(rand(10), 1:10, xribbon = (1:10, 1:10))

For comparison, it works with xerror:

julia> plot(rand(10), 1:10, xerror = (1:10, 1:10))

produces

issue_plots_hribbon

postylem commented 2 years ago

As a hack, it is possible to get a vertical plot as follows.

Here's a simple x-y plot with a vertical ribbon specified with min and max values for each y value in ribbon:

using Plots
seq = 0:30.0
values = rand(length(seq)) .- 0.5
ribbon = (seq ./ 5, seq ./ 4)
plot_horiz = plot(seq, values, ribbon = ribbon, fillcolor=:lightblue)
Screenshot 2022-11-02 at 14 06 10

A horizontal version (swapping x and y axes) can be made like this (at least with the default GR backend in Julia 1.8.2... haven't tested any other backends):

plot_vert = plot(values, seq)
function make_hribbon_shape(ys, values, ribbon)
    # make the ribbon as a shape to fill in
    rib_min = values .- ribbon[1] # the lower edge of the ribbon
    rib_max = values .+ ribbon[2] # the upper edge of the ribbon
    ys = [ys; [ys[end]; ys[end]]; reverse(ys); [ys[1]; ys[1]]]
    xs = [rib_max; [rib_max[end],rib_min[end]]; reverse(rib_min); [rib_min[1]; rib_max[1]]]
    return xs, ys
end
ribbon_xs, ribbon_ys = make_hribbon_shape(seq, values, ribbon)
plot_vert=plot!(plot_vert,
    ribbon_xs, ribbon_ys, 
    fill=true, linewidth=0, fillalpha = 0.5,  fillcolor=:lightblue,
    label=nothing)
Screenshot 2022-11-02 at 14 06 33

I've been using this method for a bit, and I think I originally got it from here