8  Jupyter Notebook

Quarto book 也支持 .ipynb 文件。你可以在 Quarto book 中直接使用 Jupyter Notebook 文件。

Jupyter Notebook 文档中的代码块可以直接运行,输出的图片会被自动存储在 ./_static 目录下。当你通过 github pages 发布时,这些图片会被自动上传到 GitHub。

一个 Jupyter Notebook 文档中可以支持多种编程语言。

8.1 Python 代码

绘制函数图 \(y = x^2\) 的代码如下:

# 绘制一幅简单的函数图

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 200)
y = x ** 2

plt.figure(figsize=(3, 2.5))
plt.plot(x, y)
plt.title("y = x^2")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.show()

8.2 Stata 代码

使用 Stata 自带的 sysuse auto 数据集,绘制汽车价格与重量的散点图:

sysuse "auto.dta", clear
twoway scatter mpg weight, scheme(cleanplots)
(1978 automobile data)