Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:27:36+00:00 2026-06-17T22:27:36+00:00

Below is a 3D curve using the R function persp3d and specifying the colors.

  • 0

Below is a 3D curve using the R function persp3d and specifying the colors.

library(rgl)

y = seq(-5,25,by=0.1)
x = seq(5,20,by=0.2)

NAs <- rep(NA, length(x)*length(y))
z <- matrix(NAs, length(x), byrow = T)
for(i in seq(1,length(x))) {
    for(j in seq(1,length(y))) {
        val = x[i] * y[j]
        z[i,j] = val
        if(z[i,j] < 0.02) {
            z[i,j] = NA
        }

    }
}

col <- rainbow(length(x))[rank(x)]

open3d()
persp3d(x,y,z,color=col,xlim=c(5,20),ylim=c(5,10),axes=T,box=F,xlab="X Axis",ylab="Y Axis",zlab="Z Axis")

And it produces this image:

enter image description here

In the current version, for an x value of 15, the color is blue regardless of the z value. But I’d like it so that high z values are dark blue whereas low z values are light blue, if that makes sense. How can I do something like this, so that color not only distinguishes x values but also z values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:27:38+00:00Added an answer on June 17, 2026 at 10:27 pm

    You will have to play around with colors in HSV format rather than RGB format for this. It’s easier that way I think.

    See my sample code below.

    library(rgl)
    
    y = seq(-5,25,by=0.1)
    x = seq(5,20,by=0.2)
    
    NAs <- rep(NA, length(x)*length(y))
    z <- matrix(NAs, length(x), byrow = T)
    for(i in seq(1,length(x))) {
      for(j in seq(1,length(y))) {
        val = x[i] * y[j]
        z[i,j] = val
        if(z[i,j] < 0.02) {
          z[i,j] = NA
        }
    
      }
    }
    

    Create unique color for each value of x.

    col <- rainbow(length(x))[rank(x)]
    

    Create grid of colors by repeating col length(y) times

    col2 <- matrix(rep(col,length(y)), length(x))
    for(k in 1:nrow(z)) {
    
      row <- z[k,]
      rowCol <- col2[k,]  
      rowRGB <- col2rgb(rowCol) #convert hex colors to RGB values
      rowHSV <- rgb2hsv(rowRGB) #convert RGB values to HSV values
    
      row[is.na(row)] <- 0
      v <- scale(row,center=min(row), scale=max(row)-min(row)) # scale z values to 0-1
    
      rowHSV['s',] <- v #update s or v values by our scaled values above
      # rowHSV['v',] <- v  # try changing either saturation or value i.e. either s or v
    
      newRowCol <- hsv(rowHSV['h',], rowHSV['s',], rowHSV['v', ]) #convert back to hex color codes
      col2[k,] <- newRowCol #Replace back in original color grid
    }
    
    open3d()
    persp3d(x,y,z,color=col2,xlim=c(5,20),ylim=c(5,10),axes=T,box=F,xlab="X Axis",ylab="Y Axis",zlab="Z Axis")
    

    This should give following. You can play around scaling of saturation or value of colors to get desired “lightness” or “darkness” of shades.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So Im using nls in ggplot2 to plot a power curve code is below:
I am trying to incrementally rotate a curve using the codes below in c#
I have several points, and I try to draw Bezier curve using code below
Is it possible to create the curved sail shape below using HTML and CSS
Below is my form. Using jQuery I load a <select> element based on value
Before getting my form helper working, I was using the below for my select
I am trying to optimize a bezier curve implementation by using the formula used
I am using a toolkit to do some Elliptical Curve Cryptography on an ATMega2560.
I have a 10 period cost curve table below. How do I programmatically collapse/condense/shrink
I have posted a part of my code below. Newton() function calls Bezier() function.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.