Julia 编程 - 使用图形
本章讨论如何使用 Julia 中的各种工具对数据进行绘图、可视化和执行其他(图形)操作。
使用 Cairo 进行文本绘制
Cairo 是一个 2D 图形库,为显示系统实现了设备上下文。它适用于 Linux、Windows、OS X,还可以创建 PDF、PostScript 和 SVG 格式的磁盘文件。Cairo 的 Julia 文件(即 Cairo.jl)对其 C API 来说是真实的。
例子
以下是画线的示例 -
首先,我们将创建一个 cr 上下文。
julia> using Cairo julia> img = CairoRGBSurface(512, 128); julia> img = CairoRGBSurface(512, 128); julia> cr = CairoContext(img); julia> save(cr);
现在,我们将添加一个矩形 -
julia> set_source_rgb(cr, 0.5, 0.5, 0.5); julia> rectangle(cr, 0.0, 0.0, 512.0, 128.0); julia> fill(cr); julia> restore(cr); julia> save(cr);
现在,我们将定义这些点并通过这些点画一条线 -
julia> x0=61.2; y0=74.0; julia> x1=214.8; y1=125.4; julia> x2=317.2; y2=22.8; julia> x3=470.8; y3=74.0; julia> move_to(cr, x0, y0); julia> curve_to(cr, x1, y1, x2, y2, x3, y3); julia> set_line_width(cr, 10.0); julia> stroke_preserve(cr); julia> restore(cr);
最后,将生成的图形写入磁盘 -
julia> move_to(cr, 12.0, 12.0); julia> set_source_rgb(cr, 0, 0, 0); julia> show_text(cr,"Line_Figure") julia> write_to_png(c,"Line_Figure.png");
输出
 
与 Winston 一起绘制文本
Winston 也是一个 2D 图形库。它类似于 MATLAB 中可用的内置图形。
例子
julia> x = range(0, stop=3pi, length=100);
julia> c = cos.(x);
julia> s = sin.(x);
julia> p = FramedPlot(
                        title="Winston Graphics!",
                        xlabel="\\Sigma x^2_i",
                        ylabel="\\Theta_i")
                        
julia> add(p, FillBetween(x, c, x, s))
julia> add(p, Curve(x, c, color="black"))
julia> add(p, Curve(x, s, color="red"))
输出
 
数据可视化
数据可视化可以定义为以各种图形和图片格式(例如饼图和条形图)呈现数据。
虻
Gadfly 是一个强大的 Julia 数据可视化包,也是“图形语法”风格的实现。它基于与 R 中的ggplot2相同的原理。要使用它,我们需要首先在 Julia 包管理器的帮助下添加它。
例子
要使用 Gadfly,我们首先需要使用 RDatasets 包,以便我们可以获得一些可以使用的数据集。在此示例中,我们将使用 iris 数据集 -
julia> using Gadfly
julia> using RDatasets
julia> iris = dataset("datasets", "iris");
julia> first(iris,10)
10×5 DataFrame
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │ Species │
│     │  Float64    │    Float64 │  Float64    │   Float64  │  Cat…   │
├─────┼─────────────┼────────────┼─────────────┼────────────┼─────────┤
│  1  │     5.1     │      3.5   │     1.4     │   0.2      │ setosa  │
│  2  │     4.9     │      3.0   │     1.4     │   0.2      │ setosa  │
│  3  │     4.7     │      3.2   │     1.3     │   0.2      │ setosa  │
│  4  │     4.6     │      3.1   │     1.5     │   0.2      │ setosa  │
│  5  │     5.0     │      3.6   │     1.4     │   0.2      │ setosa  │
│  6  │     5.4     │      3.9   │     1.7     │   0.4      │ setosa  │
│  7  │     4.6     │      3.4   │     1.4     │   0.3      │ setosa  │
│  8  │     5.0     │      3.4   │     1.5     │   0.2      │ setosa  │
│  9  │     4.4     │      2.9   │     1.4     │   0.2      │ setosa  │
│ 10  │     4.9     │      3.1   │     1.5     │   0.1      │ setosa  │
      
现在让我们绘制散点图。我们将使用变量 SepalLength 和 SepalWidth。为此,我们需要使用Geom.point设置几何元素,如下所示 -
julia> Gadfly.plot(iris, x = :SepalLength, y = :SepalWidth, Geom.point)
 
同样,我们可以添加更多几何图形,例如 geom.line 来在图中生成更多层 -
julia> Gadfly.plot(iris, x = :SepalLength, y = :SepalWidth, Geom.point, Geom.line)
 
我们还可以设置关键字参数的颜色,如下所示 -
julia> Gadfly.plot(iris, x = :SepalLength, y = :SepalWidth, color = :Species, Geom.point)
 
