将等高线与等高线填充图对齐不规则网格极坐标图(半圆)

2024-01-19

我看到有几个人回答了使用不规则网格绘图的问题。我无法使轮廓线与填充轮廓对齐。此外,需要在绘图上显示数据点位置,以及以 30 度增量显示的径向辐条,以及在 10、20 30 处显示的半圆。

Ref: 在不规则网格上绘制等高线 https://stackoverflow.com/questions/19339296/plotting-contours-on-an-irregular-grid

heading=seq(0,180,30)
speed=c(5,10,15,20,30)
mheading=matrix(heading,ncol=length(heading),nrow=length(speed),byrow=TRUE)
mspeed=matrix(speed,ncol=length(heading),nrow=length(speed),byrow=FALSE)
mag=mheading+mspeed

x=sin(mheading*pi/180)*mspeed
y=cos(mheading*pi/180)*mspeed
z=mag

library(akima)
df<-data.frame(x=x,y=y,z=z)

# interpolation
fld <- with(df, interp(x = x, y = y, z = z,
                       xo=seq(min(x),max(x),length=100),
                       yo=seq(min(y),max(y),length=100)))

filled.contour(x = fld$x,
               y = fld$y,
               z = fld$z,
               color.palette =
                 colorRampPalette(c("white", "blue")),
               xlab = "",
               ylab = "",
               main = "Max",
               key.title = title(main = "Value", cex.main = 1),
               asp=1,xlim=c(0,40),ylim=c(-30,30))

contour(x = fld$x,
               y = fld$y,
               z = fld$z,
               color.palette =
                 colorRampPalette(c("white", "blue")),
               xlab = "",
               ylab = "",
               asp=1,xlim=c(0,40),ylim=c(-30,30), add=TRUE)

按照此link https://stackoverflow.com/questions/10856882/r-interpolated-polar-contour-plot,产生以下代码/图。这是“更好”,但仍然存在问题。为什么在最小速度半径 (5) 内部存在插值数据,以及为什么轮廓填充/线没有延伸到外半径,尤其是在 90 度附近?

contours=TRUE   # Add contours to the plotted surface
legend=TRUE        # Plot a surface data legend?
axes=TRUE      # Plot axes?
points=TRUE        # Plot individual data points
extrapolate=FALSE # Should we extrapolate outside data points?
single_point_overlay=0
outer.radius=30
spatial_res=1000 #Resolution of fitted surface
interp.type = 1
circle.rads <- pretty(c(0,outer.radius))

heading=seq(0,180,30)
speed=c(5,10,15,20,30)
mheading=matrix(heading,ncol=length(heading),nrow=length(speed),byrow=TRUE)
mspeed=matrix(speed,ncol=length(heading),nrow=length(speed),byrow=FALSE)
mag=mheading+mspeed

x=sin(mheading*pi/180)*mspeed
y=cos(mheading*pi/180)*mspeed
z=mag
extrapolate=FALSE # Should we extrapolate outside data points?

contour_levels = 8
col_levels=contour_levels
col_breaks_source=1
contour_breaks_source = 1
col = rev(heat.colors(col_levels))

minitics <- seq(-outer.radius, outer.radius, length.out = spatial_res)
xmini <- seq(min(x),max(x),length=spatial_res)
ymini <- seq(min(y),max(y),length=spatial_res)
# interpolate the data
if (interp.type ==1 ){
  #   Interp <- akima:::interp(x = x, y = y, z = z, 
  #                            extrap = extrapolate, 
  #                            xo = xmini, 
  #                            yo = ymini, 
  #                            linear = FALSE)
  #   Mat <- Interp[[3]]
  df<-data.frame(x=x,y=y,z=z)

  # interpolation
  fld <- with(df, akima:::interp(x = x, y = y, z = z,
                                 xo=xmini,
                                 yo=ymini))


  Mat_x <- fld[[1]]
  Mat_y <- fld[[2]]
  Mat_z <- fld[[3]]

} else if (interp.type == 2){
  library(fields)
  grid.list = list(x=minitics,y=minitics)
  t = Tps(cbind(x,y),z,lambda=lambda)
  tmp = predict.surface(t,grid.list,extrap=extrapolate)
  Mat_z = tmp$z
  # mark cells outside circle as NA
  markNA <- matrix(minitics, ncol = spatial_res, nrow = spatial_res) 
  Mat_x <- markNA
  Mat_y <- t(markNA)
} else {stop("interp.type value not valid")}


