juliajulia-plotsjulia-dataframe

Plotting error data frame to series Julia


I am having trouble plotting in Julia. After uploading an excel file into a data frame and plotting, I the error: 'Cannot Convert Dataframe to series data for plotting'. For reference the simplest code to trigger error:

using Plots, XLSX, DataFrames
Colbalt = DataFrame(XLSX.readtable("/Users/jjtan/Downloads/statisticsExport.xlsx", "Sheet 1")) 
plot(Colbalt, x = :Year, y = :Total)

and the stack trace:

ERROR: Cannot convert DataFrame to series data for plotting
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] _prepare_series_data(x::DataFrame)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/F2mWY/src/series.jl:8
  [3] _series_data_vector(x::DataFrame, plotattributes::Dict{Symbol, Any})
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/F2mWY/src/series.jl:27
  [4] macro expansion
    @ ~/.julia/packages/RecipesPipeline/F2mWY/src/series.jl:127 [inlined]
  [5] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, #unused#::Type{RecipesPipeline.SliceIt}, x::Any, y::Any, z::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesBase/apcHH/src/RecipesBase.jl:290
  [6] _process_userrecipes!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/F2mWY/src/user_recipe.jl:36
  [7] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/F2mWY/src/RecipesPipeline.jl:70
  [8] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/W75kY/src/plot.jl:232
  [9] #plot#149
    @ ~/.julia/packages/Plots/W75kY/src/plot.jl:107 [inlined]
 [10] top-level scope
    @ ~/Documents/Research/Grants/CDFA-ANALYSIS/##Packgaes.jl:9

Solution

  • Where did you find that syntax? As the error says, you cannot directly plot a DataFrame object. Instead, pass the columns you want to plot as vectors:

    plot(Colbalt.Year, Colbalt.Total)
    

    The StatsPlots extention of Plots offers some convenience macros for plotting DataFrames, here are some examples from the Readme:

    using DataFrames
    df = DataFrame(a = 1:10, b = 10 .* rand(10), c = 10 .* rand(10))
    @df df plot(:a, [:b :c], colour = [:red :blue])
    @df df scatter(:a, :b, markersize = 4 .* log.(:c .+ 0.1))