Skip to content

Instantly share code, notes, and snippets.

@ararog
Last active November 26, 2015 15:56
Show Gist options
  • Save ararog/402365771035c0bcc87a to your computer and use it in GitHub Desktop.
Save ararog/402365771035c0bcc87a to your computer and use it in GitHub Desktop.
Sample code that ilustrastes how plotting is done with Julia programming language.
# Installing and loading libraries if needed
using Gadfly
using DataFrames
using DataFramesMeta
using ZipFile
using HTTPClient.HTTPC
# Read computer available memory
#available_memory = memory.limit()
# Since this information has been provided in advance,
# I choose to not perform any extra work to guess them
number_of_rows = 2075259
number_of_cols = 9
bytes_per_cell = 8
data_size_bytes = number_of_rows * number_of_cols * bytes_per_cell
data_size_mb = data_size_bytes / (1024 ^ 2)
println("Checking if there's enough memory available")
#if data_size_mb > available_memory
# println("There's no available memory to load dataset required to run this script, exiting...")
# exit()
#end
if ! isfile("dataset.zip")
println("Downloading dataset archive...")
HTTPC.get("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", RequestOptions(ostream="dataset.zip"))
else
println("Dataset archive already downloaded, continuing")
end
if isfile("dataset.zip")
if ! isfile("household_power_consumption.txt")
println("Unzipping dataset archive")
r = ZipFile.Reader("dataset.zip");
for f in r.files
file = open(f.name,"w")
write(file, readall(f))
close(file)
end
close(r)
end
println("Checking if household_power_consumption.txt exists...")
if isfile("household_power_consumption.txt")
classes = [UTF8String, UTF8String, Float64,
Float64, Float64, Float64, Float64, Float64, Float64]
println("Loading data")
eletricPowerData = readtable("household_power_consumption.txt",
separator = ';', header=true, eltypes = classes, nastrings = ["", "NA", "?"])
eletricPowerData_subset = @where(eletricPowerData, (:Date .== "1/2/2007") | (:Date .== "2/2/2007"))
println("Plotting data and saving the image to disk")
p = plot(eletricPowerData_subset, x="Global_active_power", Geom.histogram,
Guide.ylabel("Frequency"), Guide.xlabel("Global Active Power (kilowatts)"), Guide.title("Global Active Power"))
draw(PNG("plot1.png", 6inch, 3inch), p)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment