plotly adding colorbar to mesh3d without using intensity (using facecolor)












1















I have a need to place a colorbar to a mesh3d plot that consists of several traces. Each mesh3d trace has a single color, but I need the colorbar to span all the trace-colors.



I am trying to combine a scatter3d with visible="legendonly" with the mesh3d, so achieve this. But when the mesh is plotted, the legend is removed.



Using the helicopter-example:



library(plotly)
library(geomorph)

plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
dest <- basename(plyFile)
if (!file.exists(dest)) {
download.file(plyFile, dest)
}

mesh <- read.ply(dest, ShowSpecimen = F)
# see getS3method("shade3d", "mesh3d") for details on how to plot

# plot point cloud
x <- mesh$vb["xpts",]
y <- mesh$vb["ypts",]
z <- mesh$vb["zpts",]
m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

# now figure out the colormap
zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

library(scales)
facecolor = colour_ramp(
brewer_pal(palette="RdBu")(9)
)(rescale(x=zmean))


plot_ly() %>%
# Creates the legend, and also the plotting space
add_trace(
x = x, y = y, z = z,
color = x,
colors = c("#ffffff", "#000000"),
# visible="legendonly",
type = "scatter3d",
mode="markers"
) %>%

# Adds the mesh, but removes the legend
add_trace(
x = x, y = y, z = z,
i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
facecolor = facecolor,
type = "mesh3d"
)









