Igor M. Coelho
April 23, 2020
One powerful approach on iLectures on Pandoc is to create plots and graphs that are interactive. If we also want to be able to have a PDF-based version, the best library available seems to be plotly.
We focus on two implementations of plotly:
We will need a pandoc filter for plots: https://github.com/LaurentRDC/pandoc-plot.
We also provide a filter to switch between both versions: comments-to.py.
To install filter pandoc-plot
, you will need Anaconda3 for many data science dependencies.
For Windows and Mac, you can find the plugin on conda-forge. For Linux, it’s not available unfortunately. But you can directly download from github (or from this project) and put on your project folder.
Install plotly deps: conda install -c plotly plotly-orca
Remember to do conda activate
, otherwise you may still get errors like: ModuleNotFoundError: No module named 'plotly'
.
This amazing plugin allows several graphs (only plotly
with interactive counterpart!):
plotly_python
: plots using the plotly Python libraryplotly_r
: plots using the plotly R librarymatplotlib
: plots using the matplotlib Python librarymatlabplot
: plots using MATLABmathplot
: plots using Mathematicaoctaveplot
: plots using GNU Octaveggplot2
: plots using ggplot2gnuplot
: plots using gnuplotYou can get this information via pandoc-plot --toolkits
.
We consider Atom editor with markdown-preview-enhanced
, which allows inline script execution (“Code Chunks”). Too bad that its syntax is not equivalent to pandoc-plot
, so we will need to comment
the visual part (during development) for it to “disappear” on generated beamer
/revealjs
.
For the Atom plugin: ```python {cmd=true}
``` and specially ```python {cmd=true hide=true output="markdown"}
```
For the pandoc-plot
and beamer
: ```{.plotly_python caption="Figure caption"}
```
For the JS and revealjs
: an html <script> ... </script>
.
Only visible in Atom. See: ./figs/fig_plotly_canada.py.
<!-- BEGIN COMMENT -->
\```python {cmd=true output="markdown"}
from figs.fig_plotly_canada import draw_fig
fig = draw_fig('figs/fig_plotly_canada.png')
\```
<!-- @import "figs/fig_plotly_canada.png" -->
<!-- BEGIN COMMENT TO html -->
\`\`\`{.plotly_python caption="This is a Plotly figure"}
from figs.fig_plotly_canada import draw_fig
fig = draw_fig('figs/fig_plotly_canada.png')
\`\`\`
<!-- END COMMENT TO html -->
<!-- BEGIN COMMENT TO beamer -->
<div id='tester2'></div> <script type="module">
import {draw_fig} from './figs/fig_plotly_canada.js';
draw_fig( document.getElementById('tester2') );
</script>
<!-- END COMMENT TO beamer -->
Only visible in Atom. See: ./figs/fig_plotly_line1.py.
<!-- BEGIN COMMENT -->
\```python {cmd=true hide=true output="markdown"}
from figs.fig_plotly_line1 import draw_fig
fig = draw_fig('figs/fig_plotly_line1.png')
\```
<!-- @import "figs/fig_plotly_line1.png" -->
<!-- END COMMENT -->
\```{.plotly_python caption="This is a Plotly figure"}
from figs.fig_plotly_line1 import draw_fig
fig = draw_fig('figs/fig_plotly_line1.png')
\```
This will not appear on revealjs, only beamer.
<!-- BEGIN COMMENT TO revealjs -->
\```{.plotly_python caption="This is a Plotly figure"}
from figs.fig_plotly_line1 import draw_fig
fig = draw_fig('figs/fig_plotly_line1.png')
\```
<!-- END COMMENT TO revealjs -->
See: ./figs/fig_plotly_line1.js.
<!-- BEGIN COMMENT TO beamer -->
<div id='tester3'></div> <script type="module">
import {draw_fig} from './figs/fig_plotly_line1.js';
draw_fig( document.getElementById('tester3') );
</script>
<!-- END COMMENT TO beamer -->
Figure only appears on Atom, after pressing shift+<enter>
. See: ./figs/fig_plotly_bar1.py.
<!-- BEGIN COMMENT -->
\```python {cmd=true hide=true output="markdown"}
from figs.fig_plotly_bar1 import draw_fig
fig = draw_fig('figs/fig_plotly_bar1.png')
\```
<!-- @import "figs/fig_plotly_bar1.png" -->
<!-- END COMMENT -->
Code:
\```{.plotly_python caption="This is a Plotly figure"}
from figs.fig_plotly_bar1 import draw_fig
fig = draw_fig('figs/fig_plotly_bar1.png')
\```
See file figs/fig_plotly_bar1.py
.
This will not appear on beamer, only on revealjs. See: ./figs/fig_plotly_bar1.js.
<!-- BEGIN COMMENT TO beamer -->
<div id='tester4'></div>
<script type="module">
import {draw_fig} from './figs/fig_plotly_bar1.js';
draw_fig( document.getElementById('tester4') );
</script>
<!-- END COMMENT TO beamer -->
See ./figs/fig_plotly_bar1.js
.
Feel free to try other plot formats and technologies.
Please contribute with us if you find more nice things!
Comment Switches
We recommend three switches:
<!-- BEGIN COMMENT -->
: for only visual markdown plugin features<!-- BEGIN COMMENT TO html -->
: for onlybeamer
stuff<!-- BEGIN COMMENT TO beamer -->
: for onlyrevealjs
stuffOn Atom and
markdown-preview-enhanced
, useshift+<enter>
orctrl+shift+<enter>
to execute Code Chunks.