6

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 5

6

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I had the same problem as the OP and this solved it!
3

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

6 Comments

Thanks for your response. Your first code using for loop gives an error UndefVarError: eachcol not defined and second code using data frame gives an error UndefVarError: @df not defined `
Sorry, that was a typo - eachcol is a function, corrected above. As for the second error, did you use StatsPlots?
Yes, the first code is working now, but in the second code while installing StatsPlots, I got an error Error building 'Arpack':
Glad you got it to work! For Statsplots you probably want to ask a separate question, or even better ask on the Julia Discourse forum as these kinds of installation issues are often hard to debug in a format like SO
Thanks for your suggestion.
|
1

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.