Ju_ska
1
Hi,
I would like to stack plots on top of each other without having to manage columns.
Like separating the x axis from every other columns, then plot.
using DataFrames
using Plots
df = DataFrame(x = [1,2,3,4], y1 = [8,9,9,8], y2=[3,5,3,3])
Without success, I tried things like:
extract = df[:, filter(x -> x != ["x" ], names(df))]
plot(df.x, eachcol(extract), layout = (2, 1))
Using StatsPlots.jl with DataFrames.jl is a good combo:
using DataFrames, StatsPlots
df = DataFrame(x = [1,2,3,4], y1 = [8,9,9,8], y2=[3,5,3,3])
ys = Symbol.(names(df, Not(:x)))
@df df plot(:x, cols(ys); layout=(2,1))
1 Like
julia> plot(df.x, eachcol(extract), layout = (2, 1))
ERROR: Cannot convert DataFrames.DataFrameColumns{DataFrame} to series data for plotting
try this
extract = df[:, filter(x -> x != "x" , names(df))]
plot(df.x, collect(eachcol(extract)), layout = (2, 1))
or this
plot(df.x, [eachcol(extract)...], layout = (2, 1))
p=plot();
for c in eachcol(extract)
plot!(df.x, c)
end
display(p)
1 Like
We don’t need to extract a new dataframe for this. We can just take the names of the columns (and also use these in the legends):
colnames = filter(!=("x"), names(df))
plot(df.x, getproperty.(Ref(df), colnames), labels=permutedims(colnames), layout=(ncol(df)-1,1))
2 Likes
Just want to point out that names(df) supports a Not argument
julia> using DataFrames
julia> df = DataFrame(x = 1, y = 2, z = 3);
julia> names(df, Not("x"))
2-element Vector{String}:
"y"
"z"
1 Like