问题
When I process my .Rmd file, there is no matplotlib img displayed. Is there a chunk option, or a different matplotlib method that is needed?
---
title: "Viz Examples"
output:
html_document:
keep_md: true
---
```{r testplot, engine="python"}
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)
# You can provide either a single color or an array. To demonstrate this,
# the first bar of each set will be colored cyan.
cs = [c] * len(xs)
cs[0] = 'c'
ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.print()
```
回答1:
instead of plt.print()
or plt.show()
use
plt.savefig("somefilename.ext")
with ext
being png
or jpg
or other supported output format, and any other savefig
options, like transparent
, then use the figure using a standard knitr mechanism.
```{r showfig,include=TRUE,echo=FALSE, results='asis'} cat("!(./threedee.png)")
```
来源:https://stackoverflow.com/questions/25294911/knitr-python-engine-output-not-in-md-or-html