【问题标题】:Visualising an individual 2d graph for all points on a plane可视化平面上所有点的单个二维图
【发布时间】:2017-11-13 05:43:59
【问题描述】:

对于参数 P 和 Q 的给定值,我有一条 M 与 N 曲线(为了便于理解,我们将其视为 sigmoid)。我需要可视化一系列 P 值的 M 与 N 曲线和 Q(假设 0 到 1 中有 10 个值,即 P 和 Q 均为 0.1、0.2、...、0.9)

我为这个问题找到的唯一解决方案是格子图(本质上是图矩阵)。我想知道除了格子图之外是否还有其他方法可以可视化这种 4d(?)关系。谢谢。

【问题讨论】:

    标签: python r matplotlib data-visualization


    【解决方案1】:

    我不确定我是否理解您的期望,所以请告诉我这是否在正确的轨道上。下面是三个使用 R 的例子。

    第一个确实是一个图矩阵,其中每个面板代表q 的不同值,并且在每个面板中,每条曲线代表p 的不同值。第二个是 3D 图,它基于三个变量查看表面,第四个变量是固定的。第三个是一个闪亮的应用程序,它创建与第二个示例相同的交互式绘图,但还提供一个滑块,允许您更改 p 并查看绘图如何变化。不幸的是,我不确定如何在 Stackoverflow 中嵌入交互式绘图,所以我只是提供了代码。

    我不确定是否有一种优雅的方式可以同时查看所有四个变量,但也许有人会提供其他选项。

    pq 的各种值的图矩阵

    library(tidyverse)
    theme_set(theme_classic())
    
    # Function to plot
    my_fun = function(x, p, q) {
      1/(1 + exp(p + q*x))
    }
    
    # Parameters
    params = expand.grid(p=seq(-2,2,length=6), q=seq(-1,1,length=11))
    
    # x-values to feed to my_fun
    x = seq(-10,10,0.1)
    
    # Generate data frame for plotting
    dat = map2_df(params$p, params$q, function(p, q) {
      data.frame(p=p, q=q, x, y=my_fun(x, p, q))
    })
    
    ggplot(dat, aes(x,y,colour=p, group=p)) +
      geom_line() +
      facet_grid(. ~ q, labeller=label_both) +
      labs(colour="p") +
      scale_colour_gradient(low="red", high="blue") +
      theme(legend.position="bottom")
    

    固定一个变量的 3D 图

    下面的代码将生成一个交互式 3D 图,您可以缩放和旋转。我已经修复了p 的值,并为xq 值的网格绘制了y 曲面图。

    library(rgl)
    
    x = seq(-10,10,0.1)
    q = seq(-1,1,0.01)
    y = outer(x, q, function(a, b) 1/(1 + exp(1 + b*a)))
    
    persp3d(x, q, y, col=hcl(240,80,65), specular="grey20",
            xlab = "x", ylab = "q", zlab = "y")
    

    我不确定如何嵌入交互式绘图,但这是一个视角的静态图像:

    闪亮的应用

    下面的代码将创建与上面相同的图,但增加了使用滑块改变p 并查看图如何变化的能力。

    打开一个 R 脚本文件并粘贴下面的代码。将其另存为 app.r 在其自己的目录中,然后运行代码。 rgl 窗口和带有用于控制 p 值的滑块的 Shiny 应用程序页面都应该打开。根据需要调整窗口大小,然后移动滑块以查看函数表面如何随着 p 的各种值而变化。

    library(shiny)
    
    # Define UI for application that draws an interactive plot
    ui <- fluidPage(
    
       # Application title
       titlePanel("Plot the function 1/(1 + exp(p + q*x))"),
    
       # Sidebar with a slider input for number of bins 
       sidebarLayout(
          sidebarPanel(
             sliderInput("p",
                         "Vary the value of p and see how the plot changes",
                         min = -2,
                         max = 2,
                         value = 1,
                         step=0.2)
          ),
    
          # Show a plot of the generated distribution
          mainPanel(
             plotOutput("distPlot")
          )
       )
    )
    
    # Define server logic required to draw the plot
    server <- function(input, output) {
    
       output$distPlot <- renderPlot({
    
         library(rgl)
    
         x = seq(-10,10,0.1)
         q = seq(-1,1,0.01)
         y = outer(x, q, function(a, b) 1/(1 + exp(input$p + b*a)))
    
         persp3d(x, q, y, col=hcl(240,50,65), specular="grey20",
                 xlab = "x", ylab = "q", zlab = "y")
    
       })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 2020-12-17
      • 1970-01-01
      • 2011-05-12
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多