2

我正在绘制从另一个数据框创建的数据框。基本上,我只取了大约 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, ::Any)@RecipesBase.jl:282
_process_userrecipes!(::Any, ::Any, ::Any)@user_recipe.jl:36
recipe_pipeline!(::Any, ::Any, ::Any)@RecipesPipeline.jl:70
_plot!(::Plots.Plot, ::Any, ::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,2},String,String,Array{Dates.Date,1},Int64,Tuple{Symbol,Int64},Tuple{Symbol,String},Symbol,Bool,Symbol,Int64,Int64,Symbol}}}, ::typeof(StatsPlots.add_label), ::Array{Any,1}, ::Function, ::Array{Dates.Date,1}, ::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
4

1 回答 1

1

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

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

于 2021-03-24T20:33:50.953 回答