无法在 Julia 中绘制

Can't plot in Julia

我目前正在阅读一本书 (BRML),其中有一个用 Julia 编写的演示(地震演示,练习 1.22)。我以前从未使用过 Julia(尽管相当广泛地使用过 Python 和其他语言)所以我是一个完全的菜鸟。

下面代码中 plot(x,y,".") 行的具体作用是什么:

Pkg.add("Pkg")
using Pkg
Pkg.add("PyPlot")

S=5000 # number of points on the spiral
x=zeros(S); y=zeros(S)
for s=1:S
    theta=50*2*pi*s/S;  r=s/S
    x[s]=r*cos(theta); y[s]=r*sin(theta)
end
plot(x,y,".")

我理解在那之前所做的一切,但是我不确定那条特定的线是做什么的。我自己看不到的原因是当我试图在网上 运行 它时 Julia compiler,我收到以下错误:

INFO: Initializing package repository /home/cg/root/4655378/.julia/v0.6
INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl
ERROR: LoadError: GitError(Code:ERROR, Class:Net, curl error: Could not resolve host: github.com
)
Stacktrace:
 [1] macro expansion at ./libgit2/error.jl:99 [inlined]
 [2] clone(::String, ::String, ::Base.LibGit2.CloneOptions) at ./libgit2/repository.jl:276
 [3] #clone#100(::String, ::Bool, ::Ptr{Void}, ::Nullable{Base.LibGit2.AbstractCredentials}, ::Function, ::String, ::String) at ./libgit2/libgit2.jl:562
 [4] (::Base.LibGit2.#kw##clone)(::Array{Any,1}, ::Base.LibGit2.#clone, ::String, ::String) at ./<missing>:0
 [5] (::Base.Pkg.Dir.##8#10{String,String})() at ./pkg/dir.jl:55
 [6] cd(::Base.Pkg.Dir.##8#10{String,String}, ::String) at ./file.jl:70
 [7] init(::String, ::String) at ./pkg/dir.jl:53
 [8] #cd#1(::Array{Any,1}, ::Function, ::Function, ::String, ::Vararg{String,N} where N) at ./pkg/dir.jl:28
 [9] add(::String) at ./pkg/pkg.jl:117
while loading /home/cg/root/4655378/main.jl, in expression starting on line 1

如第三行所示,本书使用了 PyPlot 包,它基本上是 Julia wrapper around Python 的 pyplot.

所以,我们可以参考 pyplot's documentation to figure out how that line of code works. But as mentioned in that page, pyplot is trying to emulate MATLAB's plot function, and for this case their help page 更容易导航。正如那里提到的,

plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.

plot(X,Y,LineSpec) 还“使用指定的线条样式、标记和颜色创建绘图。”单击 LineSpec,我们可以在第二个 table 中看到 '.' 是标记之一,带有描述 Point 并且生成的标记是一个黑色实心点。因此 plot(x,y,".") 在 x 和 y 坐标指定的点处创建一个以点作为标记的图。

我们也可以尝试其他标记之一,例如。 plot(x,y,"+") 创建这个:

如果你仔细看,你会发现这些点是由 + 符号标记的。