knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(rTorch)
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:
```{python pyplot}
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")
```{python, engine="python3"}
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()
# 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:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.