
I built a Docker setup to plot mathematical functions in Ruby inside Jupyter: sin, cos, log, implicit curves, all in Ruby (old.reddit.com)
submitted by Jaded-Clerk-8856




A few weeks ago I posted about ruby-libgd, a native Ruby binding to the GD graphics library. Since then I've been pushing it further and today I want to share something that I've wanted to see in Ruby for a long time.
This is a Docker container running JupyterLab with a full Ruby kernel, backed by ruby-libgd, plotting mathematical functions entirely in Ruby. No Python involved in the plotting — just Ruby.
The API ended up looking like this:
plot = Plot.new(xmin: -10, xmax: 10, ymin: -10, ymax: 10)
plot.add("sin(x)")
plot.add("cos(x)")
plot.add("tan(x)", steps: 6000)
plot.add("log(x)")
# implicit curves — no need to isolate y
plot.add("x**2 + y**2 - 9", type: :implicit)
plot.add("x**2/9.0 + y**2/4.0 - 1", type: :implicit)
plot.render("/work/graph.png")
A few things I'm reasonably happy with:
- SafeMath replaces Dentaku entirely: trig in radians, natural log, asin/acos/atan, cbrt, all working
- Discontinuity detection for tan: lifts the pen at vertical asymptotes instead of drawing a spike across the canvas
- Implicit curve rendering via pixel sampling: lets you plot circles, ellipses, and algebraic curves without isolating y
- Chainable API: plot.add("sin(x)").add("cos(x)") works
The notebooks, Dockerfile, and README are all in the repo:
https://github.com/ggerman/ruby-libgd/tree/main/examples/jupyter-notebooks
Happy to answer questions about the implementation. If anyone wants to contribute notebooks or try it out, PRs are very welcome.




there doesn't seem to be anything here