How to use the wireframe and a scater3d at the same time using GLMakie in Julia? - plot

Ive been working with adjustments and I have coordinates and I need to plot a sphere with wireframe, I need to draw these points and this sphere on the same graph, how to do this using GLMakie in Julia? I can use wireframe and scatter separately, but not both at the same plot.
And also, how can i change the center of the sphere with the command Sphere(Point3(1,5)?
I tried it..
x = [0, 1]
y = [0, 1]
z = [0, 1]
scatter!(x, y, z, markersize=[0.2,0.0])
wireframe!(Sphere(Point3(1),5), limits=FRect3D([-5,-5,-5],[10,10,10]))
but returned: Combined{Makie.wireframe, Tuple{Sphere{Int64}}}

you need to display the figure, the quick and dirty way is to run
current_figure()

Related

How to plot data over a non-rectangular region in Octave?

I have three arrays of equal size: x, y, z. I want to plot z over x, y. Problems is, those x and y do not represent a rectangular region, such as what would be in case of using meshgrid function.
I know I can use something like scatter, but that would graphically only give me the points themselves. What I want is the filled, smoothed picture. So as opposed to this created by scatter:
I would like something like this:
Any suggestion how this can be done? I have a feeling the data must be smoothed out somehow via interpolation or something else prior to plotting which itself should be simple.
You can use griddata() to interpolate your x,y data on a regular grid and then you can use imagesc() to plot the result.
Here is a minimal example with a basic circle:
% INPUT
x = cos(0:0.1:2*pi);
y = sin(0:0.1:2*pi);
z = (0:0.1:2*pi);
% Create a regular grid that have the same boundary as your x,y data
[xx,yy] = meshgrid(linspace(-1,1,100),linspace(-1,1,100));
% Grid interpolation
zz = griddata (x, y, z, xx, yy);
% Plot
imagesc(zz)
colormap ([jet(); 1 1 1]); % I add a last [1 1 1] triplet to set the NaN color to white.
Noticed that this will only works if you keep the default interpolation method (which is a linear interpolation). The other method (cubic and nearest) will extend the domain of definition by analytic continuation.
I realized that the best approach would be some slight modification to what obchardon is proposing:
instead of the lines
imagesc(zz)
colormap ([jet(); 1 1 1]);
do this:
surf(xx, yy, zz);
shading interp;
colormap("jet");
This eliminates the problem with the black background. Then all it takes is just to rotate the camera with a mouse so that the 3d surface looked like 2d from above.

Drawing an arrow with specified direction on a point in scatter plot in Julia

Is there a way to draw a scatter plot in Julia (preferably with gr backend), in which every point has an arrow pointing to a specified direction on it?
Specifically, my task is to create a gif image with multiple moving points with a small arrow on every point pointing to the direction of its velocity.
So, you want to plot a vector field, right?
The "arrow plot" you are looking for, is usually called quiver-plot in many programming languages. In Julia, too.
If you use Plots.jl the syntax is quiver(x,y,quiver=(u,v)), where x and y are the coordinate vectors and u and v the arrow magnitude vectors.
If you use GR or PyPlot directly the syntax is possibly a bit different.
Small Example
using Plots
gr()
N = 10
x = rand(1:10,N)
y = rand(1:10,N)
u = rand(N)
v = rand(N)
scatter(x,y)
quiver!(x,y,quiver=(u,v))

R: Plot arrows perpendicular to contour lines

I have two vectors representing x and y-coordinates in a scatter plot, and a thrid variable (z) for each (x,y)-coordinate representing the variable from which to draw contour lines. Example data are given as follows:
df<-data.frame(x=runif(n=30,min=-6,max=6),
y=runif(n=30,min=-6,max=10),
z=seq(1,100,length.out=30))
I use the R-package akima to generate the z-matrix for the contour plot
library(akima)
M1 <- interp(x=df$x,y=df$y,z=df$z)
contour(x=M1$x,y=M1$y,z=M1$z)
I now want to draw arrows perpendicular to the contourlines, preferably using something like the function "quiver" in the R-package pracma, with the origin of an arrow at every (x,y)-coordinate and with the arrow pointing in the direction of the gradient of the contourlines. Is there a way to do this?
My best idea so far is to somehow extract (x,y)-gradients of the contourlines and use these as velocities in the quiver function.
Grateful for any assistance.
The pracma package has a gradient function that can do this for you using the original M1$z values. For example, using your code to get M1 after set.seed(123):
contour(x=M1$x,y=M1$y,z=M1$z, asp = 1) # asp = 1 needed so things look perpendicular
library(pracma)
g <- gradient(M1$z, M1$x, M1$y)
x <- outer(M1$x, M1$y, function(x, y) x)
y <- outer(M1$x, M1$y, function(x, y) y)
quiver(x, y, g$Y, g$X, scale = 0.02, col = "blue")
Note that the gradient labels in the quiver plot have been swapped. Maybe I set up the x and y values transposed from the way the package expects. Here's what you get:

Plotting ellipsoids / oblate spheroids in rgl

I have been using rgl to plot spheres, but now I need to plot ellipsoids.
The package includes ellipse3d; however, this seems to be for fitting ellipsoids to data, using matrices and stuff I'm not very good at.
What I want is a simple way to plot ellipsoids, in a similar way to spheres, using the centre coordinates and the scales in each direction. Can anyone help me out?
If you don't need the ellipse rotated around the axes, then you can just use a diagonal matrix for x (this plots a sphere, and defines the virtual "axes" along the x, y, z axes) and use the centre and scale parameters to shift the location and change the proportions.
plot3d(ellipse3d(diag(3),centre=c(1,2,4),scale=c(1,2,5)))
There's one in my cda package,
library(cda)
library(rgl)
## single ellipsoid
plot3d(rgl.ellipsoid(a=2,b=1,c=5))
## multiple ellipsoids, translated and rotated
cl <- helix(0.5, 1, 36, delta=pi/6, n.smooth=1e3)
sizes <- equal_sizes(0.04,0.02,0.02,NROW(cl$positions))
rgl.ellipsoids(cl$positions, sizes, cl$angles, col="gold")

Plotting a 3D surface plot with contour map overlay, using R

I have a 3-tuple data set (X,Y,Z points) that I want to plot using R.
I want to create a surface plot from the data, and superimpose a contour map on the surface plot, so as to create the impression of the contour map being the "shadow" or projection from the surface plot. The contour map is to appear below the surface plot.
My data set looks somewhat like this:
Axis | Data Type
-------------------
X | Date value
Y | Float value
Z | Float value
How can I achieve this?
Edit:
I just saw that you pointed out one of your dimensions is a date. In that case, have a look at Jeff Ryan's chartSeries3d which is designed to chart 3-dimensional time series. Here he shows the yield curve over time:
Original Answer:
As I understand it, you want a countour map to be the projection on the plane beneath the 3D surface plot. I don't believe that there's an easy way to do this other than creating the two plots and then combining them. You may find the spatial view helpful for this.
There are two primary R packages for 3D plotting: rgl (or you can use the related misc3d package) and scatterplot3d.
rgl
The rgl package uses OpenGL to create interactive 3D plots (read more on the rgl website). Here's an example using the surface3d function:
library(rgl)
data(volcano)
z <- 2 * volcano # Exaggerate the relief
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x, y, z, color=col, alpha=0.75, back="lines")
The alpha parameter makes this surface partly transparent. Now you have an interactive 3D plot of a surface and you want to create a countour map underneath. rgl allows you add more plots to an existing image:
colorlut <- heat.colors(zlen,alpha=1) # use different colors for the contour map
col <- colorlut[ z-zlim[1]+1 ]
rgl.surface(x, y, matrix(1, nrow(z), ncol(z)),color=col, back="fill")
In this surface I set the heights=1 so that we have a plane underneath the other surface. This ends up looking like this, and can be rotated with a mouse:
scatterplot3d
scatterplot3d is a little more like other plotting functions in R (read the vignette). Here's a simple example:
temp <- seq(-pi, 0, length = 50)
x <- c(rep(1, 50) %*% t(cos(temp)))
y <- c(cos(temp) %*% t(sin(temp)))
z <- c(sin(temp) %*% t(sin(temp)))
scatterplot3d(x, y, z, highlight.3d=TRUE,
col.axis="blue", col.grid="lightblue",
main="scatterplot3d - 2", pch=20)
In this case, you will need to overlay the images. The R-Wiki has a nice post on creating a tanslucent background image.

Resources