在 Emacs 中建立 Vega-lite 编辑环境

[Problem] 求推荐一款数据分析绘图软件继续讨论:

Vega-lite 本质上是 JSON 文件,在 Emacs 中可以用自带的 json-mode 编辑。想要䃼全的话可以用 eglot 配合 vscode-json-languageserver - npm 使用,注意需要有 yasnippet 才会开启䃼全。

JSON lsp server 读取到

"$schema": "https://vega.github.io/schema/vega-lite/v5.json"

后会自功下载 JSON Schema 提供䃼全,你也可以把 Schema 下到本地把 URL 替成本地文件。

对于不想在 macOS 上装 nodejs 和 npm 的用户,可以用 GitHub - zed-industries/json-language-server ,记得把 binary 重命名成 vscode-json-language-server

Vega lite 可以在浏览器中加載 JS 使用,也可用 npm 或 yarn 安装 vega-lite 使用命令行生成 PDF/SVG。

下面是用从 gnucap 生成的 ac frequency response 的演示,

bode

Vega lite source
{
    "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
    "data": {"url": "bode.tsv",
             "format": {"type": "tsv"}},
    "transform": [{"calculate": "20*log(100*datum['VM(out)'])/LN10",
                   "as": "gain"}],
    "mark": {
        "type": "line",
        "interpolate": "natural",
        "strokeWidth": 1
    },
    "encoding": {
        "x": {
            "field": "Freq",
            "scale": {"type": "log"},
            "title": "Frequency (Hz)",
            "axis": {"format": "~s"}
        },
        "y": {
            "field": "gain",
            "type": "quantitative",
            "title": "Gain (dB)"
        }
    },
    "width": 600,
    "height": 400
}

wave

Vega lite source
{
    "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
    "data": {"url": "data.tsv",
             "format": {"type": "tsv"}},
    "repeat": {
        "layer": ["V(in)", "V(out)", "p(Vcc)" ]
    },
    "spec": {
        "mark": {
            "type": "line",
            "interpolate": "natural",
            "strokeWidth": 1
        },
        "encoding": {
            "x": {
                "field": "Time",
                "type": "quantitative",
                "title":"Time (s)"
            },
            "y": {
                "field": {"repeat": "layer"},
                "type": "quantitative",
                "title": null
            },
            "color": {
                "datum": {"repeat": "layer"},
                "type": "nominal"
            }
        },
        "width": 600,
        "height": 400
    }
}
我用 TECO 写了个脚本把 gnucap 的输出格式转成合法的 TSV。
! Assume buffer content is the file name without extension !

0,128ET      ! Abort on error !

EO - 200 "L
    @^A/Macro requires TECO version 200+/ 13^T 10^T ^C
'
Z            !! Error if no file name
"E
    :@^A/Specify file name with --text or -T option/ ^C
'
hXa bJ       !! copy to @A
@FS/.txt/.tsv/ !! Replace the file extension
hXb hK       !! save to @B
@EW/^EQb/    !! specify write to file.tsv
@ER/^EQa/ Y  !! read from file.txt

0J
<.-Z;   !! Exit if is last of the line
DL-2CDL !! Remove leading and trailing spaces
>

0J<@FS/ /	/;>  !! Replace space to tab

EX  ! bye !
^[^[
2 个赞

另外,在发现 Vega 之前我是用 Dyalog APL 画图的,该有的是都有但都要手功实现,加新功能很麻烦。

#!/usr/local/bin/dyalogscript
⎕CY'sharpplot'
argv←2⎕NQ#'GetCommandLineArgs'
∇r←parsefile name;f;c;l;ls;h
 (f c l)←⎕NGET name
 ls←(⎕UCS l)(~⍤∊⍨⊆⊢)f
 r←⍪' '(≠⊆⊢)1↓⊃ls
 r,←⍪↓⍉↑{⊃⌽⎕VFI'+'~⍨'¯'@(=∘'-')⍵}¨1↓ls
∇
ticks←10*⍳8
grids←(10*8),⍨,⍉(⍳9)∘.×10*⍳7
labs←(⊂'100M'),⍨⊃,/{(⊂⍵),8⍴⊂''}¨'10' '100' '1K' '10K' '100K' '1M' '10M'
labs←'10' '100' '1K' '10K' '100K' '1M' '10M' '100M'
∇plot(data output);sp;ls
 sp←⎕NEW SharpPlot
 sp.Flexibility←9
 sp.LineGraphStyle←LineGraphStyles.Curves+LineGraphStyles.GridLines
 ls←,0 1↓data
 sp.XAxisStyle←XAxisStyles.LogScale
 sp.SetLabelMask(⊂grids∊ticks)
 sp.SetXLabels(⊂labs)
 sp.SetXTickMarks(⊂grids)
 sp.DrawLineGraph((20×10⍟0.01÷⍨1↓ls)(⊃ls))
 sp.SetKeyText(⊂,1↓1↑⍤1⊢data)
 sp.SavePdf(⊂output)
∇
plot(parsefile⊃1↓argv)(⊃2↓argv)