# 
Mat_z[!(sqrt(Mat_x ^ 2 + Mat_y ^ 2) <= max(speed)*1.1)] <- NA 
Mat_z[!(sqrt(Mat_x ^ 2 + Mat_y ^ 2) >= min(speed))] <- NA   # <- SHOULD REMOVE INNER DATA

### Set contour_breaks based on requested source
if ((length(contour_breaks_source == 1)) & (contour_breaks_source[1] == 1)){
#   contour_breaks = seq(min(z,na.rm=TRUE),max(z,na.rm=TRUE),
#                        by=(max(z,na.rm=TRUE)-min(z,na.rm=TRUE))/(contour_levels-1))
  contour_breaks = seq(min(z,na.rm=TRUE),max(z,na.rm=TRUE),length.out = contour_levels+1)
}else if ((length(contour_breaks_source == 1)) & (contour_breaks_source[1] == 2)){
  contour_breaks = seq(min(Mat_z,na.rm=TRUE),max(Mat_z,na.rm=TRUE),
                       by=(max(Mat_z,na.rm=TRUE)-min(Mat_z,na.rm=TRUE))/(contour_levels-1))
} else if ((length(contour_breaks_source) == 2) & (is.numeric(contour_breaks_source))){
  contour_breaks = pretty(contour_breaks_source,n=contour_levels)
  contour_breaks = seq(contour_breaks_source[1],contour_breaks_source[2],
                       by=(contour_breaks_source[2]-contour_breaks_source[1])/(contour_levels-1))
}else {stop("Invalid selection for \"contour_breaks_source\"")}

### Set color breaks based on requested source
if ((length(col_breaks_source) == 1) & (col_breaks_source[1] == 1))
{zlim=c(min(z,na.rm=TRUE),max(z,na.rm=TRUE))} else if ((length(col_breaks_source) == 1) & (col_breaks_source[1] == 2))
{zlim=c(min(Mat_z,na.rm=TRUE),max(Mat_z,na.rm=TRUE))} else if ((length(col_breaks_source) == 2) & (is.numeric(col_breaks_source)))
{zlim=col_breaks_source} else {stop("Invalid selection for \"col_breaks_source\"")}

# begin plot
Mat_plot = Mat_z
Mat_plot[which(Mat_plot<zlim[1])]=zlim[1]
Mat_plot[which(Mat_plot>zlim[2])]=zlim[2]
image(x = Mat_x, y = Mat_y, Mat_plot , 
      useRaster = TRUE, asp = 1, axes = FALSE, xlab = "", ylab = "", zlim = zlim, col = col)

# add contours if desired
if (contours){
  CL <- contourLines(x = Mat_x, y = Mat_y, Mat_z, levels = contour_breaks)
  A <- lapply(CL, function(xy){
    lines(xy$x, xy$y, col = gray(.2), lwd = .5, asp=1)
  })
}
# add interpolated point if desired
if (points){
  points(x,y,pch=4)
}
# add overlay point (used for trained image marking) if desired
if (single_point_overlay!=0){
  points(x[single_point_overlay],y[single_point_overlay],pch=0)
}

