Plotting the same plot twice as subplots - julia

I want to create a plot with a couple of subplots using Plots.jl. Here's an example:
using Plots
gr()
p = plot(1:10, 1:10)
q = plot(1:10, 10:-1:1)
plot(p, q)
That works exactly as expected! But say I want to use the same plot twice like so:
plot(p, p)
Hmmm. Only one plot. Perhaps I need to copy the plot first:
plot(p, copy(p))
but that give an error:
ERROR: MethodError: no method matching
copy(::Plots.Plot{Plots.GRBackend})
Closest candidates are:
copy(::Expr) at expr.jl:36
copy(::Core.CodeInfo) at expr.jl:64
copy(::BitSet) at bitset.jl:46
...
Stacktrace:
1 top-level scope at REPL[216]:1
How can I plot the same subplot twice?

You have the right idea, but try deepcopy() instead of copy(). deepcopy() often works on arbitrary objects which have no specific copy() method.

Related

Julia - Displaying several plots in the same plot (not subplot)

Plotting several series in a same plot display is possible and also several subplots in a display. But I want several plots which can be completely different things (not necessarily a series or graph of a map) to be displayed exactly in one frame. How can I do that? In Maple you assign names for each plot like
P1:=...:, P2:= ...: and then using plots:-display(P1,P2,...); and it works. But I want to do this in Julia. Let's say I have the following plots as an example;
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
plot(x,y)
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :yellow))
Now how to have both P1 and P2 in one plot? I don't one a shortcut or trick to write the output of this specific example with one plot line, note that my question is general, for example p2 can be a curve or something else, or I may have a forflow which generates a plot in each step and then I want to put all those shapes in one plot display at the end of the for loop.
Code for a simple example of trying to use plot!() for adding to a plot with arbitrary order.
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot!(x2,y2,fill=(0, :orange))
p3=plot(x,y)
display(p2)
p5=plot!([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green))
By running the above code I see the following plots respectively.
But what I expected to see is a plot with the green rectangle added inside the plot with the two orange rectangles.
The way to plot several series within the same set of axes is with the plot! function. Note the exclamation mark! It's part of the function name. While plot creates a new plot each time it is invoked, plot! will add the series to the current plot. Example:
plot(x, y)
plot!(x, z)
And if you are creating several plots at once, you can name them and refer to them in plot!:
p1 = plot(x, y)
plot!(p1, x, z)
Well, if you do that, what you will have is subplots, technically. That's what it means.
The syntax is
plot(p1, p2)
Sorry, I don't know how to plot a whole plot (conversely to a series) over an other plot.. For what it concerns the order of the plots, you can create as many plots as you want without display them and then display them wherever you want, e.g.:
using Plots
pyplot()
# Here we create independent plots, without displaying them:
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange));
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :orange));
p3=plot(x,y);
p5=plot([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green));
# Here we display the plots (in the order we want):
println("P2:")
display(p2)
println("P3:")
display(p3)
println("P5:")
display(p5)
println("P1:")
display(p1)

is there any facility to plot a line?

I have some nodes and I want to plot them. some of them connect to others. I don't know How can I plot a line in Julia ? would you please help me?
for example a line as follow:
y=2x+5
thank you
As an addition to the answer above, actually in Plots.jl it is even simpler. Just pass a function to plot like this:
plot(x->2x+5)
you typically will want to pass axis ranges which you can do like this:
plot(x->2x+5, xlim=(0,5), ylim=(5,15))
you can also plot several functions at once:
plot([sin, cos, x->x^2-1], label=["sin", "cos", "x²-1"], xlim=(-2,2), ylim=(-1,3))
The result of the last plotting command is:
Try looking at Julia's documentation about plotting as well as this tutorial found by searching Julia plots on a web search engine.
Plotting y = 2x+5 in Julia v1.1 :
using Pkg
Pkg.add("Plots") # Comment if you already added package Plots
using Plots
plotly() # Choose the Plotly.jl backend for web interactivity
x = -5:5 # Change for different xaxis range, you can for instance use : x = -5:10:5
y = [2*i + 5 for i in x]
plot(x, y, title="My Plot")

