I am trying to do up a 3D array plot in R.
I already have an array built up and defined with the corresponding z-values
e.g. CVHSP500 = array(0,c((nHSP500-N),N))
So now I am trying to do a 3D array plot with it. I decided to go with persp3d(CVHSP500,col = "lightblue",) and have obtained a rather decent plot.
3D Image
So there are obviously some issues with this plot.
1) The coordinates are not defined correctly.
Reading up online on the usage of persp3D, and other R programming functions/packages like slice3D, they all require x, y and z to be separate list.
I don't understand how to match the values of x and y to the respective z, and since persp3D works perfectly without me having to do that, I decided to use persp3D.
But I will need to insert coordinates for it, but I have no idea how to.
2) Any advice how do I color the plots for different ranges of z?
The ones online all seem to have to refer to individual x, y and z lists and some form of advanced modification which I can't really understand. This light blue color looks okay but it would be good for different ranges of z as well though.
Thanks for the help. Much appreciated.
To transform a 2D array representing z for each (x,y) into 3 vectors x, y and z, you can do this:
CVHSP500 = array(0,c((nHSP500-N),N))
x <- rep(1:(nHSP500-N),N)
y <- rep(1:N,(nHSP500-N))
z <- CVHSP500
dim(z) <- (nHSP500-N)*N
Related
I am writing this in IDL.
I want to pass a list of numbers in array x that goes through function y, where then all y values go into an array z, that will be plotted x vs. z.
I have tried to solve this numerous ways. Initially, I was using lists, where it is easy to use the 'list.add' function to add new elements to an array. However, I can only plot arrays in IDL, and not list (thus getting an error). I have been messing around with the code for quite some time, so hopefully I can get some pointers in the right direction
z = [] ; array to hold y data
x = [1,2,3,4] ; x data
FOREACH element, x DO BEGIN
y = x+3
z.add,y
ENDOFREACH
; the plot
p = SCATTERPLOT(x,z)
I am hoping to get a simple scatter plot from this, but I do not know how to change my arrays.
There are two main ways to do this:
Concatenate arrays. For example, y = [y, 3] tacks on a 3 at the end of the y array. This is slow if you intend to this "a lot".
Use a list for building the list of values, then use the toArray() method at the end to get and array which can be plotted.
I have 3 Nx1 arrays, say X,Y,Z. I'd like to create a 3D plot such that each of the arrays I can assign different colours to improve the visibility of points. My main objective now is to change only the colour of array Z, such that elements of array X and Y have different colours as that of elements of Z .
I tried scatter3 function and gscatter MATLAB functions, but not able to achieve what I desire.
If we see the Vertical axis in image (Z axis), the colour of points is varying from Blue to Orange/Yellow. I want to set the colour of all these Z points as Red. Rest, all the colours of X and Y points remain same
scatter(x, y, a, c) takes arguments x and y, and then a for size, and c for colour. a can either be a single scalar, or a vector with a size for each (x,y) point. c can be an RGB triplet, or a vector, the same size as x and y. For example:
x = 1:4;
scatter(x, x, 10*x, x);
results in
scatter3(x,y,z,s,c) is similar, so in your case, perhaps
scatter3(X,Y,Z,[],Z)
will result in your data having a different colour determined by its z value.
One little example which I think is what you're looking for:
X = rand(100,1);
Y = rand(100,1);
Z = rand(100,1);
scatter3(X,Y,Z,[],Z)
Produces:
I got the answer by trying different MATLAB functions.
I used the gscatter function. As I had to colour all the Z points(consider 'Z' array as the 3rd data set) as Red and rest X and Y data set (the other 2 data set) as green or other colour, I used the following code:
group = Z(:,1);
gscatter(X(:,1),Y(:,1),group, 'gr', 'xo');
It creates a 2D plot but serves my purpose.
The Image is made of 3 data sets, but the first 2 data sets are intentionally combined (in Green Cross) and 3rd data set is kept Red, to visualize the relation of 3rd data set with 1st and 2nd data sets combined.
Click here for Image.
I first begin by running the code below to tune a SVM:
tunecontrol <- tune.control(nrepeat=5, sampling = "fix",cross=5, performances=T)
tune_svm1 <- tune(svm,
Y ~ 1
+ X
, data = data,
ranges = list(epsilon = seq(epsilon_start
,epsilon_end
,(epsilon_end-epsilon_start)/10)
, cost = cost_start*(1:5)
, gamma = seq(gamma_start
,gamma_end
,(gamma_end - gamma_start)/5))
, tunecontrol=tunecontrol)
In tune_svm1$performances I have 330 observations containing all the values for epsilon, cost, and gamma that I stated in the ranges section of the above code as well as another column for the calculated error.
I'd like to generate a 3D surface plot for epsilon, cost, gamma, and error using three variables as X,Y,Z and the last for color. I've read on several resources for plot3d and persp but have had a lot of difficulty implementing.
If I try to follow the examples provided and use mesh to generate a mesh plot, I can only mesh together 3 of the 4 variables from tune_svm1$performances and saving the separate results for X,Y and Z as shown in the first link is difficult because the mesh is saved as an array, not a matrix. I've tried to hack a graph using the following code but the visual is nonsensical (probably because the order isn't being preserved by meshing each individually:
M1 <- mesh(tune_svm1$performances$epsilon[1:nrow(tune_svm1$performances)]
,tune_svm1$performances$cost[1:nrow(tune_svm1$performances)])
M2 <- mesh(tune_svm1$performances$epsilon[1:nrow(tune_svm1$performances)]
,tune_svm1$performances$gamma[1:nrow(tune_svm1$performances)])
M3 <- mesh(tune_svm1$performances$epsilon[1:nrow(tune_svm1$performances)]
,tune_svm1$performances$error[1:nrow(tune_svm1$performances)])
x <- M1$x ; y <- M1$y ; z <- M2$y ; c <- M3$y
surf3D(x,y,c, colvar = c)
What's the best way to approach this? Thank you.
I have a large 2D-array (31100 x 4) built up like this:
Intensity | X | Y | Z
out of this array I would like to create a 3D-array to visualize it as a 3D-image.
I would somehow have to transform the 2D into a 3D-array. Since there will be plenty of points where there is no intensity value available all of these points should be zero values.
Side information:
min(x) = -152.0120;
max(x) = 161.4350;
min(y) = -256.2560;
max (y) = -52.3801;
min(z) = -428.5920;
max (z) = -152.4210;
Because of the larger number of decimal places I might have to round up. Yet, this would possibly lead to several intensities being assigned to the same coordinate combination. Logically, I would have to add up the intensities in these cases.
This makes everything really complicated.
I would be happy if someone could teach me how to transform a 2D into a 3D array with zero-values for spots without intensity values.
Thanks for reading.
So, I have two arrays:
X'
ans =
2.5770 2.5974 2.1031 2.7813 2.6083 2.9498 3.0053 3.3860
>> Y'
ans =
0.7132 0.5908 1.9988 1.0332 1.3301 1.1064 1.3522 1.3024
I would like to combine n-th members of two arrays together, and than plot those coordinates on graph.
So it should be:
{(2.5770,0.7132), (2.5974,0.5908)...}
Is this possible to do? If so, how?
Schorsch showed that it is simple to plot, but just to answer the question as asked in the title, you can combine the arrays into coordinates by just arranging the vectors like rectangles.
Your x and y are vertical, so you can put them side-by-side in a 2-column matrix:
combined = [x y]
or transform and have 2 rows: combined = [x' ; y']
(Because they're vertical, what you don't want is these, which would concatenate them out into one long column or row: [x ; y] or [x' y'])
Just to be clear, though, this is not needed for plotting.
Edit: A suggested edit asked what happens if you plot(combined). That depends if it's the horizontal or vertical version. In any case, plotting a 2x? matrix won't plot x vs. y. It plots all of the columns versus the simple indices 1,2,3,... So the first way I defined combined will make two lines, plotting x and y on the y-axis against their indices on the x-axis, and the second version of combined will make a strange plot with the all of the values of x plotted in a vertical column where x=1 and all of the points of y beside those at x=2.