0

I am running the following python code in R-notebook:

```{r}
library(reticulate)
```

```{python}
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()

```

It's strange because besides a plot, there is a text result.

enter image description here enter image description here

Could you please explain the reason for this phenomena?

Akira
  • 2,594
  • 3
  • 20
  • 45
  • It seems like RStudio is running the Python code in some sort of REPL – juanpa.arrivillaga Mar 05 '20 at 18:38
  • What happens if you use \`\`\`{python, include=FALSE}\`\`\`? – r2evans Mar 05 '20 at 18:56
  • It still runs but returns nothing @r2evans. – Akira Mar 05 '20 at 18:58
  • It should still plot (per knitr's documentation), so that's different. There might be other [chunk arguments](https://yihui.org/knitr/options/#code-evaluation) that would inform this, have you tried any of the others? (I don't know that the affect python the same way they affect R, that might be a difference. I'm not familiar with python plotting, but can you capture the return values of each of your `plt.*` calls so that python doesn't try to print something to the console?) – r2evans Mar 05 '20 at 20:22
  • 1
    Thank you for your reference @r2evans! I will look at it later :) – Akira Mar 05 '20 at 20:45
  • 2
    Well this is a behaviour of `matplotlib`. Even in python itself, unless you save the output to a given variable, there will be text printed on your console – Onyambu Mar 05 '20 at 20:57

1 Answers1

0

Here is @Onyambu comment. I post it here to remove my question from unanswered list.

Well this is a behaviour of matplotlib. Even in python itself, unless you save the output to a given variable, there will be text printed on your console

Akira
  • 2,594
  • 3
  • 20
  • 45