I need to include Python code chunks in an R Markdown HTML. However, when I do so, the plot doesn't show up.
This is the code chunk I have which want to implement. I know this works in Python.
```{python, results='asis'}
import numpy as np
import matplotlib.pyplot as plt
import numpy.random as rng
import matplotlib.cm as cm
from matplotlib.animation import FuncAnimation
radii=(rng.random(int(1e3))+1)**2
iota=2*np.pi*rng.random(int(1e3))
x_posit=np.sqrt(radii)*np.cos(iota)
y_posit=np.sqrt(radii)*np.sin(iota)
plt.plot(x_posit, y_posit, 'go')
```
I expect to get a plot like this
But instead I get this , that is no graph in the R markdown Document. I'm knitting to HTML
Install the library reticulate
via install.packages("reticulate")
and load this chunk before your code presented above:
```{r setup, include=FALSE}
library(knitr)
library(reticulate)
knitr::knit_engines$set(python = reticulate::eng_python)
```
```{python}
import numpy as np
import matplotlib.pyplot as plt
import numpy.random as rng
import matplotlib.cm as cm
from matplotlib.animation import FuncAnimation
radii=(rng.random(int(1e3))+1)**2
iota=2*np.pi*rng.random(int(1e3))
x_posit=np.sqrt(radii)*np.cos(iota)
y_posit=np.sqrt(radii)*np.sin(iota)
plt.plot(x_posit, y_posit, 'go')
plt.show()
```
Note the plt.show()
command.
This will produce your expected output!
Exactly how J_F above described. Just as a reminder: In case you have more than one Python version on your system eg Mac with Python 2.7 and 3.x.
```{r setup, include=FALSE}
Library(knitr)
Library(reticulate)
knitr::knit_engines$set(python3 = reticulate::eng_python)
```
```{python3}
enter code here
```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.