Julia plotting unknown number of layers in Gadfly

arrays, gadfly, julia, plot

Solution

Apparently "splatting", if that's the correct term, works here. Try:

p=Gadfly.plot([layer(y=sim1.value[:,1,i],x=[sim1.range], Geom.line, Theme(default_color=color("red"))) for i in 1:size(sim1)[3]]...)

For different layer colors, this is just a guess/hack (feel free to edit for correctness).

p=Gadfly.plot([layer(y=sim1.value[:,1,i],x=[sim1.range], Geom.line, Theme(default_color=color(["red" "blue" "green" "cyan" "magenta" "yellow"][i%6+1]))) for i in 1:size(sim1)[3]]...)

Perhaps one of Gadfly's `Scale` color parameters would help here.

Addendum:

See first comment below for color selection method.

Problem

I'm trying to create a plot in Julia (currently using Gadfly but I'd be willing to use a different package). I have a multidimensional array. For a fixed dimension size (e.g. 4875x3x3 an appropriate plot would be: ``` p=Gadfly.plot( layer(y=sim1.value[:,1,1],x=[sim1.range],Geom.line, Theme(default_color=color("red"))), layer(y=sim1.value[:,1,2],x=[sim1.range],Geom.line, Theme(default_color=color("blue"))), layer(y=sim1.value[:,1,3],x=[sim1.range],Geom.line, Theme(default_color=color("green"))) ) ``` but in general I want to be able to write a plot statement where I do not know the third dimension of the sim1.value array. How can I write such a statement? perhaps something like: ``` p=Gadfly.plot([layer(y=sim1.value[:,1,i],x=[sim1.range], Geom.line, Theme(default_color=color("red"))) for i in 1:size(sim1)[3]]) ``` but this doesn't work. I was able to solve this problem by reshaping the array into a dataframe and adding a column to indicate what the third dimension is, but I was wondering if there was a way to do this without creating a dataframe. Data look something like this: ``` julia> sim1.value 4875x3x3 Array{Float64,3}: [:, :, 1] = 0.201974 0.881742 0.497407 0.0751914 0.921308 0.732588 -0.109084 1.06304 1.15962 -0.0149133 0.896267 1.22897 0.717094 0.72558 0.456043 0.971697 0.792255 0.40328 0.971697 0.792255 0.227884 -0.600564 1.23815 0.499631 -0.881391 1.07994 0.59905 -0.530923 1.00278 0.447363 ⋮ 0.866138 0.657875 0.280823 1.00881 0.594015 0.894645 0.470741 0.859117 1.09108 0.919887 0.540488 1.01126 2.22095 0.194968 0.954895 2.5013 0.202698 2.05665 1.94958 0.257192 2.01836 2.24015 0.209885 1.67657 0.76246 0.739945 2.2389 0.673887 0.640661 2.15134 [:, :, 2] = 1.28742 0.760712 1.61112 2.21436 0.229947 1.87528 -1.66456 1.46374 1.94794 -2.4864 1.84093 2.34668 -2.79278 1.61191 2.22896 -1.46289 1.21712 1.96906 -0.580682 1.3222 1.45223 0.17112 1.20572 0.74517 0.734113 0.629927 1.43462 1.29676 0.266065 1.52497 ⋮ 1.2871 0.595874 0.195617 1.84438 0.383567 1.15537 2.12446 0.520074 0.957211 2.36307 0.222486 0.402168 2.43727 0.19843 0.636037 2.33525 0.302378 0.811371 1.09497 0.605816 0.297978 1.366 0.56246 0.343701 1.366 0.56246 0.219561 1.35889 0.630971 0.281955 [:, :, 3] = 0.649675 0.899028 0.628103 0.718837 0.665043 0.153844 0.914646 0.807048 0.207743 0.612839 0.790611 0.293676 0.759457 0.758115 0.280334 0.77993 0.774677 0.396879 -1.63825 1.38275 0.85772 -1.43517 1.45871 0.835853 -1.15413 1.35757 1.05071 -1.10967 1.37525 0.685986 ⋮ 1.15299 0.561492 0.680718 1.14853 0.629728 0.294947 1.65147 0.517422 0.22285 1.65147 0.517422 0.517451 1.78835 0.719658 0.745866 2.36554 0.426616 1.49432 0.855502 0.739237 1.24224 -0.175234 0.701025 1.07798 -0.221313 0.939255 1.3463 1.58094 0.368615 1.63817 ```

Original source

Related problems