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

Viewed 1772

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.

5 Answers

I found this to be working for me:

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

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

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

On the other hand, this tutorial does (apparently) the same thing as the coursera plot and it works. 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)

And I haven't figured out why too...

Update: The staff answer is that maybe " Julia no longer supports plot 'Array{Any,2}' " and a simple workaround is to convert the EVDcasesbycountry data to Int doing this:

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

It worked for me and is kinda consistant with my first answer because when I checked the types of x and y they weren't Any as the data of epidays and EVDcasesbycountry.

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

This contains some nice examples

Coming to the problem you can pass vector instead on the matrix for plotting

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