简体   繁体   English

如何在Julia中使用Gadfly.jl绘制线性函数?

[英]How to plot a linear function using Gadfly.jl in Julia?

I am wondering how to graph a linear function (say y = 3x + 2) using Julia package Gadfly. 我想知道如何使用Julia包Gadfly绘制线性函数(比如y = 3x + 2)。 One way I come up with is to plot two points on that line, and add Geom.line . 我想出的一种方法是在该线上绘制两个点,并添加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. 我在Gadfly文档中找到了函数和表达式部分。 I am not sure how to use it with linear functions? 我不确定如何将它与线性函数一起使用?

Most plotting tools simply accept a list of x values and a list of y values. 大多数绘图工具只接受x值列表和y值列表。 This creates a series of (x,y) points which can then be plotted as individual points, connected by straight lines, connected by a best fit model, whatever you ask for. 这会创建一系列(x,y)点,然后可以将它们绘制为单独的点,通过直线连接,通过最佳拟合模型连接,无论您要求什么。

Linear functions are completely defined by just two points so using a pair of points along with the Geom.line option should be sufficient. 线性函数仅由两个点完全定义,因此使用一对点和Geom.line选项就足够了。 More generally you will want to use linspace to generate the points for your domain. 更一般地说,您将希望使用linspace为您的域生成点。 These will then be passed into your function to generate the range values: 然后将这些传递到您的函数中以生成范围值:

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)

线性图

Like I said before, a line is defined by two points so all of the extra points I added are superfluous in this case. 就像我之前说过的那样,一条线由两点定义,所以我添加的所有额外点在这种情况下都是多余的。 However more generally you will need more points to accurately capture the shape of your function: 但更一般地说,您需要更多的点来准确捕捉函数的形状:

julia> range2 = sin(domain);

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

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

julia> draw(img2, p2)

非线性图

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 at X axis (domain): 您可以直接绘制函数,将其作为参数传递给plot命令(如文档所示 ),然后是X轴(域)的起点和终点:

julia> using Gadfly

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

julia> plot(f, 1.0, 100.0)

在此输入图像描述

Even more complex expressions can be plotted the same way: 可以用相同的方式绘制更复杂的表达式:

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)

在此输入图像描述

If you want to plot two (or more) functions at same graph, you may pass an array of functions as the first argument: 如果要在同一图形上绘制两个(或更多)函数,可以将一组函数作为第一个参数传递:

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

在此输入图像描述

tested with: Julia Version 0.5.0 测试:Julia Version 0.5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM