gpt4 book ai didi

r - "zero-length arrow"有多小?

转载 作者:行者123 更新时间:2023-12-02 09:46:41 25 4
gpt4 key购买 nike

如果您经常使用 arrows 函数,您可能会遇到此警告:

set.seed(438520)
N = 1000
x = rnorm(N, sd = .1)
y = rnorm(N)

png('~/Desktop/arrows.png', height = 240, width = 240)
plot(NA, xlim = c(-1, 1), ylim = c(-3, 3))
arrows(x[-N], y[-N], x[-1L], y[-1L])
dev.off()

Warning message:In arrows(x[-N], y[-N], x[-1L], y[-1L]) :zero-length arrow is of indeterminate angle and so skipped

A cluttered plot showing the arrows; the x axis is labeled "Index" and the y axis is labeled "NA", and the points are in a vertically-oriented ellipse.

我们如何确定哪些箭头有问题,以便按照我们认为合适的方式处理它们?

最佳答案

答案在?arrows中暗示:

The direction of a zero-length arrow is indeterminate, and hence so is the direction of the arrowheads. To allow for rounding error, arrowheads are omitted (with a warning) on any arrow of length less than 1/1000 inch.

这当然引出了一个问题——What is an inch?

该问答围绕相关问题展开,但所学到的知识也可以应用于此处:

png('~/Desktop/arrows.png', height = 240, width = 240)
plot(NA, xlim = c(-1, 1), ylim = c(-3, 3))

# get each arrow's length by converting x and y coords to inches
units = par(c('usr', 'pin'))
x_to_inches = with(units, pin[1L]/diff(usr[1:2]))
y_to_inches = with(units, pin[2L]/diff(usr[3:4]))

dists = sqrt((x_to_inches * diff(x))**2 + (y_to_inches * diff(y))**2)

# which arrows are the culprits?
idx = which(dists < .001)

# option: remove the arrow base & head from the culprit pair(s)
arrows(x[-c(N, idx)], y[-c(N, idx)],
x[-c(1L, idx + 1L)], y[-c(1L, idx + 1L)])
dev.off()

您可以看到here在 R 源代码中,这种方法几乎与最初用于生成此警告的方法(在 C 级别)相同。

有些事情一直困扰着我,但又不足以让我坐下来好好讨论一下。

关于r - "zero-length arrow"有多小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com