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

烈酒焚心 提交于 2020-07-23 06:38:06

问题


I am learning the Julia from the coursera

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)

I am getting the error message Cannot convert Array{Any,2} to series data for plotting but in that course the lecturer successfully plots the data. where I am going wrong?

I search about the error where I end up something call parsing the string into an integer. As the data set may contain string values.

Or am I missing something else.


回答1:


It's a bit hard to tell what's going on in Coursera, as it's not clear what versions of Plots and DataFrames the video is using.

The error you're seeing however is telling you that a 2-dimensional Array (i.e. a matrix) can't be converted to a single series for plotting. This is because plot is supposed to be called with two vectors, one for x and one for y values:

plot(epidays, EVData[:, 4])

You can plot multiple columns in a loop:

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

There is also StatsPlots.jl, which extend the standard Plots.jl package for frequently needed "data science-y" plotting functions. In this case you could use the @df macro for plotting DataFrames; just quoting one of the examples in the Readme:

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])

Finally, there are some more grammar-of-graphics inspired plotting packages in Julia which are focused on plotting DataFrames, e.g. the pure-Julia Gadfly.jl, or the VegaLite wrapper VegaLite.jl




回答2:


You can also try this

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])


来源:https://stackoverflow.com/questions/61979936/cannot-convert-arrayany-2-to-series-data-for-plotting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!