|
| 1 | +{{< include ../_tool-chooser.md >}} |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Quarto has a wide variety of options available for controlling how code and computational output appear within rendered documents. |
| 6 | +In this tutorial we'll take a `.qmd` file that has some numeric output and plots, and cover how to apply these options. |
| 7 | + |
| 8 | +This tutorial will make use of the `matplotlib` and `plotly` Python packages. |
| 9 | +The commands you can use to install them are given in the table below. |
| 10 | + |
| 11 | ++-----------+---------------------------------------------------------+ |
| 12 | +| Platform | Commands | |
| 13 | ++===========+=========================================================+ |
| 14 | +| Mac/Linux | ```{.bash filename="Terminal"} | |
| 15 | +| | python3 -m pip install jupyter matplotlib plotly pandas | |
| 16 | +| | ``` | |
| 17 | ++-----------+---------------------------------------------------------+ |
| 18 | +| Windows | ```{.powershell filename="Terminal"} | |
| 19 | +| | py -m pip install jupyter matplotlib plotly pandas | |
| 20 | +| | ``` | |
| 21 | ++-----------+---------------------------------------------------------+ |
| 22 | + |
| 23 | +If you want to follow along step-by-step in your own environment, create a `computations.qmd` file and copy the following content into it. |
| 24 | + |
| 25 | +```` markdown |
| 26 | +--- |
| 27 | +title: Quarto Computations |
| 28 | +jupyter: python3 |
| 29 | +--- |
| 30 | + |
| 31 | +## NumPy |
| 32 | + |
| 33 | +```{{python}} |
| 34 | +import numpy as np |
| 35 | +a = np.arange(15).reshape(3, 5) |
| 36 | +a |
| 37 | +``` |
| 38 | + |
| 39 | +## Matplotlib |
| 40 | + |
| 41 | +```{{python}} |
| 42 | +import matplotlib.pyplot as plt |
| 43 | + |
| 44 | +fig = plt.figure() |
| 45 | +x = np.arange(10) |
| 46 | +y = 2.5 * np.sin(x / 20 * np.pi) |
| 47 | +yerr = np.linspace(0.05, 0.2, 10) |
| 48 | + |
| 49 | +plt.errorbar(x, y + 3, yerr=yerr, label='both limits (default)') |
| 50 | +plt.errorbar(x, y + 2, yerr=yerr, uplims=True, label='uplims=True') |
| 51 | +plt.errorbar(x, y + 1, yerr=yerr, uplims=True, lolims=True, |
| 52 | + label='uplims=True, lolims=True') |
| 53 | + |
| 54 | +upperlimits = [True, False] * 5 |
| 55 | +lowerlimits = [False, True] * 5 |
| 56 | +plt.errorbar(x, y, yerr=yerr, uplims=upperlimits, lolims=lowerlimits, |
| 57 | + label='subsets of uplims and lolims') |
| 58 | + |
| 59 | +plt.legend(loc='lower right') |
| 60 | +plt.show(fig) |
| 61 | +``` |
| 62 | + |
| 63 | +## Plotly |
| 64 | + |
| 65 | +```{{python}} |
| 66 | +import plotly.express as px |
| 67 | +import plotly.io as pio |
| 68 | +gapminder = px.data.gapminder() |
| 69 | +gapminder2007 = gapminder.query("year == 2007") |
| 70 | +fig = px.scatter(gapminder2007, |
| 71 | + x="gdpPercap", y="lifeExp", color="continent", |
| 72 | + size="pop", size_max=60, |
| 73 | + hover_name="country") |
| 74 | +fig.show() |
| 75 | +``` |
| 76 | +```` |
| 77 | + |
| 78 | +Now, open a Terminal and run `quarto preview`, then position your editor side-by-side with the browser showing the preview. |
| 79 | + |
| 80 | +``` {.bash filename="Terminal"} |
| 81 | +quarto preview computations.qmd |
| 82 | +``` |
| 83 | + |
| 84 | +{.border .column-page-outset-right fig-alt="Side-by-side preview of text editor on the left and live preview in the browser on the right."} |
| 85 | + |
| 86 | +## Cell Output |
| 87 | + |
| 88 | +All of the code in the source file is displayed within the rendered document. |
| 89 | +However, in some cases, you may want to hide all of the code and just show the output. |
| 90 | +Let's go ahead and specify `echo: false` within the document `execute` options to prevent code from being printed. |
| 91 | + |
| 92 | +``` yaml |
| 93 | +--- |
| 94 | +title: Quarto Computations |
| 95 | +execute: |
| 96 | + echo: false |
| 97 | +jupyter: python3 |
| 98 | +--- |
| 99 | +``` |
| 100 | + |
| 101 | +Save the file after making this change. |
| 102 | +The preview will update to show the output with no code. |
| 103 | + |
| 104 | +{.border fig-alt="Output of computations.qmd with 'echo: false' set, shows Title, resulting array in NumPy section, line chart in Matplotlib section, and interactive bubble chart in Plotly section."} |
| 105 | + |
| 106 | +You might want to selectively enable code `echo` for some cells. |
| 107 | +To do this add the `echo: true` cell option. |
| 108 | +Try this with the NumPy cell. |
| 109 | + |
| 110 | +```` markdown |
| 111 | +```{{python}} |
| 112 | +#| echo: true |
| 113 | + |
| 114 | +import numpy as np |
| 115 | +a = np.arange(15).reshape(3, 5) |
| 116 | +a |
| 117 | +``` |
| 118 | +```` |
| 119 | + |
| 120 | +Save the file and note that the code is now included for the NumPy cell. |
| 121 | + |
| 122 | +{.border fig-alt="Rendered NumPy section of computations.qmd which shows the code and the resulting array."} |
| 123 | + |
| 124 | +There a large number of other options available for cell output, for example `warning` to show/hide warnings (which can be especially helpful for package loading messages), `include` as a catch all for preventing any output (code or results) from being included in output, and `error` to prevent errors in code execution from halting the rendering of the document (and print the error in the rendered document). |
| 125 | + |
| 126 | +See the [Jupyter Cell Options](/docs/reference/cells/cells-jupyter.qmd) documentation for additional details. |
| 127 | + |
| 128 | +## Code Folding |
| 129 | + |
| 130 | +Rather than hiding code entirely, you might want to fold it and allow readers to view it at their discretion. |
| 131 | +You can do this via the `code-fold` option. |
| 132 | +Remove the `echo` option we previously added and add the `code-fold` HTML format option. |
| 133 | + |
| 134 | +``` yaml |
| 135 | +--- |
| 136 | +title: Quarto Computations |
| 137 | +format: |
| 138 | + html: |
| 139 | + code-fold: true |
| 140 | +jupyter: python3 |
| 141 | +--- |
| 142 | +``` |
| 143 | + |
| 144 | +Save the file. |
| 145 | +Now a "Code" widget is available above the output of each cell. |
| 146 | + |
| 147 | +{.border fig-alt="Rendered NumPy section of computations.qmd which shows a toggleable section that is labelled 'Code' and the resulting array."} |
| 148 | + |
| 149 | +You can also provide global control over code folding. |
| 150 | +Try adding `code-tools: true` to the HTML format options. |
| 151 | + |
| 152 | +``` yaml |
| 153 | +--- |
| 154 | +title: Quarto Computations |
| 155 | +format: |
| 156 | + html: |
| 157 | + code-fold: true |
| 158 | + code-tools: true |
| 159 | +jupyter: python3 |
| 160 | +--- |
| 161 | +``` |
| 162 | + |
| 163 | +Save the file and you'll see that a code menu appears at the top right of the document that provides global control over showing and hiding code. |
| 164 | + |
| 165 | +{.border fig-alt="Rendered version of the computations.qmd document. A new code widget appears on top right of the document. The screenshot shows that the widget is clicked on, which reveals a drop down menu with three choices: Show All Code, Hide All Code, and View Source. In the background is the rendered document. The title is followed by some text, which is followed by a Code widget that would expand if clicked on, which is followed by the output of the code. The Code widgets are folded, so the code is not visible in the rendered document."} |
| 166 | + |
| 167 | +## Figures |
| 168 | + |
| 169 | +Let's improve the appearance of our Matplotlib output. |
| 170 | +It could certainly stand to be wider, and it would be nice to provide a caption and a label for cross-referencing. |
| 171 | + |
| 172 | +Go ahead and modify the Matplotlib cell to include `label` and `fig-cap` options as well as a call to `fig.set_size_inches()` to set a larger figure size with a wider aspect ratio: |
| 173 | + |
| 174 | +```` markdown |
| 175 | +```{{python}} |
| 176 | +#| label: fig-limits |
| 177 | +#| fig-cap: "Errorbar limit selector" |
| 178 | + |
| 179 | +import matplotlib.pyplot as plt |
| 180 | + |
| 181 | +fig = plt.figure() |
| 182 | +fig.set_size_inches(12, 7) |
| 183 | +``` |
| 184 | +```` |
| 185 | + |
| 186 | +Save the file to re-render and see the updated plot: |
| 187 | + |
| 188 | +{.border fig-alt="Rendered Matplotlib section of computations.qmd which includes a toggleable code-folding widget, the figure, and a caption under the figure that reads 'Figure 1: Errorbar limit selection.'"} |
| 189 | + |
| 190 | +## Multiple Figures |
| 191 | + |
| 192 | +The Plotly cell visualizes GDP and life expectancy data from a single year (2007). |
| 193 | +Let's plot another year next to it for comparison and add a caption and subcaptions. |
| 194 | +Since this will produce a wider visualization we'll also use the `column` option to lay it out across the entire page rather than being constrained to the body text column. |
| 195 | + |
| 196 | +There are quite a few changes to this cell. |
| 197 | +Copy and paste this code into `computations.qmd` if you want to try them locally: |
| 198 | + |
| 199 | +``` python |
| 200 | +#| label: fig-gapminder |
| 201 | +#| fig-cap: "Life Expectancy and GDP" |
| 202 | +#| fig-subcap: |
| 203 | +#| - "Gapminder: 1957" |
| 204 | +#| - "Gapminder: 2007" |
| 205 | +#| layout-ncol: 2 |
| 206 | +#| column: page |
| 207 | + |
| 208 | +import plotly.express as px |
| 209 | +import plotly.io as pio |
| 210 | +gapminder = px.data.gapminder() |
| 211 | +def gapminder_plot(year): |
| 212 | + gapminderYear = gapminder.query("year == " + |
| 213 | + str(year)) |
| 214 | + fig = px.scatter(gapminderYear, |
| 215 | + x="gdpPercap", y="lifeExp", |
| 216 | + size="pop", size_max=60, |
| 217 | + hover_name="country") |
| 218 | + fig.show() |
| 219 | + |
| 220 | +gapminder_plot(1957) |
| 221 | +gapminder_plot(2007) |
| 222 | +``` |
| 223 | + |
| 224 | +Save the file, the preview will update as follows: |
| 225 | + |
| 226 | +{.border fig-alt="Output of Plotly section which shows two charts side-by-side. The first has a caption below that reads '(a) Gapminder: 1957', the second's caption reads '(b) Gapminder 2007'. Below both figures, there's a caption that reads 'Figure 1: Life Expectancy and GDP (Data from World Bank via gapminder.org).'"} |
| 227 | + |
| 228 | +Let's discuss some of the new options used here. |
| 229 | +You've seen `fig-cap` before but we've now added a `fig-subcap` option: |
| 230 | + |
| 231 | +``` python |
| 232 | +#| fig-cap: "Life Expectancy and GDP" |
| 233 | +#| fig-subcap: |
| 234 | +#| - "Gapminder: 1957" |
| 235 | +#| - "Gapminder: 2007" |
| 236 | +``` |
| 237 | + |
| 238 | +For code cells with multiple outputs adding the `fig-subcap` option enables us to treat them as subfigures. |
| 239 | + |
| 240 | +We also added an option to control how multiple figures are laid out---in this case we specified side-by-side in two columns: |
| 241 | + |
| 242 | +``` python |
| 243 | +#| layout-ncol: 2 |
| 244 | +``` |
| 245 | + |
| 246 | +If you have 3, 4, or more figures in a panel there are many options available for customizing their layout. |
| 247 | +See the article [Figures](/docs/authoring/figures.qmd) for details. |
| 248 | + |
| 249 | +Finally, we added an option to control the span of the page that our figures occupy: |
| 250 | + |
| 251 | +``` python |
| 252 | +#| column: page |
| 253 | +``` |
| 254 | + |
| 255 | +This allows our figure display to span out beyond the normal body text column. |
| 256 | +See the documentation on [Article Layout](/docs/authoring/article-layout.qmd) to learn about all of the available layout options. |
| 257 | + |
| 258 | +{{< include _footer.md >}} |
0 commit comments