Help with using the Plots package

Hello
How to plot a two-dimensional graph?
In this example I am trying to plot (x, y) and in response Julia returns a three-dimensional graph.

using Plots
x = 1:4
y = [0.16, 12, 8, 0.13]
y1 = [0.07, 13, 7]
y2 = y,y1
plot!(x,y2)

Hm: you make y2 into a tuple consisting of y and y1, and plot x vs. (y, y1).

Could you post your output from a fresh REPL session? Using your code I get:

image

I.e. the second element of your y2 tuple is ignored. Could you also explain what you expect to happen - why do you construct the tuple in the first place?

I just stopped and started Julia again, and he returned exactly what you put here.
I would like to plot two lines that would be y and y1

The problem is your y1 has 3 elements, while x and y have four elements. If I add an element to y1, I get:

x = 1:4
y = [0.16, 12, 8, 0.13]
y1 = [0.07, 13, 7,2]
y2 = [y, y1]
plot(x,y2)

4 Likes

… if you only have 3 elements for y1, do as follows:

y1 = [0.07, 13, 7, NaN]

and then nothing will be plotted for the last point.

1 Like

Thanks!!!

You could also do it as follows:

plot(x,y)
plot!(x,y1)

Others have answered your question pretty well, but just wanted to give my 2 cents.

I like this syntax when making plots:

p = plot() #You can insert all attributes you want here like "xlabel","size","title" etc.
plot!(x,y,label="first line")
plot!(x1,y1,label="second line")

This allows me to get a plot element “p” and add plots to these as I please, while only having to specify most attirbutes once. If for example you someday want to make two different plots in a for loop you can do:

p1 = plot()
p2 = plot()
plot!(p1,x,y) #Add this plot only to p1
plot!(p2,x1,y1) #Add this plot only to p2

Just wanted to share :slight_smile:

Kind regards

4 Likes

I know that this example is not compilable, but the idea was to put this example inside a loop. I tried to follow the example above but I couldn’t.

x = 1:4
y1 = getvalue(r[1,:]+ o[1,:] + s[1,:])
y2 = getvalue(r[2,:]+ o[2,:] + s[2,:])
y3 = getvalue(r[3,:]+ o[3,:] + s[3,:])
y4 = getvalue(r[4,:]+ o[4,:] + s[4,:])
y=[y1,y2,y3,y4]
plot(x,y)
using Plots
r=rand(4,4)
o=rand(4,4)
s=rand(4,4)
x=1:4
getvalue(u) = u
y=[]
for i=1:4
  push!(y,getvalue(r[i,:]+ o[i,:] + s[i,:]))
end
plot(x,y)

image

2 Likes

Minor alternative to Jeff’s solution:

using Plots
r=rand(4,4)
o=rand(4,4)
s=rand(4,4)
x=1:4
#
y = [r[i,:]+o[i,:]+s[i,:] for i in 1:size(r,1)]
plot()
for yi in y
    plot!(x,yi)
end
plot!()

I like the improved generality, but shouldn’t it be size(r,1) since i is used as the first index of r?

Ooops… of course. I’ll fix it.

@BLI, it seems enough to do in this case:

y = r + o + s
plot(x,y')
1 Like

I included the trivial getvalue as a placeholder for OP’s example.

1 Like

True, and simpler.

However, if one saves the data in other data structures, e.g., a tuple – z = Tuple(y), then

plot(x,z)

doesn’t work, while iterating over z works:

plot()
for zi in z
    plot!(x,zi)
end
plot!()

Although this is elegant and noise-free, I think that OP’s coding skills are at a level that terse solutions like this won’t be much help to her.

1 Like

Then do:

z = Tuple(y)
plot(x, [z...])
1 Like

Yet another case: if one has not decided whether to plot r+o+s row-wise or column-wise:

Y = r+o+s
#
plot()
for i in 1:size(Y,1)
    plot!(x,Y[i,:])
end
plot!()

vs.

plot()
for i in 1:size(Y,2)
    plot!(x,Y[:,i])
end
plot!()