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

[feature request] support multi-column y with automatic legend #302

Closed
daviehh opened this issue Feb 26, 2019 · 8 comments
Closed

[feature request] support multi-column y with automatic legend #302

daviehh opened this issue Feb 26, 2019 · 8 comments
Labels
conversions Mainly `convert_arguments` enhancement Feature requests and enhancements Makie Backend independent issues (Makie core)

Comments

@daviehh
Copy link
Contributor

daviehh commented Feb 26, 2019

e.g. when x of size n and one has calculated/read in y as a n*m matrix; Plots.jl allows for something like

using Plots
x = 1:10; y = rand(10,2)
plot(x,y)

to plot the columns of y against x, then label them as y1...yn in the legend; I can use a loop to plot lines!(x,y[:,i]) manually but something like lines(x,y) where y can be a multi-column matrix can be helpful/make the code look cleaner. Thanks!

@mkborregaard
Copy link
Contributor

sorry, I have no idea what you are requesting

@daviehh
Copy link
Contributor Author

daviehh commented Feb 26, 2019

@mkborregaard It's quite common when the plot data is a matrix, each column of which corresponding to a line to be plotted. For example, using Plots:

using Plots
xspan = 0:.01:2*pi
fs = [sin, cos, sinc]
y = [fs[i](x) for x in xspan, i in 1:size(fs,1)]
plot(xspan,y)

plots all columns of y against xspan, automatically using different colors and plot labels

screen shot 2019-02-26 at 12 19 20 pm

in Makie, to achieve the same thing, there needs to be additional codes for color, and a loop to go through all columns:

using Makie

fs = [sin, cos, sinc]
colors = [:red, :green, :blue]

xspan = 0:.01:2*pi
y = [fs[i](x) for x in xspan, i in 1:size(fs,1)]

scene = Scene()

for i in 1:size(fs,1)
    lines!(scene,xspan,y[:,i],color=colors[i])
end

scene

screen shot 2019-02-26 at 12 24 42 pm

I'm requesting something like lines(xspan,y) to iterate through all columns of y, with default different colors for each line, and optional automatic/user-provided legends.
This is somewhat related to #8: many of the julia tutorials make examples using Plots and it would be nice for makie to be a drop-in replacement for plots.

Thanks!

@asinghvi17
Copy link
Member

Could you use convert_arguments? See #307 (comment) for now, but we should have the docs up as soon as JuliaPlots/MakieGallery.jl#13 is merged.

@daviehh
Copy link
Contributor Author

daviehh commented Mar 4, 2019

@asinghvi17 thanks! can you give a more concrete example, e.g. an example for the figures above? not sure how to use convert_arguments to do the loop,

using Makie

fs = [sin, cos, sinc]
colors = [:red, :green, :blue]

xspan = 0:.01:2*pi
y = [fs[i](x) for x in xspan, i in 1:size(fs,1)]

scene = Scene()

for i in 1:size(fs,1)
    lines!(scene,xspan,y[:,i],color=colors[i])
end

scene

I think a full recipe is needed, however, the documentation does not include a concrete example, and code below fails:

@recipe(MyPlot2d, x, y)do scene
    Theme(
    )
    end

xy_type = Tuple{<:AbstractRange,<:AbstractArray{<: AbstractFloat, 2}}
xy_data = MyPlot2d{xy_type}

function plot!(plot::xy_data)
    # normal plotting code, building on any previously defined recipes
    # or atomic plotting operations, and adding to the combined `plot`:
    x = plot_object[:arg1]
    y = plot_object[:arg2]
    for i in 1:size(y,2)
        lines!(plot,x,y[:,i])
    end
    plot
end


myplot2d(xspan,y)

returns

Plotting for the arguments (::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Array{Float64,2}) not defined for myplot2d. If you want to support those arguments, overload plot!(plot::myplot2d{ <: Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Array{Float64,2}}})

whereas typeof((xspan,y)) <: xy_type is true.

@asinghvi17
Copy link
Member

You're right that it needs a recipe, I think I was thinking of the old transform_arguments (which no longer exists) when I said that. Sorry!

I'm not sure why exactly the recipe is failing, but I'll definitely look into it tomorrow.

@daviehh
Copy link
Contributor Author

daviehh commented Mar 4, 2019

Right now, looks like it'll be simpler to define a new function (although it feels like the "makie" way is to handle it through recipes and not by defining/overloading functions), e.g.

using Makie
using AbstractPlotting
using Colors

fs = [sin, cos, sinc]
xspan = 0:.01:2*pi
y = [fs[i](x) for x in xspan, i in 1:size(fs,1)]


function plot2d(x::Union{AbstractRange,AbstractArray{<: AbstractFloat, 1}}, y::AbstractArray{<: AbstractFloat, 2};legend_desc=[], kwargs...)
    scene = Scene()
    ny = size(y,2)
    colors = distinguishable_colors(ny+1,[RGB(1,1,1)])[2:end]
    if size(legend_desc,1) != ny
        legend_desc = ["data $i" for i in 1:ny]
    end
    p = []
    for i in 1:ny
            pli = lines!(scene, x, y[:,i], color = colors[i];kwargs...)
            push!(p,pli[end])
    end
    ls = legend(p, legend_desc, camera = campixel!, raw = true)

    # scene
    vbox(scene, ls)
end

plot2d(xspan, y, linewidth = 2)

# with user-supplied legend string 
plot2d(xspan, y, linewidth = 2, legend_desc = string.(fs))

however, currently the legends are printed backwards, i.e. from the last column of y to the 1st. Maybe fixed when we have better documentation of legend: #308

screen shot 2019-03-04 at 5 28 17 pm

screen shot 2019-03-04 at 5 27 18 pm

daviehh referenced this issue in daviehh/AbstractPlotting.jl Mar 5, 2019
@asinghvi17 asinghvi17 added the enhancement Feature requests and enhancements label Apr 23, 2019
@asinghvi17
Copy link
Member

Perhaps the MultiplePlot feature could be of use here?

@ffreyer ffreyer added Makie Backend independent issues (Makie core) conversions Mainly `convert_arguments` labels Aug 21, 2024
@ffreyer
Copy link
Collaborator

ffreyer commented Aug 21, 2024

This has been implemented as series

@ffreyer ffreyer closed this as completed Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conversions Mainly `convert_arguments` enhancement Feature requests and enhancements Makie Backend independent issues (Makie core)
Projects
None yet
Development

No branches or pull requests

4 participants