Introducing Julia/Plotting
繪圖
編輯Julia 有許多不同的繪圖包,應該會有一款適合您的需要,合您口味。本節是對其中一種繪圖包 Plots.jl 的快速介紹,它非常有趣,因為它與許多其他繪圖軟件包進行了交互。在使用Julia進行繪圖之前,請下載並安裝一些繪圖包 (請先在 REPL 中鍵入 ] 進入 pkg 模式)
(v1.0) pkg> add Plots PyPlot GR UnicodePlots
第一個包,Plots,是一個高級繪圖包,它為其他繪圖包 (這裏稱為「後端」backend )提供一個統一的接口,而這些後端則充當生成圖形的圖形「引擎」。每個後端都是一個獨立的繪圖包,可以單獨使用,但使用 Plots 的優點是,它提供給您一個更簡單更一致的接口。
通過下面的代碼,您可以開始在 Julia 會話中使用 Plots.jl 這個繪圖包。
julia> using Plots
一般來說,您會需要將一個或者多個 序列 (即數值數組)繪製成圖,或者也可以提供一個或多個函數來生成這些數值。
如果您想繼續探究,這裏使用的示例數據是一個簡單的數值數組,表示當前年份中每天的 均時差 的值。(這些值曾用於調整機械時鐘,以解釋地球繞橢圓軌道擺動時的不穩定軌道。)
julia> using Astro # you'll need to add this package with: add https://github.com/cormullion/Astro.jl julia> using Dates
julia> days = Dates.datetime2julian.(Dates.DateTime(2018, 1, 1, 0, 0, 0):Dates.Day(1):Dates.DateTime(2018, 12, 31, 0, 0, 0)) julia> eq_values = map(equation_time, days)
我們現在有了一組 Float64 類型的數據,每個數據表示一年中每天的數值:
365-element Array{Float64,1}: -3.12598 -3.59633 -4.97289 -5.41857 -5.85688 ⋮ -1.08709 -1.57435 -2.05845 -2.53887 -3.01508
要對這一組數據繪圖,只需要把它傳入到 Plots 的 plot()
函數中。
julia> plot(eq_values)
這使用了第一個可用的繪圖引擎。Plot 將該系列視為 y 值,添加了其他打印值,自動提供與您提供的 y 值相匹配的 x 值,然後為您打印所有內容。
如果你想要選擇不同的繪圖引擎,使用提供的幾個函數 gr()
, unicodeplots()
, plotly()
以及其他的。例如,選擇 Unicodeplots 繪圖包(會使用 Unicode 字符繪圖,這是在 REPL/終端 上的最佳選擇):
julia> unicodeplots() julia> plot(eq_values)
+------------------------------------------------------------+ 17 | ,--u | y1 | ./ "\ | | ./ \ | | ./ . | | / \. | | / \ | | .` ", | | ,` \ | | .r--\. / . | | /` \. / \ | |----------------nr-------fhr-----------v------------------v*| | .F \. ,` |.| |, / \, ,/ `| |l / "\. ,` | |". / \-ur/` | | \. /` | | l ,` | | \ ./ | | "\. ./` | -15 | '--" | +------------------------------------------------------------+ 0 370
GR 引擎/後端 也是一個很好的通用繪圖軟件包:
julia> gr(); julia> plot(eq_values)
繪製函數圖像
編輯Switch back to using PyPlot back-end:
julia> pyplot()
The Equation of Time graph can be approximately modeled by a function combining a couple of sine functions:
julia> equation(d) = -7.65 * sind(d) + 9.87 * sind(2d + 206);
It's easy to plot this function for every day of a year. Pass the function to plot()
, and use a range to specify the start and end values:
julia> plot(equation, 1:365)
To combine the two plots, so as to compare the equation and the calculated series versions, Plots lets you add another plot to an existing one. The usual Julia convention of using "!" to modify the argument is available here (in an implicit way — you don't actually have to provide the current plot as an argument): the second plot function, plot!()
modifies the previous plot:
julia> plot(eq_values); julia> plot!(equation, 1:365)
自定義繪圖
編輯There is copious documentation for the Plots.jl package, and after studying it you'll be able to spend hours tweaking and customizing your plots to your heart's content. Here are a few examples.
The ticks along the x-axis show the numbers from 1:365, derived automatically from the single series provided. It would be better to see the dates themselves. First, create the strings:
julia> days = Dates.DateTime(2018, 1, 1, 0, 0, 0):Dates.Day(1):Dates.DateTime(2018, 12, 31, 0, 0, 0) julia> datestrings = Dates.format.(days, "u dd")
The supplied value for the xticks
option is a tuple consisting of two arrays/ranges:
(xticks = (1:14:366, datestrings[1:14:366])
the first provides the numerical values, the second provides matching text labels for the ticks.
Extra labels and legends are easily added, and you can access colors from the Colors.jl package. Here's a prettier version of the basic plot:
julia> plot!( eq_values, label = "equation of time (calculated)", line=(:black, 0.5, 6, :solid), size=(800, 600), xticks = (1:14:366, datestrings[1:14:366]), yticks = -20:2.5:20, ylabel = "Minutes faster or slower than GMT", xlabel = "day in year", title = "The Equation of Time", xrotation = rad2deg(pi/3), fillrange = 0, fillalpha = 0.25, fillcolor = :lightgoldenrod, background_color = :ivory )
其他包
編輯UnicodePlots
編輯If you work in the REPL a lot, perhaps you want a quick and easy way to draw plots that use text rather than graphics for output? The UnicodePlots.jl package uses Unicode characters to draw various plots, avoiding the need to load various graphic libraries. It can produce:
- scatter plots
- line plots
- bar plots (horizontal)
- staircase plots
- histograms (horizontal)
- sparsity patterns
- density plots
Download and add it to your Julia installation, if you haven't already done so:
pkg> add UnicodePlots
You have to do this just once. Now you load the module and import the functions:
julia> using UnicodePlots
Here is a quick example of a line plot:
julia> myPlot = lineplot([1, 2, 3, 7], [1, 2, -5, 7], title="My Plot", border=:dotted)
My Plot ⡤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⢤ 10 ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠔⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠀⠀⠀⠀⠔⠒⠊⠉⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠉⠉⠉⠉⠉⠉⠉⠉⠉⠫⡉⠉⠉⠉⠉⠉⢉⠝⠋⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠁⢸ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⡀⠀⢀⡠⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠔⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ -10 ⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸ ⠓⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠚ 0 10
And here's a density plot:
julia> myPlot = densityplot(collect(1:100), randn(100), border=:dotted) ⡤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⢤ 10 ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ░ ⢸ ⡇ ░░░ ░ ▒░ ▒░ ░ ░ ░ ░ ░ ░ ⢸ ⡇░░ ░▒░░▓▒▒ ▒░░ ▓░░ ░░░▒░ ░ ░ ▒ ░ ░▒░░⢸ ⡇▓▒█▓▓▒█▓▒▒▒█▒▓▒▓▒▓▒▓▓▒▓▒▓▓▓█▒▒█▓▒▓▓▓▓▒▒▒⢸ ⡇ ░ ░ ░░░ ░ ▒ ░ ░ ░░ ░ ⢸ ⡇ ░ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ ⡇ ⢸ -10 ⡇ ⢸ ⠓⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠚ 0 100
(Note that it needs the terminal environment for the displayed graphs to be 100% successful - when you copy and paste, some of the magic is lost.)
VegaLite
編輯allows you to create visualizations in a web browser window. VegaLite is a visualization grammar, a declarative format for creating and saving visualization designs. With VegaLite you can describe data visualizations in a JSON format, and generate interactive views using either HTML5 Canvas or SVG. You can produce:
- Area plots
- Bar plots/Histograms
- Line plots
- Scatter plots
- Pie/Donut charts
- Waterfall charts
- Wordclouds
To use VegaLite, first add the package to your Julia installation. You have to do this just once:
pkg> add VegaLite
Here's how to create a stacked area plot.
julia> using VegaLite julia> x = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9] julia> y = [28, 43, 81, 19, 52, 24, 87, 17, 68, 49, 55, 91, 53, 87, 48, 49, 66, 27, 16, 15] julia> g = [0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1] julia> a = areaplot(x = x, y = y, group = g, stacked = true)
A general feature of VegaLite is that you can modify a visualization after you've created it. So, let's change the color scheme using a function (notice the "!" to indicate that the arguments are modified):
julia> colorscheme!(a, ("Reds", 3))
You can create pie (and donut) charts easily by supplying two arrays. The x array provides the labels, the y array provides the quantities:
julia> fruit = ["peaches", "plums", "blueberries", "strawberries", "bananas"]; julia> bushels = [100, 32, 180, 46, 21]; julia> piechart(x = fruit, y = bushels, holesize = 125)