knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(rTorch)

Example 1

import numpy as np
import matplotlib.pyplot as plt

n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)

fig, ax = plt.subplots( nrows=1, ncols=1 )
ax.plot (X, Y+1, color='blue', alpha=1.00)
ax.plot (X, Y-1, color='blue', alpha=1.00)
# plt.show()
fig.savefig('foo.png', bbox_inches='tight')
print("finished")

Output image: output

Example 2

```{python pyplot}

https://stackoverflow.com/a/50711837/5270873

import matplotlib 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)

fig, ax = plt.subplots() ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='About as simple as it gets, folks') ax.grid()

fig.savefig("pyplot.png") plt.close(fig)

```r
knitr::include_graphics("pyplot.png")

multiprocessing, no output 1

```{python, engine="python3"}

https://stackoverflow.com/a/56982302/5270873

import time import multiprocessing import os

def plot_graph(data): from matplotlib.pyplot import plot, draw, show print("entered plot_graph()") plot(data) show() # this will block and remain a viable process as long as the figure window is open print("exiting plot_graph() process")

multiprocessing.Process(target=plot_graph, args=([1, 2, 3],)).start() time.sleep(3)

## multiprocessing, no output 2

```{python}
# https://stackoverflow.com/a/458246/5270873
from multiprocessing import Process
from matplotlib.pyplot import plot, show

def plot_graph(*args):
    for data in args:
        plot(data)
    show()

p = Process(target=plot_graph, args=([1, 2, 3],))
p.start()

print('yay')
print('computation continues...')
print('that rocks.')

print('Now lets wait for the graph be closed to continue...:')
p.join()

Call python code sample, markdown image

# https://stackoverflow.com/a/36439422/5270873
import numpy as np
import matplotlib.pyplot as plt

n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)

fig, ax = plt.subplots( nrows=1, ncols=1 )
ax.plot (X, Y+1, color='blue', alpha=1.00)
ax.plot (X, Y-1, color='blue', alpha=1.00)
#plt.show()
fig.savefig('foo.png', bbox_inches='tight')
print("finished")

Output image: output



f0nzie/rTorch documentation built on Oct. 28, 2021, 5:40 a.m.