I am wondering how to graph a linear function (say y = 3x + 2) using Julia package Gadfly. One way I come up with is to plot two points on that line, and add Geom.line
.
using Gadfly
function f(x)
3 * x + 2
end
domains = [1, 100]
values = [f(i) for i in domains]
p = plot(x = domains, y = values, Geom.line)
img = SVG("test.svg", 6inch, 4inch)
draw(img, p)
Are there any better way to plot lines? I have found the Functions and Expressions section on the Gadfly document. I am not sure how to use it with linear functions?
You may plot the function directly, passing it as an argument to the
plot
command (as you pointed at documentation), followed by the start and end points atX
axis (domain):Even more complex expressions can be plotted the same way:
If you want to plot two (or more) functions at same graph, you may pass an array of functions as the first argument:
tested with: Julia Version 0.5.0