# Plots is not plotting correctly

**URL:** https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477
**Category:** General Usage
**Tags:** package, plots, pluto
**Created:** [February 11, 2023, 7:03pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477 "2023-02-11T19:03:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmj/32/46304_2.png) [@mmj](https://discourse.julialang.org/u/mmj)
#### Post date: [February 11, 2023, 7:03pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/1 "2023-02-11T19:03:37Z")

</div>

Hello.  
This is a simple problem, but I have not found any solutions on it yet. I am very new to Plots.  
I am using Plots on a Pluto Notebook but I have been facing certain difficulty with its syntax. I tried to do a simple interactive code to exemplify vector addition, and so, I did this:

 ![erro Julia plot vetor](https://global.discourse-cdn.com/julialang/original/3X/b/5/b59d8dfdae6f041afe878e24dc7363f35e0645f2.png)  
As you can see, the blue vector (which is the result of the sum) is correctly plotted, but the black vectors don’t appear. Any idea as to why this happens?  
Thank you.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [February 11, 2023, 7:22pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/2 "2023-02-11T19:22:17Z")

</div>

You need to use the in-place plot command (has a `!` on the end of the function name). By default `plot` creates a new plot.

```julia
begin
plt=plot(line1, ...)
plot!(plt, line2, ...)
#...etc
return plt
end

```

Filling in the parts you want for style etc.

---

<div class="post-metadata">

### Author: ![mmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmj/32/46304_2.png) [@mmj](https://discourse.julialang.org/u/mmj)
#### Post date: [February 11, 2023, 8:05pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/3 "2023-02-11T20:05:11Z")

</div>

Thank you for your answer. I had tried to do this, but got a very strange result. I believe this has something to do with the fact that I have different plots on the same notebook. Take a look:

 ![segundo erro Julia plot vetor](https://global.discourse-cdn.com/julialang/original/3X/4/4/44caf9c8e41ec37ecb78157fa053f471690cc0a8.png)  
As you can see, the code only includes three plots, but two other plots (that should belong in another graph) are present in the graph. I don’t understand why this happens, because the code doesn’t even include the variables refering to the vectors in another plot.  
Where can I find the syntax for the plot function? I couldn’t find it.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [February 11, 2023, 8:07pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/4 "2023-02-11T20:07:53Z")

</div>

Yes, you should make the first plot command in your `begin... end` block the usual `plot` and only the subsequent ones as `plot!`.

`plot!` adds to the currently open plot so this behaviour is expected. Its why we use `plot` for the first item as it creates a new figure.

---

<div class="post-metadata">

### Author: ![mmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmj/32/46304_2.png) [@mmj](https://discourse.julialang.org/u/mmj)
#### Post date: [February 11, 2023, 10:44pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/6 "2023-02-11T22:44:32Z")

</div>

I didn’t know that. I am just confused as to what the syntax of the plot function should be… You said that “plot” by default creates a new plot. Since the syntax of the “plot!” requires the coordinates for the differente vectors. What am I supposed to insert in “plot” in a way that can be recalled by “plot!”?  
Also, how can I use this function with a scatter plot? I noticed that some of the vector plots I executed had the scatter points I executed in a previous plot.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [February 11, 2023, 10:59pm UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/7 "2023-02-11T22:59:19Z")

</div>

You can create a new plot and add each series one by one like this:

```julia
plt=plot()
plot!(plt, [0,0], [1,1]) #for a line
scatter!(plt, [0.5,1.0], [2.0, 1.0]) # for points
#.... Etc

```

The `!` versions of the function plot can optionally take a reference to a specific plot as the first argument. All the other arguments are the same as the non `!` versions. If you provide a reference to a plot as the first argument (as in the example above) it will add the series (line/points etc) to that existing plot. If you do not provide a reference, it will use the currently active plot by default.

There is nothing special about the `!` on the function name, this is just a convention in Julia to tell the caller of the function that this function will modify something in place, usually the first argument.

Remember that `plot` and `plot!` are different functions but take the exact same arguments, except for `plot!` optionally taking in a reference to a figure (returned by a previous `plot` command) as the first argument. This is the same for `scatter` and `scatter!` and most other functions with plot.

If have trouble with the plot not showing, just type the reference to the figure on the last line of the cell, or call `display(plt)` somewhere in your code after it is plotted.

---

<div class="post-metadata">

### Author: ![mmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmj/32/46304_2.png) [@mmj](https://discourse.julialang.org/u/mmj)
#### Post date: [February 12, 2023, 12:47am UTC](https://discourse.julialang.org/t/plots-is-not-plotting-correctly/94477/8 "2023-02-12T00:47:34Z")

</div>

I understand now! Its working now. Thank you for your help. Many blessings.
