常用作图脚本

准备一套作图脚本供日后使用。目标包括:

  • 直出 pdf
  • 文字数据可选中
  • 矢量图,放缩无损
  • 风格统一
  • 尽量黑白打印不影响

折线图

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.15.1.json",
  "config": {
    "axisX": {
      "tickMinStep": 50
    },
    "axisY": {
      "tickMinStep": 10000
    },
    "view": {
      "continuousHeight": 300,
      "continuousWidth": 300
    }
  },
  "data": {
    "name": "data-90c5cf575d993e0ad54f896cfbb5c8e6"
  },
  "datasets": {
    "data-90c5cf575d993e0ad54f896cfbb5c8e6": [
      {
        "GPU": "K40",
        "L1": 64,
        "L2": 1536
      },
      {
        "GPU": "P100",
        "L1": 64,
        "L2": 4096
      },
      {
        "GPU": "V100",
        "L1": 128,
        "L2": 6144
      },
      {
        "GPU": "A100",
        "L1": 192,
        "L2": 40960
      },
      {
        "GPU": "H100",
        "L1": 256,
        "L2": 51200
      }
    ]
  },
  "height": 300,
  "layer": [
    {
      "encoding": {
        "text": {
          "field": "GPU",
          "type": "nominal"
        },
        "x": {
          "field": "L1",
          "scale": {
            "domain": [
              50,
              250
            ]
          },
          "title": "L1D+SMEM/KB per SM",
          "type": "quantitative"
        },
        "y": {
          "field": "L2",
          "scale": {
            "domain": [
              0,
              50000
            ]
          },
          "title": "L2/KB",
          "type": "quantitative"
        }
      },
      "mark": {
        "color": "grey",
        "type": "line"
      }
    },
    {
      "encoding": {
        "text": {
          "field": "GPU",
          "type": "nominal"
        },
        "x": {
          "field": "L1",
          "scale": {
            "domain": [
              50,
              250
            ]
          },
          "title": "L1D+SMEM/KB per SM",
          "type": "quantitative"
        },
        "y": {
          "field": "L2",
          "scale": {
            "domain": [
              0,
              50000
            ]
          },
          "title": "L2/KB",
          "type": "quantitative"
        }
      },
      "mark": {
        "align": "left",
        "baseline": "middle",
        "dx": 5,
        "dy": 5,
        "type": "text"
      }
    }
  ],
  "width": 400
}
import sys
import altair as alt # tested on altair==4.2.0 && altair==5.2.0
from vl_convert import vegalite_to_svg
from cairosvg import svg2pdf


def main(*argv):
    data = [{"GPU": "K40", "L1": 64, "L2": 1536},
            {"GPU": "P100", "L1": 64, "L2": 4096},
            {"GPU": "V100", "L1": 128, "L2": 6144},
            {"GPU": "A100", "L1": 192, "L2": 40960},
            {"GPU": "H100", "L1": 256, "L2": 51200}]

    data = alt.Chart(alt.InlineData(data)).encode(
        x=alt.X('L1:Q', title='L1D+SMEM/KB per SM',
                scale=alt.Scale(domain=(50, 250))),
        y=alt.Y('L2:Q', title='L2/KB', scale=alt.Scale(domain=(0, 50000))),
        text='GPU:N'
    )

    data = data.mark_line(
        color="grey"
    ) + data.mark_text(
        align='left',
        baseline='middle',
        dx=5,
        dy=5
    )

    data = data.configure_axisY(
        tickMinStep=10000
    ).configure_axisX(
        tickMinStep=50
    ).properties(
        width=400,
        height=300
    )

    svg2pdf(write_to="gpu-evole.pdf",
            bytestring=vegalite_to_svg(vl_spec=data.to_json()))

    return 0


if __name__ == '__main__':
    sys.exit(main(*sys.argv))