I have three variables: x,y, and z and I want to produce a surface plot.
z<-runif(50,0,1)
y<-runif(50,1,2)
x<-runif(50,3,6)
plot_ly(x = ~x, y = ~y, z= ~z) %>% add_surface()
I get the following error
Error: `z` must be a numeric matrix
What exactly does z represent if not the variable corresponding to the vertical axis? I have seen the Volcano example where they use the matrix to generate that plot, but I still am not sure what that z matrix represents in that example either.
What I would like is for someone to plot an easy to understand 3D function like z=f(x,y) = x^2 + y^2 using the surface functionality in plot_ly just so I can understand how to generate a plot based on three variables.
The problem with your above code is, that you haven't specified the trace type - and what you need to pass to the z argument depends on this specification.
Passing the arguments x, y, z suggests you want to display a scatter3d plot - you can test this by dropping add_surface():
z <- runif(50,0,1)
y <- runif(50,1,2)
x <- runif(50,3,6)
plot_ly(x = x, y = y, z = z)
Which gives the warning:
No trace type specified: Based on info supplied, a 'scatter3d' trace
seems appropriate. Read more about this trace type ->
https://plot.ly/r/reference/#scatter3d No scatter3d mode specifed:
Setting the mode to markers Read more about this attribute ->
https://plot.ly/r/reference/#scatter-mode
add_surface() on the other hand suggests you want to display a 3D Surface Plot.
You already mentioned the volcano example. This kind of plot only needs a single numeric matrix to create the plot (argument z).
Accordingly with your example code you mixed up both plot types which leads to the error message.
How to avoid this confusion?
If you have a look at ?plot_ly there is a description for arguments "..." passed to the according trace type (z is one of them):
Arguments (i.e., attributes) passed along to the trace type. See
schema() for a list of acceptable attributes for a given trace type
(by going to traces -> type -> attributes).
schema() is a very useful hint to orient oneself in the plotly library. Execute the following code to browse through the different plotly trace types and their available attributes in a very convenient way:
# install.packages("listviewer")
library(plotly)
library(listviewer)
schema(jsonedit = interactive())
I guess this is what you were after in the first place:
z <- runif(50,0,1)
y <- runif(50,1,2)
x <- runif(50,3,6)
plot_ly(x = x, y = y, z = z, type = 'mesh3d')
Related
I'm trying to create a volume plot in R that will show the smoothed density of points in 3D space. I'm using plotly to plot with scatter3D at the moment.
df = data.frame(a = sample(seq(.5,.8,.001),100),
b = sample(seq(0,.5,.001),100),
c = sample(seq(0,.3,.001),100),
value = sample(seq(0,1,.01),100))
plot_ly(df, x = ~a, y = ~b, z = ~c, type = 'scatter3d')
Setting type to 'mesh3d' works to connect these points as a surface in 3d space.
plot_ly(df, x = ~a, y = ~b, z = ~c, type = 'mesh3d')
However, trying to set the type to 'volume' gives me a blank plot each time.
plot_ly(df, x = ~a, y = ~b, z = ~c, value = ~value, type = 'volume',
isomin = 0, isomax = 1, opacity = 1)
The documentation is here: https://plotly.com/r/reference/volume/ and specifies:
Draws volume trace between iso-min and iso-max values with coordinates given by four 1-dimensional arrays containing the value, x, y and z of every vertex of a uniform or non-uniform 3-D grid. Horizontal or vertical slices, caps as well as spaceframe between iso-min and iso-max values could also be drawn using this trace.
The python version of plotly provides an example: https://plotly.com/python/3d-volume-plots/ but I don't know where I'm missing the conversion to R. I've included the extra value dimension for volume plots, but it doesn't seem to do anything.
Unlike the documentation of plotly where it states you can use non-uniform grids with a volume, plotting 3d volumes with isomin and isomax is actually not possible. Having a single coordinate which is non-uniform invalidates the whole plot. I struggled with the same issue for ages, wondering why my plots keep ending up blank.
To demonstrate, first create a plot with a uniform grid. For example, use the Python example from the resource you linked: https://plotly.com/python/3d-volume-plots/. (It doesn't matter that it's the Python API, rendering the plots is language independent).
import plotly.graph_objects as go
import numpy as np
X, Y, Z = np.mgrid[-8:8:40j, -8:8:40j, -8:8:40j]
values = np.sin(X*Y*Z) / (X*Y*Z)
fig = go.Figure(data=go.Volume(
x=X.flatten(),
y=Y.flatten(),
z=Z.flatten(),
value=values.flatten(),
isomin=0.1,
isomax=0.8,
opacity=0.1, # needs to be small to see through all surfaces
surface_count=17, # needs to be a large number for good volume rendering
))
fig.show()
This should show a plot. Next, change the coordinate of one point:
X[0, 0, 0] += 1
And tada: it no longer works and shows an empty plot.
I am using the R programing language. Recently, I came across this previous stackoverflow post where it describes how to make a 1 dimensional scatter plot in R: How can I plot a 1-D plot in R?:
x <- rnorm(100,10,10)
x <- data.frame(x,1) ## 1 is your "height"
plot(x, type = 'o', pch = '|', ylab = '')
My question: is it possible to transform the above plot into a "plotly" plot?
Suppose I have the following data:
library(plotly)
x <- rnorm(100,10,10)
color <- rnorm(100, 2,1)
frame = data.frame(x,color)
Would it be possible to do something like this?
fig <- plot_ly(data = frame, x = ~frame$x, color ~ frame$color )
fig
I get the following error when running this code:
No trace type specified:
Based on info supplied, a 'histogram' trace seems appropriate.
Can someone please show me how to do this?
Thanks
Source: https://plotly.com/r/line-and-scatter/
In plotly language, a trace is the type of visualization that you would like to use to display your data. So the error basically lets you know that you have not specified any trace and that the program is picking one for you: "a histogram". For scatterplots, you need type = "scatter" and mode = "markers'.
Also, inside the plot_ly() function, once you specify the data argument, you can simply access the columns with the column name preceded by a tilde ~.
Finally, since you want a one dimensional scatterplot along the x-axis, you need to add y = " " to the plot_ly() function.
This is how you can achieve your desired result:
library(plotly)
x <- rnorm(100,10,10)
color <- rnorm(100, 2,1)
frame = data.frame(x,color)
plot_ly(type = "scatter", mode = "markers", data = frame, x = ~x, y = " ", color = ~color )
Note that plotly is a very rich framework and you can read the appropriate documentation to learn how to customize your plot to your liking.
I apologize for this super basic question, but I am not experienced in plotting, and a lot of the documentation for Julia plotting assumes more knowledge than I have!
I am creating a scatter plot, using Plots, where each marker is plotted based on spatial position, and I want to scale the color by magnitude of value that each marker holds. I created a color gradient as such:
C(g::ColorGradient) = RGB[g[z] for z = LinRange(0,1,M)]
g = :inferno
cgrad(g,[0.01,0.99]) |> C
M is related to the number of markers, this way I create a suitable scale of colors based on the number of markers I have.
I assumed I was creating some kind of structure that would assign a color from this gradient based off a value ranging from 0.01 to 0.99. However, I guess I don't understand what the structure C is. When I assign color = C(v), where v is between 0 and 1.00, I get an error saying that C does not accept type Float64.
Is there a way I can assign a marker some color from this gradient based off its value? I have all of the values for each location stored in another array.
UPDATE: I have also tried indexing into C. I turned my values into Int64 ranging from 1-99, and tried to set color=C[v], but C also does not take Type Int64.
UPDATE 2: Ok, so i realized my issue was I did not understand the |> functionality, So i rewrote the code to look like:
C(g::ColorGradient) = RGB[g[z] for z = LinRange(0,1,M)]
g = :inferno
myGrad = (cgrad(g,[0.00,1.00]) |> C)
and now I can index into my color gradiant! However I still am having an issue setting the color equal to the value stored in the myGradient array.
for i = 1:M
X,Y = find_coords(i,pd)
colors = myGrad[c_index[i]]
outline = rand(Float64,3)
plt = plot!(X,Y,colors, markerstrokecolor = outline)
end
When I type myGrad[c_index[i]] into REPL it plots a color. However I am getting an error from the above code which states
"Cannot convert RGB{Float64} to series data for plotting"
If i change the plot line as follows I get a slightly different error:
plt = plot!(X,Y,markercolor = colors, markerstrokecolor = outline)
ERROR: LoadError: MethodError: no method matching plot_color(::Float64)
So for some reason I cant store this color, as a color variable for my plot.
There are a few different issues at play here. Firstly, if you want to create a scatter plot, you should probably use scatter. It also doesn't seem necessary to plot things in a loop here, although it's hard to tell as your code isn't a minimum working example (MWE), as it relies on things defined somewhere else in your code.
Here's an example of how this might work:
using Plots
# Create a discrete color gradient with 20 points
my_colors = [cgrad(:inferno, [0.01, 0.99])[z] for z ∈ range(0.0, 1.0, length = 20)]
# Draw some random data points
x, y = sort(rand(100)), rand(100)
# Assign a color between 1 and 20 on the color grid to each point
z = sort(rand(1:20, 100))
# Plot
scatter(x, y, color = my_colors[z], markerstrokecolor = "white", label = "",
markersize = [10 for _ ∈ 1:100])
gives:
I want to plot the function
4(x)^2 = ((y)^2/(1-y));
how can I plot this?
--> 4*(x) = ((y^2)*(1-y)^-1)^0.5;
4*(x) = ((y^2)*(1-y)^-1)^0.5;
^^
Error: syntax error, unexpected =, expecting end of file
Since Scilab 6.1.0, plotimplicit() does it:
plotimplicit "4*x^2 = y^2/(1-y)"
xgrid()
Can't do more simple. Result:
Well, you have to first create a function and for that you have to express one variable in terms of the other.
function x = f(y)
x = (((y^2)*(1-y)^-1)^0.5)/4;
endfunciton
Then you need to generate the input data (i.e, the points at which you want to evaluate the function)
ydata = linspace(1, 10)
Now you push your input point through the function to get your output points
xdata = f(ydata)
Then, you can plot the pairs of x and y using:
plot(xdata, ydata)
Or even easier, without the intermediate step of generating the output data, you can simply do:
plot(f(ydata), ydata)
BTW. I find it strange that the function you are trying to plot is x in terms of y, usually, x is the input variable, but I hope you know what you are trying to accomplish.
Reference: https://www.scilab.org/tutorials/getting-started/plotting
Take care that y must be in [-inf 1[
y=linspace(-10 ,1.00001,1000);
x = sqrt(y^2./(1-y))/4;
clf; plot(y,x),plot(y,-x)
If x is a solution -x is also solution
I am having trouble changing the x and y labels on a partial plot for a gbm model. I need to rename them for the journal article.
I read this in and create the plot as follows:
library(gbm)
final<- readRDS(final_gbm_model)
summary(final, n.trees=final$n.trees)
Here is the summary output:
var rel.inf
ProbMn50ppb ProbMn50ppb 11.042750
ProbDOpt5ppm ProbDOpt5ppm 7.585275
Ngw_1975 Ngw_1975 6.314080
PrecipMinusETin_1971_2000_GWRP PrecipMinusETin_1971_2000_GWRP 4.988598
N_total N_total 4.776950
DTW60YrJurgens DTW60YrJurgens 4.415016
CVHM_TextZone CVHM_TextZone 4.225048
RiverDist_NEAR RiverDist_NEAR 4.165035
LateralPosition LateralPosition 4.036406
CAML1990_natural_water CAML1990_natural_water 3.720303
PctCoarseMFUpActLayer PctCoarseMFUpActLayer 3.668184
BioClim_BIO12 BioClim_BIO12 3.561071
MFDTWSpr2000Faunt MFDTWSpr2000Faunt 3.383900
PBot_krig PBot_krig 3.362289
WaterUse2 WaterUse2 3.291040
AVG_CLAY AVG_CLAY 3.280454
Age_yrs Age_yrs 3.144734
MFVelSept2000 MFVelSept2000 3.064030
AVG_SILT AVG_SILT 2.882709
ScreenLength ScreenLength 2.683542
HydGrp_C HydGrp_C 2.666106
AVG_POR AVG_POR 2.563147
MFVelFeb2000 MFVelFeb2000 2.505106
HiWatTabDepMin HiWatTabDepMin 2.421521
RechargeAnnualmmWolock RechargeAnnualmmWolock 2.252706
I can create a partial dependence plot as follows:
plot(final,"ProbMn50ppb",n.trees=final$n.trees)
But if I try to set the label arguments I get the following error:
plot(final,"ProbMn50ppb",n.trees=final$n.trees,ylab="LNNO3")
Error in plot.default(X$X1, X$y, type = "l", xlab = x$var.names[i.var], :
formal argument "ylab" matched by multiple actual arguments
How can I change the y and x axis labels?
The plot.gbm function passes its own name to the generic plot function so the two are colliding. So you will not be able to customize the plot the way you want in that mode. But the authors did provide an alternative when you set return.grid=TRUE. Instead of building a plot, it will output the data itself. You can then use that for any plot including ggplot2.
plotdata <- plot(gbm1, return.grid=TRUE)
plot(plotdata, type="l", ylab="ylab", xlab="xlab")
Example data from help(gbm)
You can also change the gbm object itself before plotting (or in a function):
your_gbm_obj$var.names[index] = "axis label"