微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Julia 无法将 Array 转换为用于绘图的系列数据

如何解决Julia 无法将 Array 转换为用于绘图的系列数据

我正在绘制从另一个数据框创建的数据框。基本上,我只取了大约 5 行和所有 N-4 列并创建了一个新的数据框。我想绘制此图,但 Julia(在 Pluto.jl 上)抛出此错误

Cannot convert Array{Any,2} to series data for plotting

error(::String)@error.jl:33
_prepare_series_data(::Array{Any,2})@series.jl:8
_series_data_vector(::Array{Any,2},::Dict{Symbol,Any})@series.jl:27
macro expansion@series.jl:144[inlined]
apply_recipe(::AbstractDict{Symbol,Any},::Type{RecipesPipeline.SliceIt},::Any,::Any)@RecipesBase.jl:282
_process_userrecipes!(::Any,::Any)@user_recipe.jl:36
recipe_pipeline!(::Any,::Any)@RecipesPipeline.jl:70
_plot!(::Plots.Plot,::Any)@plot.jl:172
#plot#129@plot.jl:58[inlined]
#add_label#17(::Base.Iterators.Pairs{Symbol,Any,NTuple{13,Symbol},NamedTuple{(:label,:xlabel,:ylabel,:xticks,:xrotation,:marker,:line,:legend,:grid,:framestyle,:legendfontsize,:tickfontsize,:formatter),Tuple{Array{String,String,Array{Dates.Date,1},Int64,Tuple{Symbol,Int64},String},Symbol,Bool,Symbol}}},::typeof(StatsPlots.add_label),::Array{Any,::Function,::Array{Dates.Date,::Vararg{Any,N} where N)@df.jl:155
(::Main.workspace140.var"#1#3")(::DataFrames.DataFrame)@range.jl:0
top-level scope@Local: 17

这是引发此错误的单元格:

begin
    
    countries = ["Italy","Germany","India","United Kingdom"];
    y = DataFrame() # empty dataframe

    for country in countries    
        data_dfr = get_country(df,country); # returns a dataframe row 
        data_dfr = DataFrame(data_dfr);           # convert dataframe row back to a                                                                             dataframe
        df_rows,df_cols = size(data_dfr);
        data_dfl = stack(data_dfr,5:df_cols);       # convert dataframe into long                                                                                  format
        y[!,Symbol("$country")] = data_dfl[!,:value]
    end

    rows,cols = size(y)
    
    gr(size=(900,600))
    @df y plot(x_axis,cols(1:cols),label =  reshape(names(y),(1,length(names(y)))),xlabel = "Time",ylabel = "Total number of reported cases",xticks = x_axis[1:7:end],xrotation = 45,marker = (:diamond,4),line = (:line,"gray"),legend = :topleft,grid = false,framestyle = :semi,legendfontsize = 9,tickfontsize = 9,formatter = :plain
        )
    
    y.One_million = Array{Union{Missing,Float64},1}(missing,size(y,1));
    y.One_million .= 10^6.0;
    
    display(@df y plot!(x_axis,y[!,cols+1],linestyle = :dot,linewidth = 5,color = :red,label = names(y)[cols+1]))
    
    y = select!(y,Not([:One_million])); 
end

下面是上面代码中用到的函数和变量:

begin
    dates = names(df)[begin:end-4]
    date_format = Dates.DateFormat("m/d/y")
    x_axis = parse.(Date,dates,date_format) .+ Year(2000)
end
function get_country(dataframe,country::String)
    df_country = dataframe[ismissing.(dataframe[!,Symbol("Province/State")]),:]
    indx = findfirst(df_country[!,Symbol("Country/Region")] .== country)
    return df_country[indx,:]
end

解决方法

错误消息“无法将 Array{Any,2} 转换为用于绘图的系列数据”指出问题在于您作为 y 值传递的数据,即 cols(1:cols)(x 值在系列之间共享).

鉴于它的类型是 Any,一些非数字似乎潜入了。看看那个矩阵,看看你发现了哪些非数字,我相信 missing 应该没问题,但如果那样的话使用 skipmissing() 摆脱它们。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。