share|improve this question





























    1















    I have a need to place a colorbar to a mesh3d plot that consists of several traces. Each mesh3d trace has a single color, but I need the colorbar to span all the trace-colors.



    I am trying to combine a scatter3d with visible="legendonly" with the mesh3d, so achieve this. But when the mesh is plotted, the legend is removed.



    Using the helicopter-example:



    library(plotly)
    library(geomorph)

    plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
    dest <- basename(plyFile)
    if (!file.exists(dest)) {
    download.file(plyFile, dest)
    }

    mesh <- read.ply(dest, ShowSpecimen = F)
    # see getS3method("shade3d", "mesh3d") for details on how to plot

    # plot point cloud
    x <- mesh$vb["xpts",]
    y <- mesh$vb["ypts",]
    z <- mesh$vb["zpts",]
    m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

    # now figure out the colormap
    zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

    library(scales)
    facecolor = colour_ramp(
    brewer_pal(palette="RdBu")(9)
    )(rescale(x=zmean))


    plot_ly() %>%
    # Creates the legend, and also the plotting space
    add_trace(
    x = x, y = y, z = z,
    color = x,
    colors = c("#ffffff", "#000000"),
    # visible="legendonly",
    type = "scatter3d",
    mode="markers"
    ) %>%

    # Adds the mesh, but removes the legend
    add_trace(
    x = x, y = y, z = z,
    i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
    facecolor = facecolor,
    type = "mesh3d"
    )









    share|improve this question



























      1












      1








      1


      1






      I have a need to place a colorbar to a mesh3d plot that consists of several traces. Each mesh3d trace has a single color, but I need the colorbar to span all the trace-colors.



      I am trying to combine a scatter3d with visible="legendonly" with the mesh3d, so achieve this. But when the mesh is plotted, the legend is removed.



      Using the helicopter-example:



      library(plotly)
      library(geomorph)

      plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
      dest <- basename(plyFile)
      if (!file.exists(dest)) {
      download.file(plyFile, dest)
      }

      mesh <- read.ply(dest, ShowSpecimen = F)
      # see getS3method("shade3d", "mesh3d") for details on how to plot

      # plot point cloud
      x <- mesh$vb["xpts",]
      y <- mesh$vb["ypts",]
      z <- mesh$vb["zpts",]
      m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

      # now figure out the colormap
      zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

      library(scales)
      facecolor = colour_ramp(
      brewer_pal(palette="RdBu")(9)
      )(rescale(x=zmean))


      plot_ly() %>%
      # Creates the legend, and also the plotting space
      add_trace(
      x = x, y = y, z = z,
      color = x,
      colors = c("#ffffff", "#000000"),
      # visible="legendonly",
      type = "scatter3d",
      mode="markers"
      ) %>%

      # Adds the mesh, but removes the legend
      add_trace(
      x = x, y = y, z = z,
      i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
      facecolor = facecolor,
      type = "mesh3d"
      )









      share|improve this question
















      I have a need to place a colorbar to a mesh3d plot that consists of several traces. Each mesh3d trace has a single color, but I need the colorbar to span all the trace-colors.



      I am trying to combine a scatter3d with visible="legendonly" with the mesh3d, so achieve this. But when the mesh is plotted, the legend is removed.



      Using the helicopter-example:



      library(plotly)
      library(geomorph)

      plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
      dest <- basename(plyFile)
      if (!file.exists(dest)) {
      download.file(plyFile, dest)
      }

      mesh <- read.ply(dest, ShowSpecimen = F)
      # see getS3method("shade3d", "mesh3d") for details on how to plot

      # plot point cloud
      x <- mesh$vb["xpts",]
      y <- mesh$vb["ypts",]
      z <- mesh$vb["zpts",]
      m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

      # now figure out the colormap
      zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

      library(scales)
      facecolor = colour_ramp(
      brewer_pal(palette="RdBu")(9)
      )(rescale(x=zmean))


      plot_ly() %>%
      # Creates the legend, and also the plotting space
      add_trace(
      x = x, y = y, z = z,
      color = x,
      colors = c("#ffffff", "#000000"),
      # visible="legendonly",
      type = "scatter3d",
      mode="markers"
      ) %>%

      # Adds the mesh, but removes the legend
      add_trace(
      x = x, y = y, z = z,
      i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
      facecolor = facecolor,
      type = "mesh3d"
      )






      r plotly r-plotly






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 14:44







      Athanasia Mowinckel

















      asked Nov 13 '18 at 16:28









      Athanasia MowinckelAthanasia Mowinckel

      907




      907
























          1 Answer
          1






          active

          oldest

          votes


















          0














          After a lot of hacking, I finally have a working solution.
          In this case, by plotting a mesh3d area, with points that are "inside" the helicopter and will not be visible later, and then plot the actual helicopter later.



          It seems that "visible='legendonly'" does not apply to mesh3d, as this option removes both the plot and legend.



          library(plotly)
          library(geomorph)

          plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
          dest <- basename(plyFile)
          if (!file.exists(dest)) {
          download.file(plyFile, dest)
          }

          mesh <- read.ply(dest, ShowSpecimen = F)
          # see getS3method("shade3d", "mesh3d") for details on how to plot

          # plot point cloud
          x <- mesh$vb["xpts",]
          y <- mesh$vb["ypts",]
          z <- mesh$vb["zpts",]
          m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

          # now figure out the colormap
          zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

          # Get colors you want
          cols = brewer_pal(palette="RdBu")(9)

          # Ramp to add to facecolor
          library(scales)
          facecolor = colour_ramp(cols)(rescale(x=zmean))

          # Create data.frame of colours and breakpoints.
          # Must go from 0 to 1, plotly scales it based on values it self.
          colz = data.frame(seq(0,1,length.out = length(cols)),
          cols)

          # Make stupid pointcloud to fool the colorbar
          xx = c(min(x), max(x))
          yy = c(min(y), max(y))
          zz = c(min(z), max(z))

          plot_ly() %>%

          # Creates the legend, and also the plotting space
          add_trace(
          x = xx, y = yy, z = zz,
          intensity = x,
          colorscale = colz,
          # visible="legendonly",
          type = "mesh3d"
          ) %>%


          # Adds the mesh
          add_trace(
          x = x, y = y, z = z,
          i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
          facecolor = facecolor,
          showscale=FALSE,
          type = "mesh3d"
          )





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285427%2fplotly-adding-colorbar-to-mesh3d-without-using-intensity-using-facecolor%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            After a lot of hacking, I finally have a working solution.
            In this case, by plotting a mesh3d area, with points that are "inside" the helicopter and will not be visible later, and then plot the actual helicopter later.



            It seems that "visible='legendonly'" does not apply to mesh3d, as this option removes both the plot and legend.



            library(plotly)
            library(geomorph)

            plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
            dest <- basename(plyFile)
            if (!file.exists(dest)) {
            download.file(plyFile, dest)
            }

            mesh <- read.ply(dest, ShowSpecimen = F)
            # see getS3method("shade3d", "mesh3d") for details on how to plot

            # plot point cloud
            x <- mesh$vb["xpts",]
            y <- mesh$vb["ypts",]
            z <- mesh$vb["zpts",]
            m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

            # now figure out the colormap
            zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

            # Get colors you want
            cols = brewer_pal(palette="RdBu")(9)

            # Ramp to add to facecolor
            library(scales)
            facecolor = colour_ramp(cols)(rescale(x=zmean))

            # Create data.frame of colours and breakpoints.
            # Must go from 0 to 1, plotly scales it based on values it self.
            colz = data.frame(seq(0,1,length.out = length(cols)),
            cols)

            # Make stupid pointcloud to fool the colorbar
            xx = c(min(x), max(x))
            yy = c(min(y), max(y))
            zz = c(min(z), max(z))

            plot_ly() %>%

            # Creates the legend, and also the plotting space
            add_trace(
            x = xx, y = yy, z = zz,
            intensity = x,
            colorscale = colz,
            # visible="legendonly",
            type = "mesh3d"
            ) %>%


            # Adds the mesh
            add_trace(
            x = x, y = y, z = z,
            i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
            facecolor = facecolor,
            showscale=FALSE,
            type = "mesh3d"
            )





            share|improve this answer




























              0














              After a lot of hacking, I finally have a working solution.
              In this case, by plotting a mesh3d area, with points that are "inside" the helicopter and will not be visible later, and then plot the actual helicopter later.



              It seems that "visible='legendonly'" does not apply to mesh3d, as this option removes both the plot and legend.



              library(plotly)
              library(geomorph)

              plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
              dest <- basename(plyFile)
              if (!file.exists(dest)) {
              download.file(plyFile, dest)
              }

              mesh <- read.ply(dest, ShowSpecimen = F)
              # see getS3method("shade3d", "mesh3d") for details on how to plot

              # plot point cloud
              x <- mesh$vb["xpts",]
              y <- mesh$vb["ypts",]
              z <- mesh$vb["zpts",]
              m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

              # now figure out the colormap
              zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

              # Get colors you want
              cols = brewer_pal(palette="RdBu")(9)

              # Ramp to add to facecolor
              library(scales)
              facecolor = colour_ramp(cols)(rescale(x=zmean))

              # Create data.frame of colours and breakpoints.
              # Must go from 0 to 1, plotly scales it based on values it self.
              colz = data.frame(seq(0,1,length.out = length(cols)),
              cols)

              # Make stupid pointcloud to fool the colorbar
              xx = c(min(x), max(x))
              yy = c(min(y), max(y))
              zz = c(min(z), max(z))

              plot_ly() %>%

              # Creates the legend, and also the plotting space
              add_trace(
              x = xx, y = yy, z = zz,
              intensity = x,
              colorscale = colz,
              # visible="legendonly",
              type = "mesh3d"
              ) %>%


              # Adds the mesh
              add_trace(
              x = x, y = y, z = z,
              i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
              facecolor = facecolor,
              showscale=FALSE,
              type = "mesh3d"
              )





              share|improve this answer


























                0












                0








                0







                After a lot of hacking, I finally have a working solution.
                In this case, by plotting a mesh3d area, with points that are "inside" the helicopter and will not be visible later, and then plot the actual helicopter later.



                It seems that "visible='legendonly'" does not apply to mesh3d, as this option removes both the plot and legend.



                library(plotly)
                library(geomorph)

                plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
                dest <- basename(plyFile)
                if (!file.exists(dest)) {
                download.file(plyFile, dest)
                }

                mesh <- read.ply(dest, ShowSpecimen = F)
                # see getS3method("shade3d", "mesh3d") for details on how to plot

                # plot point cloud
                x <- mesh$vb["xpts",]
                y <- mesh$vb["ypts",]
                z <- mesh$vb["zpts",]
                m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

                # now figure out the colormap
                zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

                # Get colors you want
                cols = brewer_pal(palette="RdBu")(9)

                # Ramp to add to facecolor
                library(scales)
                facecolor = colour_ramp(cols)(rescale(x=zmean))

                # Create data.frame of colours and breakpoints.
                # Must go from 0 to 1, plotly scales it based on values it self.
                colz = data.frame(seq(0,1,length.out = length(cols)),
                cols)

                # Make stupid pointcloud to fool the colorbar
                xx = c(min(x), max(x))
                yy = c(min(y), max(y))
                zz = c(min(z), max(z))

                plot_ly() %>%

                # Creates the legend, and also the plotting space
                add_trace(
                x = xx, y = yy, z = zz,
                intensity = x,
                colorscale = colz,
                # visible="legendonly",
                type = "mesh3d"
                ) %>%


                # Adds the mesh
                add_trace(
                x = x, y = y, z = z,
                i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
                facecolor = facecolor,
                showscale=FALSE,
                type = "mesh3d"
                )





                share|improve this answer













                After a lot of hacking, I finally have a working solution.
                In this case, by plotting a mesh3d area, with points that are "inside" the helicopter and will not be visible later, and then plot the actual helicopter later.



                It seems that "visible='legendonly'" does not apply to mesh3d, as this option removes both the plot and legend.



                library(plotly)
                library(geomorph)

                plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
                dest <- basename(plyFile)
                if (!file.exists(dest)) {
                download.file(plyFile, dest)
                }

                mesh <- read.ply(dest, ShowSpecimen = F)
                # see getS3method("shade3d", "mesh3d") for details on how to plot

                # plot point cloud
                x <- mesh$vb["xpts",]
                y <- mesh$vb["ypts",]
                z <- mesh$vb["zpts",]
                m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

                # now figure out the colormap
                zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

                # Get colors you want
                cols = brewer_pal(palette="RdBu")(9)

                # Ramp to add to facecolor
                library(scales)
                facecolor = colour_ramp(cols)(rescale(x=zmean))

                # Create data.frame of colours and breakpoints.
                # Must go from 0 to 1, plotly scales it based on values it self.
                colz = data.frame(seq(0,1,length.out = length(cols)),
                cols)

                # Make stupid pointcloud to fool the colorbar
                xx = c(min(x), max(x))
                yy = c(min(y), max(y))
                zz = c(min(z), max(z))

                plot_ly() %>%

                # Creates the legend, and also the plotting space
                add_trace(
                x = xx, y = yy, z = zz,
                intensity = x,
                colorscale = colz,
                # visible="legendonly",
                type = "mesh3d"
                ) %>%


                # Adds the mesh
                add_trace(
                x = x, y = y, z = z,
                i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
                facecolor = facecolor,
                showscale=FALSE,
                type = "mesh3d"
                )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 15:14









                Athanasia MowinckelAthanasia Mowinckel

                907




                907






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285427%2fplotly-adding-colorbar-to-mesh3d-without-using-intensity-using-facecolor%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation