如何使用pyplot在Julia中绘制两个变量的函数

时间:2016-07-12 19:55:58

标签: matplotlib plot julia

我试图在Julia中用pyplot绘制两个变量的函数。工作起点如下(在StackOverflow中找到):

function f(z,t)
    return z*t
end

z = linspace(0,5,11)
t = linspace(0,40,4)

for tval in t
   plot(z, f(z, tval))
end
show()

这适合我,正在给我我想要的东西: a field of lines

我自己的职能如下:

## needed functions ##

const gamma_0 = 6
const Ksch = 1.2
const Kver = 1.5

function Kvc(vc)
    if vc <= 0 
        return 0
    elseif vc < 20 
        return (100/vc)^0.1
    elseif vc < 100
        return 2.023/(vc^0.153)
    elseif vc == 100
        return 1
    elseif vc > 100
        return 1.380/(vc^0.07)
    else 
        return 0
    end
end 

function Kgamma(gamma_t)
    return 1-((gamma_t-gamma_0)/100)
end

function K(gamma_t, vc)
    return Kvc(vc)*Kgamma(gamma_t)*Ksch*Kver
end

我试图将它们绘制如下:

i = linspace(0,45,10)
j = linspace(0,200,10)
for i_val in i
    plot(i,K(i,j))
end

这给了我以下错误:

  

isless没有匹配无法的方法(:: Int64,:: Array {Float64,1})   加载时[51],从第3行开始的表达式

     

在Kvc in In [17]:2 in anonymous at no file:4

显然,我的函数无法处理数组。

接下来尝试:

i = linspace(0,200,11)
j = linspace(0,45,11)
for i_val in i
    plot(i_val,map(K,i_val,j))
end

给我一​​个只有轴

的空图

任何人都可以给我一个提示......

修改

一个更简单的例子:

using PyPlot
function P(n,M)
    return (M*n^3)/9550
end
M = linspace(1,5,5)
n = linspace(0,3000,3001)

for M_val in M
    plot(n,P(n,M_val))
end
show()

解决方案

好的,在你的帮助下,我找到了这个缩短的例子的解决方案,它对我有用:

function P(n,M)
    result = Array(Float64, length(n))
    for (idx, val) in enumerate(n)
        result[idx] = (M*val^3)/9550
    end
    return result
end
n = linspace(0,3000,3001)

for M_val = 1:5
    plot(n,P(n,M_val))
end
show()

这给了我what I wanted for this shortened example。遗留问题是:它可以用更简单更优雅的方式完成吗?

我会尝试将其应用于原始示例,并在我成功后发布。

2 个答案:

答案 0 :(得分:0)

我并没有完全遵循您要完成的所有细节,但这里有一些示例,说明如何修改几个函数以便它们接受并返回数组:

function Kvc(vc)
    result = Array(Float64, length(vc))
    for (idx, val) in enumerate(vc)
        if val <= 0
            result[idx] = 0
        elseif val < 20
            result[idx] = (100/val)^0.1
        elseif val < 100
            result[idx] = 2.023/(val^0.153)
        elseif val == 100
            result[idx] = 1
        elseif val > 100
            result[idx] = 1.380/(val^0.07)
        else
            result[idx] = 0
        end
    end
    return result
end 

function Kgamma(gamma_t)
    return ones(length(gamma_t))-((gamma_t - gamma_0)/100)
end

另外,对于你的循环,我想你可能想要这样的东西:

for i_val in i
    plot(i_val,K(i_val,j))
end

而不是plot(i, K(i,j),因为它会一遍又一遍地打印相同的东西。

答案 1 :(得分:0)

<是为标量定义的。我认为你需要为数组广播它,即使用.<。例如:

julia> x = 2
2

julia> x < 3
true

julia> x < [3 4]
ERROR: MethodError: no method matching isless(::Int64, ::Array{Int64,2})
Closest candidates are:
  isless(::Real, ::AbstractFloat)
  isless(::Real, ::Real)
  isless(::Integer, ::Char)
 in <(::Int64, ::Array{Int64,2}) at .\operators.jl:54
 in eval(::Module, ::Any) at .\boot.jl:234
 in macro expansion at .\REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at .\event.jl:46

julia> x .< [3 4]
1x2 BitArray{2}:
 true  true