撰写
Compose 是一个声明性矢量图形系统。它也是由 Daniel Jones 编写的,作为 Gadfly 系统的一部分。在Compose中,图形是使用树结构定义的,基元可以分类如下 -
- 上下文- 它代表一个内部节点。 
- 形式- 它表示定义一些几何形状的叶节点,例如圆或线。 
- 属性- 它表示修改其父级子树绘制方式的叶节点。例如,填充颜色和线宽。 
- Compose(x,y) - 它返回一棵以 x 为根并以 y 作为子节点附加的新树。 
例子
下面的示例将绘制一个简单的图像 -
julia> using Compose
julia> composition = compose(compose(context(), rectangle()), fill("tomato"))
julia> draw(SVG("simple.svg", 6cm, 6cm), composition)
 
现在让我们通过用括号将子树分组来形成更复杂的树 -
julia> composition = compose(context(),
               (context(), circle(), fill("bisque")),
               (context(), rectangle(), fill("tomato")))
julia> composition |> SVG("simple2.svg")
 
图形引擎
在本节中,我们将讨论 Julia 中使用的各种图形引擎。
绘图
PyPlot 源自 PyCall 模块的先前开发,为 Python 中的 Matplotlib 绘图库提供了 Julia 接口。它使用 PyCall 包直接从 Julia 调用 Matplotlib。要使用 PytPlot,我们需要进行以下设置 -
julia> using Pkg
julia> pkg"up; add PyPlot Conda"
julia> using Conda
julia> Conda.add("matplotlib")
完成此设置后,您可以使用 PyPlot命令简单地导入 PyPlot。它可以让您在matplotlib.pyplot中调用函数。
例子
这个例子来自 PyPlot 文档,将创建一个正弦调制的正弦曲线 -
julia> using PyPlot
julia> x = range(0; stop=2*pi, length=500);
julia> y = sin.(3 * x + 4 * cos.(2 * x));
julia> plot(x, y, color="blue", linewidth=1.0, linestyle="--")
1-element Array{PyCall.PyObject,1}:
PyObject <matplotlib.lines.Line2D object at 0x00000000323405E0>
julia> title("A sinusoidally modulated sinusoid")
PyObject Text(0.5, 1.0, 'A sinusoidally modulated sinusoid')
 
PyPlot 包还可用于 3D 绘图,为此它可以从 Matplotlib 的 mplot3d 工具包导入函数。我们也可以通过调用Axes3d的一些相应方法如bar3d、plot_surface、plot3d等直接创建3D图。
例如,我们可以绘制一个随机表面网格,如下所示 -
julia> surf(rand(20,30)) PyObject <mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x00000000019BD550>
 
加斯顿
Gaston 是另一个有用的绘图包。该绘图包提供了gnuplot的接口。
加斯顿的一些特点如下 -
- 它可以使用图形窗口进行绘图,并且通过鼠标交互,它可以同时保持多个绘图处于活动状态。 
- 它可以直接绘制到 REPL。 
- 它还可以在 Jupyter 和 Juno 中绘图。 
- 它支持流行的二维图,如茎图、步长图、直方图等。 
- 它还支持流行的 3 维绘图,例如曲面、等高线和热图。 
- 加载包、绘图并保存为 pdf 大约需要五秒钟。 
例子
在加斯顿的帮助下,一个简单的二维图如下所示 -
julia> x = 0:0.01:1 0.0:0.01:1.0 julia> plot(x, sin.(2π*5*t))
 
现在,我们可以添加另一条曲线,如下所示 -
julia> plot!(x, cos.(2π*5*t))
 
PyPlot can also be used to plot 3-d plots. Example is given below:
julia> a = b = -10:0.30:10
-10.0:0.3:9.8
julia> surf(a, b, (a,b)->sin.(sqrt.(a.*a+b.*b))./sqrt.(a.*a+b.*b),
         title="Sombrero", plotstyle="pm3d")
 
PGF 图
与 Gaston 不同,PGFPlots 是一个相对较新的绘图包。该绘图包使用pgfplots LaTex例程来生成绘图。它可以轻松地与 IJulia 集成,将 SVG 图像输出到笔记本。要使用它,我们需要安装以下依赖项 -
- Pdf2svg,TikzPictures 需要。 
- Pgfplots,您可以使用 Latex 包管理器安装它。 
- GNUPlot,您需要绘制轮廓 
完成这些安装后,您就可以使用 PGFPlots。
在此示例中,我们将在同一轴上生成多条曲线,并以 LaTex 格式分配它们的图例条目 -
julia> using PGFPlots
julia> R = Axis( [ Plots.Linear(x->sin(3x)*exp(-0.3x), (0,8),
      legendentry = L"$\sin(3x)*exp(-0.3x)$"),
      Plots.Linear(x->sqrt(x)/(1+x^2), (0,8),
      legendentry = L"$\sqrt{2x}/(1+x^2)$") ]);
      
julia> save("Plot_LinearPGF.svg", R);