# add radial axes if desired
if (axes){ 
  # internals for axis markup
  RMat <- function(radians){
    matrix(c(cos(radians), sin(radians), -sin(radians), cos(radians)), ncol = 2)
    # matrix(c(sin(radians), -cos(radians), cos(radians), sin(radians)), ncol = 2)
  }    

  circle <- function(x, y, rad = 1, nvert = 500, angle=360){
    rads <- seq(0,angle*pi/180,length.out = nvert)
    #     xcoords <- cos(rads) * rad + x
    #     ycoords <- sin(rads) * rad + y
    xcoords=sin(rads)*rad + x
    ycoords=cos(rads)*rad + y
    cbind(xcoords, ycoords)
  }

  # draw circles
  if (missing(circle.rads)){
    circle.rads <- pretty(c(0,outer.radius))
  }

  endAngle = 180
  for (i in circle.rads){
    lines(circle(0, 0, i, angle = endAngle), col = "#66666650")
  }

  # put on radial spoke axes:

  axis.degs <- c(0, 30, 60, 90, 120, 150)
  # axis.rads <- c(0, pi / 6, pi / 3, pi / 2, 2 * pi / 3, 5 * pi / 6)
  axis.rads <- axis.degs * pi/180
  r.labs <- c(90, 60, 30, 0, 330, 300)
  l.labs <- c(270, 240, 210, 180, 150, 120)

  for (i in 1:length(axis.rads)){ 
    if (axis.degs[i]==0) {
      # endpoints <- zapsmall(c(RMat(axis.rads[i]) %*% matrix(c(1, 0, -1, 0) * outer.radius,ncol = 2)))
      endpoints <- zapsmall(c(RMat(axis.rads[i]) %*% matrix(c(1, 0, 0, 0) * outer.radius,ncol = 2)))
    } else if (0 < axis.degs[i] & axis.degs[i] < 90) {
      endpoints <- zapsmall(c(RMat(axis.rads[i]) %*% matrix(c(1, 0, 0, 0) * outer.radius,ncol = 2)))
    } else {
      endpoints <- zapsmall(c(RMat(axis.rads[i]) %*% matrix(c(0, 0, -1, 0) * outer.radius,ncol = 2)))
    }
    segments(endpoints[1], endpoints[2], endpoints[3], endpoints[4], col = "#66666650")
    endpoints <- c(RMat(axis.rads[i]) %*% matrix(c(1.1, 0, -1.1, 0) * outer.radius, ncol = 2))
    lab1 <- bquote(.(r.labs[i]) * degree)
    lab2 <- bquote(.(l.labs[i]) * degree)
    if (0 <= r.labs[i] & r.labs[i] <= 180) text(endpoints[1], endpoints[2], lab1, xpd = TRUE)
    if (0 <= l.labs[i] & l.labs[i] <= 180) text(endpoints[3], endpoints[4], lab2, xpd = TRUE)
  }

  # axis(2, pos = -1.25 * outer.radius, at = sort(union(circle.rads,-circle.rads)), labels = NA)
  # text( -1.26 * outer.radius, sort(union(circle.rads, -circle.rads)),sort(union(circle.rads, -circle.rads)), xpd = TRUE, pos = 2)
  axis(2, pos = 0 * outer.radius, at = sort(union(circle.rads,-circle.rads)), labels = NA)
  text( -0.02 * outer.radius, sort(union(circle.rads, -circle.rads)),
        abs(sort(union(circle.rads, -circle.rads))), 
        xpd = TRUE, pos = 2)
}

# add legend if desired
# this could be sloppy if there are lots of breaks, and that's why it's optional.
# another option would be to use fields:::image.plot(), using only the legend. 
# There's an example for how to do so in its documentation
if (legend){
  library(fields)
  image.plot(legend.only=TRUE, smallplot=c(.78,.82,.1,.8), col=col, zlim=zlim)
  # ylevs <- seq(-outer.radius, outer.radius, length = contour_levels+ 1)
  # #ylevs <- seq(-outer.radius, outer.radius, length = length(contour_breaks))
  # rect(1.2 * outer.radius, ylevs[1:(length(ylevs) - 1)], 1.3 * outer.radius, ylevs[2:length(ylevs)], col = col, border = NA, xpd = TRUE)
  # rect(1.2 * outer.radius, min(ylevs), 1.3 * outer.radius, max(ylevs), border = "#66666650", xpd = TRUE)
  # text(1.3 * outer.radius, ylevs[seq(1,length(ylevs),length.out=length(contour_breaks))],round(contour_breaks, 1), pos = 4, xpd = TRUE)
}

