如何在 Julia 0.5 中绘制图表?

How to Plot Graphs in Julia 0.5?

我正在通过 Chris Rackauckas' Introduction 学习 Julia,我遇到了一项需要我绘制一些数据的任务。我无法导入 Plots 模块,所以我尝试了一个简单的测试:

using Plots
x = 1:10
y = 0.5*x + 3
plot(x, y)

当我第一次使用 Juno 运行 这段代码时 IDE 我得到一个错误:

LoadError: LoadError: LoadError: syntax: unhandled expr (error #<julia: Main.Base.MethodError(f=FixedPointNumbers.#floattype(), args=(Main.FixedPointNumbers.FixedPoint{UInt8, 8},))>)
in include_from_node1(::String) at .\loading.jl:488 (repeats 2 times)
in eval(::Module, ::Any) at .\boot.jl:234
in require(::Symbol) at .\loading.jl:415
in include_string(::String, ::String) at .\loading.jl:441
in include_string(::Module, ::String, ::String) at 2

这是指我代码段中的 using 语句。当我从 REPL 运行 时,不会出现此错误。版本信息如下:

朱莉娅版本 0.5.0
提交 3c9d753(2016-09-19 18:14 UTC)
平台信息:
系统:NT (x86_64-w64-mingw32)
CPU:英特尔(R)酷睿(TM)i7-4810MQ CPU @ 2.80GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK:libopenblas64_
LIBM:libopenlibm
LLVM:libLLVM-3.7.1(ORCJIT,haswell)

我目前安装了 0.10.3 版的 Plots。

如果您通过共享 versioninfo() 的输出来提供一些 version/platform 信息,可以更好地帮助您。

例如下面的摘录

Pkg.add("Plots")
using Plots
plotly() # this backend is installed by default
x = 1:10
y = 0.5*x + 3
plot(x, y)

下工作良好
Julia Version 0.5.0
Commit 3c9d753* (2016-09-19 18:14 UTC)
Platform Info:
  System: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (NO_LAPACK NO_LAPACKE NO_AFFINITY SANDYBRIDGE)
  LAPACK: liblapack
  LIBM: libm
  LLVM: libLLVM-3.7.1 (ORCJIT, ivybridge)

也许你应该考虑 Pkg.add("PyPlot") 或类似的后端,稍后再试?

要使用 Atom IDE 中的 Juno 包使用 运行 文件命令,必须将绘图分配给变量并传递给 display 函数。

using Plots
pyplot()
x = 1:100
y = 0.5*x + 10
println(y)
graph = plot(x, y)
display(graph)

这将在 Juno's Plots window 中显示图表。在评论中,Arda Aytekin 建议可以使用 pyplot(display=true)graph = plot(x, y, display=true),这导致图表显示在单独的 pyplot window.