|
| 1 | +############################################ |
| 2 | +# Data Professor # |
| 3 | +# http://youtube.com/dataprofessor # |
| 4 | +# http://github.com/dataprofessor # |
| 5 | +# http://facebook.com/dataprofessor # |
| 6 | +# https://www.instagram.com/data.professor # |
| 7 | +############################################ |
| 8 | + |
| 9 | +# https://rstudio.github.io/reticulate/ |
| 10 | +# install.packages("reticulate") |
| 11 | +library(reticulate) |
| 12 | + |
| 13 | +# Loads Python Shell |
| 14 | +repl_python() |
| 15 | + |
| 16 | +# Check the current Python version |
| 17 | + |
| 18 | +reticulate::py_config() |
| 19 | + |
| 20 | +# Load a particular Python version on our system |
| 21 | +use_python("C:/Program Files/Python38", required = TRUE) |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +############################ |
| 26 | +# |
| 27 | +# matplotlib Example - Scatter plot |
| 28 | +# https://matplotlib.org/3.1.1/gallery/shapes_and_collections/scatter.html#sphx-glr-gallery-shapes-and-collections-scatter-py |
| 29 | +# |
| 30 | +############################ |
| 31 | + |
| 32 | +############################ |
| 33 | +# Import libraries |
| 34 | +############################ |
| 35 | + |
| 36 | +# import matplotlib.pyplot as plt |
| 37 | +plt <- import('matplotlib.pyplot') |
| 38 | + |
| 39 | +# import numpy as np |
| 40 | +np <- import('numpy') |
| 41 | + |
| 42 | +############################ |
| 43 | +# Load the Iris dataset |
| 44 | +############################ |
| 45 | +data(iris) |
| 46 | + |
| 47 | + |
| 48 | +############################ |
| 49 | +# Fixing random state for reproducibility |
| 50 | +############################ |
| 51 | + |
| 52 | +# np.random.seed(19680801) # https://github.com/rstudio/reticulate/issues/226 |
| 53 | +np$random$seed(19680801L) |
| 54 | + |
| 55 | +# N = 50 |
| 56 | +N <- 50L |
| 57 | +# x = np.random.rand(N) |
| 58 | +x <- np$random$rand(N) |
| 59 | + |
| 60 | +# y = np.random.rand(N) |
| 61 | +y <- np$random$rand(N) |
| 62 | + |
| 63 | +# colors = np.random.rand(N) |
| 64 | +colors <- np$random$rand(N) |
| 65 | + |
| 66 | +# area = (30 * np.random.rand(N))**2 # 0 to 15 point radii |
| 67 | +area <- (30 * np$random$rand(N))**2 |
| 68 | + |
| 69 | +# plt.scatter(x, y, s=area, c=colors, alpha=0.5) |
| 70 | +plt$scatter(x, y, s=area, c=colors, alpha=0.5) |
| 71 | + |
| 72 | +# plt.show() |
| 73 | +plt$show() |
0 commit comments