轮廓和颜色不对齐,因为filled.contour 生成两个图(图例和轮廓)。绘图后,这些坐标系就会丢失。 (?filled.contour)。可以通过添加相关命令来解决plot.axes争论。可以用以下方法绘制半圆draw.arc来自plotrix封装,辐条segments。最小半径内的区域可以用白色线段覆盖来表示no data.

# min distance of contours lines from center
min_dist=5

# position of spokes (degrees)
spk = seq(0,180,30)

filled.contour(x = fld$x,
               y = fld$y,
               z = fld$z,
               color.palette = colorRampPalette(c("white", "blue")),
               xlab = "",
               ylab = "",
               main = "Max",
               key.title = title(main = "Value", cex.main = 1),
               asp=1, xlim=c(0,40), ylim=c(-30,30),   frame.plot=F,
               plot.axes = {contour(fld$x, fld$y, fld$z , add=T, levels = seq(0,max(fld$z, na.rm=T),30), drawlabels=F, col=2);
                            # semi circles
                            draw.arc(x=0,y=0,radius = (1:3)*10, deg1=90, deg2=-90, col='grey');
                            # cover zone within minimum radius with (draw many closely spaced white lines
                            segments(x0 = 0, y0 = 0, x1 = sin((0:180)*pi/180)*min_dist, y1 = cos((0:180)*pi/180)*min_dist, col='white');
                            # spokes with labels
                            segments(x0 = 0, y0 = 0, x1 = sin(spk*pi/180)*30, y1 = cos(spk*pi/180)*30, col='grey');
                            text(x = sin(spk*pi/180)*30, y=cos(spk*pi/180)*30, labels = spk, pos=4, cex=0.6, xpd=NA)
                            # data points
                            points(x,y, pch=16, cex=0.6);
                            # x axis
                            axis(1);
                            # modified y axis
                            axis(2, at = axisTicks(range(y), log=F), labels = abs(axisTicks(range(y), log=F)), pos = 0);
               }
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将等高线与等高线填充图对齐不规则网格极坐标图(半圆) 的相关文章

随机推荐

  • Swift 对具有多个排序标准的数组进行排序[重复]

    这个问题在这里已经有答案了 Swift 中如何按多个条件对数组进行排序 例如 字典数组如下所示 items item itemA status 0 category B item itemB status 1 category C item
  • 使用自定义图标进行 Google 地图标记定位

    我遇到一个问题 带有自定义图标的标记似乎显示在地图上略有不同的点 具体取决于缩放级别 我以前使用过带有自定义图标的标记 所以我不知道我做错了什么 您会看到 最初标记看起来像是位于道路上 但如果缩小两次 它看起来就像在上面 如果放大两次 它看
  • GAE Blobstore 类文件 API 弃用时间表(py 2.7 运行时)

    这是 App Engine 团队的问题 上周我们意识到 App Engine 团队已将用于写入和读取 blobstore 的类文件 API 标记为已弃用 并且将来可能会被删除 我们有相当多的基础设施依赖于该 API 现在我们需要移植到他们建
  • 在python 3.6中导入tensorflow时出现非法指令:4

    我安装了macOS 上的张量流 https www tensorflow org install install mac使用 Virtualenv 一切顺利 成功安装了 6 1 11 0 tensorflow 1 6 0 是终端的最后一个输
  • 尝试呈现一个 UIViewController,其视图不在窗口层次结构中

    我有一个具有以下层次结构的多视图应用程序 启动 gt 导航控制器 gt 表视图控制器 gt 设置视图控制器 Splash 是应用程序入口点 因此成为根视图控制器 当我尝试通过设置视图控制器上的操作将图块添加到带区时 我收到调试器警告 app
  • 传单 GeoJSON 显示

    我遇到了一个任务 需要使用 leaflet js 库和 geojson 作为数据存储 几乎立即 遇到了以下问题 从 geojson 对象创建的多边形不显示在地图上 而由本机传单方法创建的多边形则完美地工作 这是我的代码 var map ne
  • 自编译 Roslyn 构建性能:不如最初发布的 Roslyn 版本快

    用一句话来形容我在做什么 检查分支Update 1来自罗斯林 github 存储库 https github com dotnet roslyn git 构建 csc exe 并使用我刚刚自己构建的 csc exe 版本编译随机解决方案 预
  • Pandas 的 ValueError - 传递值的形状

    我正在尝试使用 Pandas 和 PyODBC 从 SQL Server 视图中提取内容并将内容转储到 Excel 文件中 但是 在转储数据帧时出现错误 我可以打印列和数据帧内容 ValueError Shape of passed val
  • Eclipse 中的调试:无断点的变量快照

    我正在 Eclipse 中调试 Java 程序 我想观察一个特定的变量 但是 由于我的程序使用 GUI 因此创建断点会导致窗口冻结 例如 这尤其令人烦恼 尝试右键单击某个项目并导航上下文菜单 我实际上并不想停止程序 我只是想观察一个特定的变
  • 页面加载时使用 AJAX 不是一件坏事吗?

    我在上面看到过这个书呆子晚餐 http nerddinner codeplex com和其他网站 页面加载时 在 JavaScript 中 通过浏览器 将发出 AJAX 请求 从呈现初始页面的同一服务器获取一些数据 数据会很小 并且不存在会
  • AnyDac 又名 FireDac 无法生成更新查询

    我已经使用 UniDac 很长时间了 决定转向 FireDac 因为它具有良好的异步方法 在继续使用后 我发现我的数据编辑不再起作用 它给了我一个错误 FireDAC 物理 330 无法生成更新查询 更新表未定义 我在这里想做的是我有一个
  • Spring单例和Java单例(设计模式)有什么区别? [复制]

    这个问题在这里已经有答案了 我正在学习 Spring 框架 目前正在阅读一本关于它的书 这本书里说Spring单例与Java单例不同 这意味着什么以及有什么区别 谢谢 Java 单例的作用域是 Java 类加载器 Spring 单例的作用域
  • 严重:泄漏:在垃圾收集之前未调用 ByteBuf.release()。内蒂

    我已经创建了一些游戏服务器 并且刚刚与大约 10 个伙伴进行了测试 一切都很顺利 我们玩了大约 10 分钟 在游戏的某个时刻 游戏服务器停止为客户端提供服务 断开了每个人的连接 而且我连接到运行游戏的 VPS 的 SSH 也断开了连接 我不
  • Excel VBA 函数检查文件名是否包含该值

    我需要一个可以输出如下内容的公式 如果特定文件夹中的文件名包含 因为文件名在 字符后会有一个附加字符串 因此如果文件名包含单元格值 AB1 则在单元格 AC1 中添加完整文件名 用VBA可以实现吗 非常感谢 这在VBA中可行吗 当然 这是我
  • 如何反序列化 ArrayObject

    Note 我尝试过使用反序列化函数 但它说第一个参数应该是字符串而不是对象 所以我的问题 Q 如何在 php 中反序列化 ArrayObject 我的 ArrayObject 包含 O 11 ArrayObject 2 s 14 shopp
  • 将 WebKit 用于桌面应用程序 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • UIImage:调整大小,然后裁剪

    几天来 我一直在努力解决这个问题 尽管我一直觉得自己正处于启示的边缘 但我根本无法实现我的目标 在设计的概念阶段之前 我认为从 iPhone 的相机或库中抓取图像 使用相当于宽高比填充UIImageView 的选项 完全在代码中 然后cro
  • 是什么导致我的 WP7 应用程序崩溃?

    我在模拟器和手机本身上都发生过一些无法解释的崩溃 基本上 当我的应用程序崩溃时 我不会看到任何对话框 并且手机会返回主屏幕 我有以下代码来显示消息框 但这在某种程度上被绕过了 Code to execute if a navigation
  • 明星算法:距离启发式

    我正在使用 A 星算法 如此处所示 取自http code activestate com recipes 578919 python a pathfinding with binary heap http code activestate
  • 将等高线与等高线填充图对齐不规则网格极坐标图(半圆)

    我看到有几个人回答了使用不规则网格绘图的问题 我无法使轮廓线与填充轮廓对齐 此外 需要在绘图上显示数据点位置 以及以 30 度增量显示的径向辐条 以及在 10 20 30 处显示的半圆 Ref 在不规则网格上绘制等高线 https stac