【问题标题】:Cannot convert Array{Any,2} to series data for plotting无法将 Array{Any,2} 转换为系列数据以进行绘图
【发布时间】:2020-09-10 18:21:21
【问题描述】:

我正在向coursera 学习 Julia

using DelimitedFiles
EVDdata = DelimitedFiles.readdlm("wikipediaEVDdatesconverted.csv", ',')

# extract the data
epidays = EVDdata[:,1]
EVDcasesbycountry = EVDdata[:, [4, 6, 8]]

# load Plots and plot them
using Plots
gr()
plot(epidays, EVDcasesbycountry)

我收到错误消息Cannot convert Array{Any,2} to series data for plotting 但在那门课程中,讲师成功地绘制了数据。我哪里错了?

我搜索错误,我最终将字符串解析为整数。由于数据集可能包含字符串值。

或者我错过了什么。

【问题讨论】:

    标签: julia


    【解决方案1】:

    我发现这对我有用:

    # extract the data
    epidays = Array{Integer}(EVDdata[:,1])
    EVDcasesbycountry = Array{Integer}(EVDdata[:, [4, 6, 8]])
    
    # load Plots and plot them
    using Plots
    gr()
    plot(epidays, EVDcasesbycountry)
    

    【讨论】:

    • 谢谢!我遇到了和 OP 一样的问题,这解决了!
    【解决方案2】:

    很难判断 Coursera 中发生了什么,因为不清楚视频使用的是什么版本的 Plots 和 DataFrames。

    但是,您看到的错误是告诉您二维数组(即矩阵)无法转换为单个系列进行绘图。这是因为plot 应该用两个向量调用,一个用于 x 值,一个用于 y 值:

    plot(epidays, EVData[:, 4])
    

    您可以在一个循环中绘制多列:

    p = plot()
    for c in eachcol(EVData[:, [4, 6, 8]])
        plot!(p, epidays, c)
    end
    display(p)
    

    还有StatsPlots.jl,它扩展了标准Plots.jl 包,用于经常需要的“数据科学-y”绘图功能。在这种情况下,您可以使用 @df 宏来绘制 DataFrame;仅引用自述文件中的一个示例:

    using DataFrames, IndexedTables
    df = DataFrame(a = 1:10, b = 10 .* rand(10), c = 10 .* rand(10))
    @df df plot(:a, [:b :c], colour = [:red :blue])
    

    最后,在 Julia 中有更多受图形语法启发的绘图包,它们专注于绘制 DataFrame,例如纯 Julia Gadfly.jl,或 VegaLite 包装器 VegaLite.jl

    【讨论】:

    • 感谢您的回复。您使用 for 循环的第一个代码给出了错误 UndefVarError: eachcol not defined,而使用数据框的第二个代码给出了错误 UndefVarError: @df not defined `
    • 抱歉,这是一个错字 - eachcol 是一个函数,已在上面更正。至于第二个错误,你用StatsPlots吗?
    • 是的,第一个代码现在可以工作了,但是在安装StatsPlots 的第二个代码中,我收到了一个错误Error building 'Arpack':
    • 很高兴您可以使用它!对于 Statsplots,您可能想问一个单独的问题,或者更好地在 Julia Discourse 论坛上提问,因为这些类型的安装问题通常很难以 SO 之类的格式进行调试
    • 感谢您的建议。
    【解决方案3】:

    你也可以试试这个

    using StatsPlots 
    gr()
    using DataFrames, IndexedTables
    df = DataFrame(EVDdata)
    @df df plot(:x1, [:x4 :x6 :x8], marker = ([:octagon :star7 :square], 9), title = "EVD in West Africa, epidemic segregated by country", xlabel   = "Days since 22 March 2014",ylabel   = "Number of cases to date",line = (:scatter), colour = [:red :blue :black])
    

    【讨论】:

      【解决方案4】:

      另一方面,本教程(显然)与 coursera 的情节相同,并且有效。 https://docs.juliaplots.org/latest/tutorial/#Basic-Plotting:-Line-Plots

      x = 1:10; y = rand(10, 2) # 2 columns means two lines
      plot(x, y)
      

      我也没有弄清楚为什么......

      更新:工作人员的回答是“Julia 不再支持 plot 'Array{Any,2}'”,一个简单的解决方法是将 EVDcasesbycountry 数据转换为 Int 这样做:

      epidays = EVDdata[:,1]
      EVDcasesbycountry = convert.(Int, EVDdata[:, [4, 6, 8]])
      

      它对我有用,并且与我的第一个答案有点一致,因为当我检查 x 和 y 的类型时,它们与 Epidays 和 EVDcasesbycountry 的数据不同。

      【讨论】:

        【解决方案5】:

        https://docs.juliaplots.org/latest/generated/gr/

        这包含一些很好的例子

        遇到问题,您可以在矩阵上传递向量而不是进行绘图

        using Plots
        gr()
        y = Vector[EVData[:,4],EVData[:,6],EVData[:,8]]
        plot(
        epidays,y,
        color = [:black :orange :red],
        line = (:scatter),
        marker = ([:hex :d :star4],5)
        )
        

        【讨论】:

          猜你喜欢
          • 2020-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多