Struggling_Student
Struggling_Student

Reputation: 464

Using operators inside a plot function in Julia

I have following dataframe

enter image description here

I would like to plot Evar / (T^2 * L)

using Plots, DataFrames, CSV
@df data plot(:T, :Evar / (:T * T * :L)  , group=:L, legend=nothing)
MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})

Unfortunately I am not sure how to use operators inside the plot function. For the "/" operator it seems to work, but if I want to multiply using "*" I get the error above.
Here is an example of what I mean by "/" working: enter image description here

Upvotes: 2

Views: 50

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42224

You need to vectorize the multiplication and division so this will be:

@df data plot(:T, :Evar ./ (:T .* :T .* :L)  , group=:L, legend=nothing)

Simpler example:

julia> a = [1,3,4]; 
julia> b = [4,5,6];

julia> a * b
ERROR: MethodError: no method matching *(::Vector{Int64}, ::Vector{Int64})

julia> a .* b
3-element Vector{Int64}:
  4
 15
 24

Not that / works because / is defined for vectors but the results is perhaps not exactly what you would have wanted:

julia> c = a / b
3×3 Matrix{Float64}:
 0.0519481  0.0649351  0.0779221
 0.155844   0.194805   0.233766
 0.207792   0.25974    0.311688

It just returned matrix such as c*b == a where * is a matrix multiplication.

Upvotes: 2

Related Questions