R, suppress plot from curve function

when using the "curve" function in R, how do you suppress/stop the plot from showing up? For example, this code always plots the curve
my_curve = curve(x)
Is there a parameter to do this or should I being using a different function? I just want the x y points as a dataframe from the curve.
curve() is from the graphics library and is unhandy for generating lists.
Just try using:
x = seq(from, to, length.out = n)
y = function(x)
If you stick to the curve function, the closest to a solution I know is adding dev.off() after the curve() statement!
Here's a way to take advantage of the part of curve that you want without generating a plot.
I made a copy of the curve function (just type curve in the console); called it by a new name (curve2); and commented out the four lines at the end starting with if (isTRUE(add)). When it's called and assigned, I had a list with two vectors—x and y. No plot.

How to change default plot type from points to line in R?

I am working with timeseries with millions of points. I normally plot this data with
plot(x,type='l')
Things slow down terribly if I accidentally type
plot(x)
because the default is type='p'
Is there any way using setHook() or something else to modify the default plot(type=...) during an R session?
I see from How to set a color by default in R for all plot.default, plot or lines calls that this can be done for par() parameters like 'col'. But there doesn't appear to be any points-vs-line setting in par().
A lightweight solution is to just define a wrapper function that calls plot() with type="l" and any other arguments you've given it. This approach has some possible advantages over changing an existing function's defaults, a few of them mentioned here
lplot <- function(...) plot(..., type="l")
x <- rnorm(9)
par(mfcol=c(1,2))
plot(x, col="red", main="plot(x)")
lplot(x, col="red", main="lplot(x)")

Write using mouse on R plot?

I created scattergram using the plot() function in R.
Is there any possibility to draw on this graph?
I would like to add a straight line and get parameters of it, but in my opinion abline() can be inconvenient (I would like to draw many lines and choose one which will be most proper).
How can I accomplish this task?
Take a look at RStudio and this example:
library(manipulate)
data = matrix(rnorm(20), ncol = 2)
example <- function(data, a, b){
plot(data[,1],data[,2])
abline(a = a, b = b)
}
manipulate(
example(data, a, b),
a = slider(-5,5),
b = slider(-5,5)
)
This will put a new line on the plot, and allow you to tweak its slope and intercept.
This was inspired by the example on this page: http://support.rstudio.org/help/discussions/questions/106-rstudio-manipulate-command
Note that this requires installing RStudio (it ships with the manipulate package, I believe). For more info, see the site.
Others' solutions with locator can be done in base R.
Use locator(), a function that allows you to get the coordinates of the mouse pointer when clicking on a plot. Then use
plot(cars)
xy <- locator(n=2)
lines(xy, col="red", lwd=5)
lm(y~x, xy)
abline(coef(lm(y~x, xy)))
coef(lm(y~x, xy))
(Intercept) x
33.142094 1.529687
Of course the correct way of fitting lines through data is to use a proper model. Here is how you can do it with lm:
abline(coef(lm(dist~speed, cars)), col="blue")
I made the following graph with this code:
The thick red line is the line connecting my two mouse clicks
The black line is the abline through these points
The blue line is the line of best fit produced by lm
Warning 1: locator only works on some graphics devices. See ?locator for more details.
Warning 2: Drawing lines of fit by hand could well be a really stupid idea. Use a regression function like lm or a smoothing function like loess instead.
If you were hoping to add horizontal or vertical lines to your plot interactively, you may want to use the locator() function to capture the position of a mouse click on the plot.
For example, the following code would allow the repeated addition of vertical lines to an existing plot:
repeat {
click.loc <- locator(1)
if(!is.null(click.loc)) abline(v=click.loc$x)
else break
}
You could adapt this for horizontal lines with abline(h=click.loc$y)

Resources