Plots is not plotting correctly

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:

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.

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.

plt=plot(line1, ...)
plot!(plt, line2, ...)
return plt

Filling in the parts you want for style etc.

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:

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.

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.

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.

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

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.

1 Like

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

1 Like