# Help with using the Plots package

**URL:** <https://discourse.julialang.org/t/help-with-using-the-plots-package/57036>\
**Category:** Visualization\
**Tags:** question\
**Created:** [March 12, 2021, 6:29pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036 "2021-03-12T18:29:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [March 12, 2021, 6:29pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/1 "2021-03-12T18:29:52Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 12, 2021, 6:35pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/2 "2021-03-12T18:35:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 12, 2021, 6:36pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/3 "2021-03-12T18:36:34Z")

</div>

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

![image](https://global.discourse-cdn.com/julialang/original/3X/7/9/792955861ca0df6894a0ec13eb84d48b886e1b48.png)

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?

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [March 12, 2021, 7:03pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/4 "2021-03-12T19:03:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 12, 2021, 7:04pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/5 "2021-03-12T19:04:51Z")

</div>

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

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2d20342f7792bb610101732f640935aefa7c511.png)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 12, 2021, 7:06pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/6 "2021-03-12T19:06:20Z")

</div>

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

```julia
y1 = [0.07, 13, 7, NaN]

```

and then nothing will be plotted for the last point.

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [March 12, 2021, 7:36pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/7 "2021-03-12T19:36:55Z")

</div>

Thanks!!!

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 12, 2021, 7:50pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/8 "2021-03-12T19:50:34Z")

</div>

You could also do it as follows:

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

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 12, 2021, 9:01pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/9 "2021-03-12T21:01:26Z")

</div>

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

I like this syntax when making plots:

```julia
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:

```julia
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 🙂

Kind regards

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [March 16, 2021, 1:34pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/10 "2021-03-16T13:34:14Z")

</div>

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.

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 16, 2021, 3:04pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/11 "2021-03-16T15:04:13Z")

</div>

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/1/e/1e322c01763190c4a729aa1edee666868b31133c.png)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 16, 2021, 4:55pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/13 "2021-03-16T16:55:17Z")

</div>

Minor alternative to Jeff’s solution:

```julia
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!()

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 16, 2021, 4:58pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/14 "2021-03-16T16:58:49Z")

</div>

> [@BLI](#):
>
> `size(r,2)`

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 16, 2021, 4:59pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/15 "2021-03-16T16:59:24Z")

</div>

Ooops… of course. I’ll fix it.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 16, 2021, 4:59pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/16 "2021-03-16T16:59:28Z")

</div>

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

```julia
y = r + o + s
plot(x,y')

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 16, 2021, 5:00pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/17 "2021-03-16T17:00:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 16, 2021, 5:06pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/18 "2021-03-16T17:06:24Z")

</div>

True, and simpler.

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

```julia
plot(x,z)

```

doesn’t work, while iterating over `z` works:

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

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 16, 2021, 5:11pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/19 "2021-03-16T17:11:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 16, 2021, 5:11pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/20 "2021-03-16T17:11:40Z")

</div>

Then do:

```julia
z = Tuple(y)
plot(x, [z...])

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 16, 2021, 5:24pm UTC](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036/21 "2021-03-16T17:24:37Z")

</div>

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

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

```

vs.

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

```

[Next page](https://discourse.julialang.org/t/help-with-using-the-plots-package/57036.md?page=2)
