Displaying plots in julia in notebook (using juliaBox) - plot

I want to display a plot in the julia language (using iJulia).
But is doesn't show the plot.
Here is a minimal working example of what I tried:
using Plots
function testplotting()
x=[1,2,3,4]
y1=[1,2,3,4]
y2=[1,2,3,4]
plt=plot(x,y)
plot!(x,y2)
return plt
end
plt=testplotting()
display(plt)
println("finished")`
But is doesn't show the plot..
Without the line where I add the extra line to plot the other array it works, but I want to plot multiple variables at the same time.
Can anyone explain why it doesn't display or how to fix it?

Try plot!(plt, xlabel = "blabla").
plot! with a single argument implicitly modifies the "current" plot, which it gets by querying the current plot in global scope. Given that you are in a function scope, the global scope has no access to the plot you just created. Thus you need to specify which plot you want to modify.

Related

How to save two overlapped plots together in one single file?

using Plots
plot(0:10,sin);
plot!(0:10,sin,seriestype = :scatter)
In this example, the output are actually two plots. How can I save them in one file?
I searched and tried some method, but they only support one single plot and I haven't found any functions for multiple plots.
This is just one figure - plot! (with a bang) mutates the figure object created in the first plot call.
Saving this is as simple as savefig("my_output.svg") - if this does not work as expected please provide more details.
Not directly related, but seeing that you are overlaying a scatter plot onto a line plot you might be interested in the linetype kwarg:
plot(0:10, sin, linetype = :scatterpath)
plot(0:10, sin, lt = :scatterpath) # short form alias
will both produce a line and scatter at the same time (with a single label and single colour).

plotting polygons inside loop in R, why this ambiguous behavior?

So, I want to plot multiple polygons over one polygon using a loop in r. I have read that you need to use print in order to succesively display the polygons over the original polygon. However, that is not what R seems to do. Besides, in a hardly reproducible example, what i find is that either using print or not, the polygons will not plot within the loop. I am using a mac with OS monterrey, btw.
Here is a reproducible example, using print gives a bunch of NULL messages, but does print, not using print still plots. Yet a more complicated example will never plot from within the loop despite plotting everything when i request line by line from within the loop.
require(maptools)
data(wrld_simpl)
plot(wrld_simpl)
for (i in 1:12){
region=wrld_simpl[wrld_simpl$NAME==wrld_simpl$NAME[i],]
print(plot(region,border="red",add=T))
}
Maybe you're confusing print and plot? The plot function creates (or adds elements to) a plot. Whereas print function prints its arguments to the console. Since plot doesn't return a value, print has nothing to print and hence prints NULL.
Btw. you don't even need a loop. Since the loaded data wrld_simpl is of class SpatialPolygonsDataFrame, the object is subsettable like a dataframe you can just plot the needed indices:
plot(wrld_simpl)
plot(wrld_simpl[1:12,], border="red", add=T)
If you need help on a more complicated example, I suggest you share it?

Multiple axes in Julia Plots (GR backend)

How do I plot a set of curves with Julia (1.4.1) and Plots in a Jupyter notebook, some of them using the x axis and the left y axis, and some (more than one!) using the x axis and the right y axis? Whatever I try, I always get only one plot (the first one, which gets the twinx command) to use the right y axis, whereas the ones following it use the first (left) axis again. Among several other things, I tried
epl=plot(title="A title",legend=:topright)
epl=plot!(x_arr,y1_arr)
epl=plot!(x_arr,y2_arr)
epl=plot!(twinx(),x_arr,y3_arr,legend=:bottomright)
epl=plot!(x_arr,y4_arr)
display((epl))
and also
epl=plot(title="A title",legend=:topright)
epl=plot!(x_arr,[y1_arr,y2_arr])
epl=plot!(twinx(),x_arr,[y3_arr,y4_arr],legend=:bottomright)
display((epl))
The latter works a bit better, but I don't know how to define individual linestyles, legend labels etc. for each vector element; my attempt to assign matching vectors to the corresponding keyword arguments failed. The label for the second y axis (not included in the example above) does not show up either. The legend is wrong in the first case and does not show up at all in the second case.
The interface I use is apparently gr(), which I tend to prefer, but I'm not sure.
The plot! function takes an optional first plot object to modify. If you don't pass in a plot to be modified, it uses the global one.
When you call twinx(), it creates a new subplot that you can plot! into — one that's inside your current global plot — and then it updates and returns the overall plot. Your next plot! just updates the first subplot as usual.
The key is to save your twinned subplot and explicitly plot! into it.
epl=plot(title="A title", legend=:topright)
plot!(x_arr, y1_arr) # could be explicitly `plot!(epl, x_arr, y1_arr)`
plot!(x_arr, y2_arr)
subplot = twinx()
plot!(subplot, x_arr, y3_arr, legend=:bottomright)
plot!(subplot, x_arr, y4_arr)

Remove Legend In R

How I can remove the legend in a plot in R? I tried legend<-FALSE, doesn't work.
Also, is there a better way to set the position of the legend? For example, is there
a way I can pick the location with my cursor? And I am not talking about ggplot or any fancy add-ons, just regular R plotting.
In order:
This is related to what people tried to explain to you yesterday: Think of a script as primary means of creating your R session. In ESS, you get the script as a by-product; in RStudio you can also work with commands first and then pass those to your session. Lastly, no you can't remove a legend which has already been added to a plot, but you can hopefully re-create your graph using the saved commands.
Yes, since Duncan Murdoch added support for 'topleft' etc you can use logical commands:
plot(1:10) # simple plot
legend("bottomright", "foo") # 'foo' in bottom-right corner
Yes, if you use the output of locator() as input for the legend() command.
You need to specify which plotting function is producing the legend. (Most plotting function do not plot legends by default.)
There is a locator function.

Repeat plot command with minor changes in R

I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.
In gnuplot I would use the replot command.
plot ...
set title "The same plot with logarithmic axes"
set logscale
replot
Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.
Thanks for your help,
Sven
R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.
Depending on what you are really doing there are two options:
If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.
Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.

Resources