The following python code when run at a python console plots a graph and then blocks on the plt.show()
call:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(0,1,100)
y = np.random.uniform(0,1,100)
plt.scatter(x, y)
plt.show()
When run from a repl_python
this does the same. The usual way to avoid the blocking is to call plt.ion()
("interactive on"). The following code run at a python from the console shows the plot after plt.scatter(x,y)
and returns the python prompt. The shown plot window can be interacted with and the python prompt is live and working.
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
x = np.random.uniform(0,1,100)
y = np.random.uniform(0,1,100)
plt.scatter(x, y)
But when run from a repl_python
session, no plot appears if plt.ion()
is called. The plot object is created, but nothing appears, and even trying plt.show()
has no effect.
> repl_python()
Python 3.8.10 (/usr/bin/python3)
Reticulate 1.26 REPL -- A Python interpreter in R.
Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> plt.ion()
>>>
>>> x = np.random.uniform(0,1,100)
>>> y = np.random.uniform(0,1,100)
>>> plt.scatter(x, y)
<matplotlib.collections.PathCollection object at 0x7fe4136529d0>
>>> plt.show() # nothing shows
Calling plt.close
very briefly flashes up a small empty window which looks like the system mapping and unmapping the window as it closes it . Odd.
The plot backend is TkAgg
as returned by plt.get_backend()
at this point.
This is all running Python and R from Linux terminal windows so no RStudio or notebooks or such involved.
Any ideas? I can supply system info if needed, its R 4.1.1 and reticulate 1.26 with python 3.8.10 on Linux. I can try other systems later...