How to plot from a CSV file?

plotting

#1

I have the following Julia code:

using DataFrames, CSV
using Cairo, Gadfly

data = CSV.read("simulate_public.csv")
p = plot(data, x=:p, y=:Expected, Geom.point);
img = SVG("public_plot.svg", 6inch, 4inch)
draw(img, p)

Now, my CSV file (and also the data variable here), have six columns, and they areeA, eB, f , p, Expected, Simulated. But, what’s important for me are the last three columns, namely p, Expected and Simulated. According to the printed values in Julia notebook, it says that p has type of Nullable{Int64}, Expected and Simulated have types of Nullable{Float64}. So, what I want to plot is that, I want the x-axis to be values of p, and in y-axis I want to have the values for Expected and Simulated. Though, I do not know how to combine two plots in one. Also, I want Expected to be plotted as unfilled blue points (circles), and Simulated to be plotted as red crosses (x), So basically I want to see whether the Expected values match the Simulated values that I have. So, one problem is that I do not know how to combine two different sets of data in the same plot, and when I also run the above code I get the following error message:

MethodError: no method matching isfinite(::Nullable{Float64})

Any ideas how to achieve what I want in Julia?


#2

You are probably using an outdated version of the CSV package. Use readtable instead of CSV.read, or try to identify the installed package which prevents you from updating DataFrames and CSV to their latest versions (see How to upgrade from DataFrames 0.10.1 to 0.11.3?).


#3
  • CSV with Gadfly: use CSV.read(..., nullable=false). More info here
  • For your other questions, see the Gadfly docs e.g. Geom.point, Shapes

#4

If you are stuck on an old DataFrames/CSV combo because you need some other package that prevents them from updating, you can also try CSVFiles.jl:

using CSVFiles, FileIO, Gadfly

p = plot(load("simulate_public.csv"), x=:p, y=:Expected, Geom.point)

Note that load(filename) doesn’t return a DataFrame, but because of the iterable tables integration for both CSVFiles and Gadfly things should still just work (i.e. the data is transferred directly from the load to the plot function).

If I got all this complicated logic right, this should work regardless of what version of DataFrames/Gadfly you are using.


#5

Thanks, readtable solved it.