How to build a linear function using Gadfly.jl in Julia?

I am wondering how to graph a linear function (e.g. y = 3x + 2) using the Julia Gadfly package. One of the ways I came up with is to build two points on this 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)

enter image description here

Is there a better way to draw lines? I found the Functions and Expressions section in a Gadfly document. I'm not sure how to use it with linear functions?

+4
source share
2 answers

, plot ( ), X ():

julia> using Gadfly

julia> f(x) = 3x + 2
f (generic function with 1 method)

julia> plot(f, 1.0, 100.0)

enter image description here

:

julia> f3(x) = (x >= 2.0) ? -2.0x+1.0 : 2.0x+1.0
f3 (generic function with 1 method)

julia> plot(f3, -5, 10)

enter image description here

( ) , :

julia> plot([f, f3], -5, 10)

enter image description here

: 0.5.0

+3

x y. (x,y), , , , , .

, Geom.line . linspace . :

julia> using Gadfly

julia> f(x) = 3x + 2
f (generic function with 1 method)

julia> domain = linspace(-2,2,100);

julia> range1 = f(domain);

julia> p = plot(x=domain, y=range1, Geom.line)

julia> img = SVG("test.svg", 6inch, 4inch)

julia> draw(img, p)

Line graph

, , , , . :

julia> range2 = sin(domain);

julia> p2 = plot(x=domain, y=range2, Geom.line)

julia> img2 = SVG("test2.svg", 6inch, 4inch)

julia> draw(img2, p2)

Non-linear graph

+3

Source: https://habr.com/ru/post/1665048/


All Articles