[PYTHON] Draw a heart in Ruby with PyCall

Overview

This is the third in a series that draws a heart. This time, the Python code of the second Drawing a Heart with Python Part 2 (SymPy Edition) is used as PyCall. I tried porting it to Ruby code using a Gem called / mrkn / pycall).

Please note that PyCall has installed the latest version 0.1.0.alpha.20170419' at this time (2017/04/24 23:30).

code

Porting source (Python)

draw_heart.py


from sympy.plotting import plot_parametric
from sympy import Symbol, cos, sin


def draw_heart():
  t = Symbol('t')
  x = 16 * sin(t)**3
  y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)

  plot_parametric(x, y,
                  autoscale=True, title='heart', line_color='pink')


if __name__ == '__main__':
  try:
    draw_heart()
  except KeyboardInterrupt:
    pass

Port (Ruby)

draw_heart.rb


require 'pycall/import'
include PyCall::Import

pyimport 'sympy'
pyfrom 'sympy', import: %i[cos sin]
pyfrom 'sympy.plotting', import: :plot_parametric

def draw_heart
  t = sympy.Symbol.('t')
  x = 16 * sin.(t)**3
  y = 13 * cos.(t) - 5 * cos.(2 * t) - 2 * cos.(3 * t) - cos.(4 * t)

  plot_parametric.(x, y, autoscale: true, title: 'heart', line_color: 'pink')
end

draw_heart

Execution result

figure_1.png

It worked: heart_eyes:

There is no documentation on how to use it in the official repository README.md, so I wrote it with reference to Code examples. It moved unexpectedly smoothly! It's strange that Python code works almost exactly in Ruby: sparkles:

Recommended Posts

Draw a heart in Ruby with PyCall
Draw a heart in Python
Draw a heart in Python Part 2 (SymPy)
Draw a graph with Japanese labels in Jupyter
Draw a graph with NetworkX
Draw a graph with networkx
Draw a graph with Julia + PyQtGraph (2)
Draw a scatterplot matrix in python
Draw a loose graph with matplotlib
Draw a beautiful circle with numpy
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with pandas + XlsxWriter
Draw a CNN diagram in Python
Draw a graph with PySimple GUI
Easily draw a map with matplotlib.basemap
Draw a watercolor illusion with edge detection in Python3 and openCV3
Draw Nozomi Sasaki in Excel with python
Draw a Mandelbrot set with Brainf * ck
Draw a tree in Python 3 using graphviz
Draw a graph with PyQtGraph Part 1-Drawing
(Matplotlib) I want to draw a graph with a size specified in pixels
Draw a flat surface with a matplotlib 3d graph
Draw a graph of a quadratic function in Python
[Python] Get the files in a folder with Python
How to draw a 2-axis graph with pyplot
Draw a "breast curved surface" in a 3D graph (1)
Delete data in a pattern with Redis Cluster
Start Django in a virtual environment with Pipenv
Draw a graph by processing with Pandas groupby
[Python] Draw a directed graph with Dash Cytoscape
Try to draw a life curve with python
Create a virtual environment with conda in Python
Build a Django environment with Vagrant in 5 minutes
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Clone with a specific branch / tag in GitPython
Draw a graph with PyQtGraph Part 4-PlotItem settings
Rock-paper-scissors in Ruby
Work in a virtual environment with Python virtualenv.
Draw a graph with matplotlib from a csv file
Create a new page in confluence with Python
A story packed with absolute values in numpy.ndarray
Sort dict in dict (dictionary in dictionary) with a specific key
Draw a graph with PyQtGraph Part 6-Displaying a legend
[Python] How to draw a histogram in Matplotlib
Draw a "breast curved surface" in a 3D graph (2)
Configure a module with multiple files in Django
Draw multiple photos in a graph from multiple folders
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
Draw a graph in Julia ... I tried a little analysis
[Python] Draw a Qiita tag relationship diagram with NetworkX
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
I want to transition with a button in flask
Playing with a user-local artificial intelligence API in Python
I tried to draw a route map with Python
Make a simple Slackbot with interactive button in python
Try embedding Python in a C ++ program with pybind11
Drawing a tree structure with D3.js in Jupyter Notebook
Forcibly draw something like a flowchart with Python, matplotlib
I want to work with a robot in python.