A simple interface for plotting with Plotly in Lua
Installation with luarocks
:
luarocks install plotly
The interface in written in pure Lua and requires internet for rending the plots
Some examples can be found here: https://github.com/kenloen/plotly.lua/tree/main/example
The module comes with two approches for creating figures/plots.
The fastest interface is trying to mimic the plotting interface in Matlab and Matplotlib, using the plotly.plot{x, y, ...}
or fig.plot{x, y, ...}
api.
plotly = require("plotly")
plotly.plot{{ 1, 2, 3, 4 }, { 10, 15, 13, 17 },
xlabel="x-axis", ylabel="y-axis",
title="Using plotly.plot"}:show()
Which will open the plot in the browser.
Examples are here:
The other one is a simple wrapper around the plain plotly.js where the data for a plot is provided though JSON data such as data
(containing data about traces), layout
(containing data about figure layout, e.g. title, axis-title, size), and config
(containing data about figure configurations e.g. responsive sizing, interactive title editing).
plotly = require("plotly")
-- Figure with all traces, layout and config
-- From= https://plotly.com/javascript/line-charts/
fig1 = plotly.figure()
fig1:add_trace { x = { 1, 2, 3, 4 }, y = { 10, 15, 13, 17 }, mode = 'markers' }
fig1:add_trace { x = { 2, 3, 4, 5 }, y = { 16, 5, 11, 9 }, mode = 'lines' }
fig1:add_trace { x = { 1, 2, 3, 4 }, y = { 12, 9, 15, 12 }, mode = 'lines+markers' }
fig1:update_layout { title = 'Line and Scatter Plot with scroll-zoom and editable titles' }
fig1:update_config { scrollZoom = true, editable = true }
fig1:show()
Examples are here:
The examples above are using :show()
to show the plot in the browser. If instead a file should be saved the :tofile(filename)
method should be used. Using the example with fig1
from above:
fig1:tofile("awesome_plot.html")
or if both a file should be saved and opening in the browser:
fig1:tofile("awesome_plot.html"):show()