如何在Julia中使用Gadfly.jl绘制线性函数? [英] How to plot a linear function using Gadfly.jl in Julia?

查看:531
本文介绍了如何在Julia中使用Gadfly.jl绘制线性函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Julia包Gadfly来绘制线性函数(比如说y = 3x + 2)。我想出的一种方法是在该行上绘制两点,并添加 Geom.line



<$ p $使用Gadfly

函数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 ,6英寸,4英寸)
draw(img,p)



有没有更好的方法来绘制线条?我发现



更复杂的表达式可以用相同的方式绘制:

  julia> ; f3(x)=(x> = 2.0)? -2.0x + 1.0:2.0x + 1.0 
f3(带1个方法的通用函数)

julia> plot(f3,-5,10)



如果您想在同一个图上绘制两个(或多个)函数,您可以传递一个函数数组作为第一个参数:

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



经过测试:Julia 0.5.0版本


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 at X axis (domain):

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中使用Gadfly.jl绘制线性函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