1
0
Fork 0
Auswertung_Archiv-Daten_S-B.../plotting.jl

593 lines
16 KiB
Julia

module plotting
using StatsPlots, Plots, DataFrames, Statistics, Dates
export plotEverything, plotBars, plotAllDistributions
function plotEverything(df1, df2, settings, memory)
println("Data is being plotted.")
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
memory.focus = ""
memory.yTicks = ((0-240):60:2000) #change for case
memory.xRotation = 90.0
memory.xLabel = "stations"
memory.yLabel = ""
memory.color = [:chartreuse :yellow2 :lightskyblue :purple]
memory.linewidth = 0
memory.barwidth = 0.7
memory.tickSize = 13
memory.guidefontsize = 20
memory.legendSize = 13
memory.titleSize = 23
memory.legendPosition = :outerright
if settings.approach == "registration points"
memory.yLabel = "deviation in seconds"
memory.focus = "Line " * settings.analyzedLine
memory.size = (2000, 1300)
memory.direction1 =
settings.stationDict[df1.station[length(df1.station)]]
memory.direction2 =
settings.stationDict[df2.station[length(df2.station)]]
if settings.timePeriod[1] != "no" &&
settings.timePeriod[1] != "match day" &&
settings.timePeriod[1] != "rush hour" &&
settings.analyzedLine != "11"
memory.title =
memory.focus *
" - Direction " *
memory.direction1 *
" - " *
settings.timePeriod[1] *
"/ " *
settings.timePeriod[2] *
" - " *
settings.quantile[1] *
" %-Quantile"
p1 = plotBarsDays(df1, settings, memory)
memory.title =
memory.focus *
" - Direction " *
memory.direction2 *
" - " *
settings.timePeriod[1] *
"/ " *
settings.timePeriod[2] *
" - " *
settings.quantile[1] *
" %-Quantile"
p2 = plotBarsDays(df2, settings, memory)
elseif settings.timePeriod[1] == "match day" &&
settings.analyzedLine != "11"
memory.size = (900, 750)
memory.title =
memory.focus *
" - Direction " *
memory.direction1 *
" - Match Day - " *
settings.quantile[1] *
" %-Quantile"
p1 = plotBarsGameOrRushHour(df1, settings, memory)
memory.title =
memory.focus *
" - Direction " *
memory.direction2 *
" - Match Day - " *
settings.quantile[1] *
" %-Quantile"
p2 = plotBarsGameOrRushHour(df2, settings, memory)
elseif settings.timePeriod[1] == "rush hour" &&
settings.analyzedLine != "11"
memory.title =
memory.focus *
" - Direction " *
memory.direction1 *
" - Rush Hour - " *
settings.quantile[1] *
" %-Quantile"
p1 = plotBarsGameOrRushHour(df1, settings, memory)
memory.title =
memory.focus *
" - Direction " *
memory.direction2 *
" - Rush Hour - " *
settings.quantile[1] *
" % Quantile"
p2 = plotBarsGameOrRushHour(df2, settings, memory)
elseif settings.timePeriod[1] == "no" #no extra settings
memory.title = memory.focus * " - Direction " * memory.direction1
p1 = plotBarsMultQuant(df1, settings, memory)
memory.title = memory.focus * " - Direction " * memory.direction2
p2 = plotBarsMultQuant(df2, settings, memory)
memory.title = memory.focus * " - Distribution"
#plotDistributionInSec(df1, df2, settings, memory)
plotDistributionInMin(df1, df2, settings, memory)
if length(settings.allLines) == length(memory.distributionMin)
#plotAllDistributions(settings, memory)
#memory.title = "Distribution S-Bahn Stuttgart - 'new' quantiles"
memory.title = "Distribution S-Bahn Stuttgart - 'danish' quantiles"
p3 = plotAllDistributions(settings, memory)
settings.allLines = ["1", "2", "3", "4", "5", "6", "60"]
pop!(memory.distributionMin)
memory.title = ""
p4 = plotAllDistributions(settings, memory)
all = plot(p3, p4, layout = (2, 1), legend = :bottomright)
savefig(
all,
settings.outputFilePath *
"\\Plots\\all_Lines" *
"_" *
dateString *
".pdf",
)
end
end
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
all = plot(p1, p2, layout = (2, 1), legend = memory.legendPosition)
savefig(
all,
settings.outputFilePath *
"\\Plots\\" *
memory.focus *
" " *
settings.approach *
"_" *
dateString *
".pdf",
)
elseif settings.approach == "between registration points"
myTitle1 = ""
myTitle2 = ""
memory.yLabel = "deviation - median (seconds)"
memory.size = (900, 750)
memory.focus = "Line " * settings.analyzedLine
memory.direction1 = settings.stationDict[df1.point2[length(df1.point2)]]
memory.direction2 = settings.stationDict[df2.point2[length(df2.point2)]]
memory.title = memory.focus * " - Direction " * memory.direction1
p1 = plotBarsLineSection(df1, settings, memory)
memory.title = memory.focus * " - Direction " * memory.direction2
p2 = plotBarsLineSection(df2, settings, memory)
all = plot(p1, p2, layout = (2, 1), legend = false, size = (800, 600))
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
savefig(
all,
settings.outputFilePath *
"\\Plots\\" *
memory.focus *
" " *
settings.approach *
"_" *
dateString *
".pdf",
)
end
end
function plotAllDistributions(settings, memory)
Plots.pyplot()
y = Any[]
x = Any[]
for quantile in settings.quantile
push!(y, parse(Int, quantile))
end
x = memory.distributionMin[1]
#average september and october; source: DB
a = [3, 6]
b = [84.9, 95.8]
tickX = (0:1:20)
z = plot(
a,
b,
label = "average deviation sept/ oct 2017",
xlabel = "Deviation (min)",
ylabel = "Quantile (%)",
marker = true,
legend = :bottomright,
color = :red,
xticks = tickX,
size = (840, 600),
)
colors = [
:gray71,
:slateblue,
:goldenrod,
:darkcyan,
:magenta4,
:aqua,
:deeppink,
:tan4,
]
z = plot!(
x,
y,
title = memory.title,
label = "line " * settings.allLines[1],
marker = true,
color = colors[1],
)
for i = 2:length(memory.distributionMin)
x = memory.distributionMin[i]
colorN = colors[i]
z = plot!(
x,
y,
marker = true,
color = colorN,
label = "line " * settings.allLines[i],
)
end
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
savefig(
z,
settings.outputFilePath *
"\\Plots\\all_lines_deviation(min)_" *
dateString *
".pdf",
)
return z
end
function plotBarsMultQuant(df, settings, memory)
pyplot()
stations = Array{Int,1}()
depOrArr = Array{String,1}()
quantile = Array{Float64,1}()
type = Array{String,1}()
stationList = Any[]
for index = 1:length(df.station)
push!(stationList, df.station[index] * "_" * df.DepOrArr[index])
for numberQuant = 1:length(settings.quantile)
push!(stations, index)
currentColumn = df[!, "quantile"*string(numberQuant)]
push!(quantile, currentColumn[index])
push!(type, settings.quantile[numberQuant] * "% quantile")
end
end
df_new =
DataFrame(station = (stations), quantile = (quantile), type = (type))
memory.stationList = copy(stationList)
x = plotBars(df_new, memory, settings)
x = plot!(
df.AverageDelay,
linewidth = 3,
linecolor = :blue,
marker = true,
label = "Average",
)
return x
end
function plotBarsDays(df, settings, memory)
pyplot()
stations = Array{Int,1}()
depOrArr = Array{String,1}()
quantile = Array{Float64,1}()
type = Array{String,1}()
stationList = Any[]
for index = 1:length(df.station)
push!(stationList, df.station[index] * "_" * df.DepOrArr[index])
for day in settings.timePeriod
push!(stations, index)
currentColumn = df[!, "quantile"*settings.quantile[1]*"_"*day]
push!(quantile, currentColumn[index])
push!(type, day)
end
end
df_new =
DataFrame(station = (stations), quantile = (quantile), type = (type))
memory.stationList = copy(stationList)
x = plotBars(df_new, memory, settings)
x = plot!(
df[!, "average_"*settings.timePeriod[1]],
linewidth = 3,
linecolor = :orange,
label = "Average Delay " * settings.timePeriod[1],
)
x = plot!(
df[!, "average_"*settings.timePeriod[2]],
linewidth = 3,
linecolor = :blue,
label = "Average Delay " * settings.timePeriod[2],
)
return x
end
function plotBarsLineSection(df, settings, memory)
pyplot()
x = bar(
df.median,
xticks = ([1:1:length(df.points);], df.points),
yticks = ((0-270):30:210),
legend = false,
title = memory.title,
ylabel = memory.yLabel,
xlabel = memory.xLabel,
size = (800, 300),
bar_width = 1.0,
xtickfontrotation = memory.xRotation,
)
return x
end
function plotDistributionInSec(df1, df2, settings, memory)
mean1 = (mean(df1.quantile1) + mean(df2.quantile1)) / 2
mean2 = (mean(df1.quantile2) + mean(df2.quantile2)) / 2
mean3 = (mean(df1.quantile3) + mean(df2.quantile3)) / 2
mean4 = (mean(df1.quantile4) + mean(df2.quantile4)) / 2
pyplot()
y = Any[]
for quantile in settings.quantile
push!(y, parse(Int, quantile))
end
x = [mean1, mean2, mean3, mean4]
#Jahrendurschnitt Quelle: DB
a = [3 * 60, 6 * 60]
b = [84.9, 95.8]
z = plot(
a,
b,
title = memory.title,
label = "average deviation september/october 2017",
xlabel = "Deviation (sec)",
ylabel = "Quantile",
marker = true,
legend = :bottomright,
color = :red,
)
z = plot!(
x,
y,
marker = true,
label = "Deviation Line" * settings.analyzedLine,
)
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
savefig(
z,
settings.outputFilePath *
"\\Plots\\Line_" *
settings.analyzedLine *
"_Deviation(sec)_" *
dateString *
".pdf",
)
end
function plotDistributionInMin(df1, df2, settings, memory)
mean1 = (mean(df1.quantile1) + mean(df2.quantile1)) / 2 / 60
mean2 = (mean(df1.quantile2) + mean(df2.quantile2)) / 2 / 60
mean3 = (mean(df1.quantile3) + mean(df2.quantile3)) / 2 / 60
mean4 = (mean(df1.quantile4) + mean(df2.quantile4)) / 2 / 60
pyplot()
y = Any[]
for quantile in settings.quantile
push!(y, parse(Int, quantile))
end
x = [mean1, mean2, mean3, mean4]
push!(memory.distributionMin, x)
#Jahrendurschnitt Quelle: DB
a = [3, 6]
b = [84.9, 95.8]
z = plot(
a,
b,
title = memory.title,
label = "average deviation september/october 2017",
xlabel = "Deviation (min)",
ylabel = "Quantile",
marker = true,
legend = :bottomright,
color = :red,
)
z = plot!(
x,
y,
marker = true,
label = "Deviation " * memory.focus,
color = :blue,
)
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
savefig(
z,
settings.outputFilePath *
"\\Plots\\" *
memory.title *
dateString *
".pdf",
)
end
function plotRailwayNetworkDistr(df1, df2, settings)
mean1 = (mean(df1.quantile1) + mean(df2.quantile1)) / 2 / 60
mean2 = (mean(df1.quantile2) + mean(df2.quantile2)) / 2 / 60
mean3 = (mean(df1.quantile3) + mean(df2.quantile3)) / 2 / 60
mean4 = (mean(df1.quantile4) + mean(df2.quantile4)) / 2 / 60
pyplot()
y = Any[]
for quantile in settings.quantile
push!(y, parse(Int, quantile))
end
x = [mean1, mean2, mean3, mean4]
z = plot(
x,
y,
title = settings.objectInFocus * " - Distribution",
label = "Deviation " * settings.objectInFocus,
xlabel = "Deviation (min)",
ylabel = "Quantile",
marker = true,
legend = :bottomright,
)
#Durchschnitt September und Oktober Quelle: DB
a = [3, 6]
b = [84.9, 95.8]
z = plot!(a, b, marker = true, label = "Average Deviation 2017")
dateString = Dates.format(Dates.now(), "yyyy-mm-dd_HH.MM.SS")
savefig(
z,
settings.outputFilePath *
"\\Plots\\" *
settings.objectInFocus *
"_Deviation(min)_" *
dateString *
".pdf",
)
end
function plotBarsGameOrRushHour(df, settings, memory)
pyplot()
stations = Array{Int,1}()
depOrArr = Array{String,1}()
quantile = Array{Float64,1}()
type = Array{String,1}()
stationList = Any[]
for index = 1:length(df.station)
push!(stationList, df.station[index] * "_" * df.DepOrArr[index])
for indicator in ["yes", "no"]
push!(stations, index)
currentColumn = df[!, "quantile"*settings.quantile[1]*"_"*indicator]
push!(quantile, currentColumn[index])
push!(type, indicator)
end
end
newType = Any[]
if settings.timePeriod[1] == "rush hour"
label1 = "Rush Hour"
label2 = "'Normal' Time"
elseif settings.timePeriod[1] == "match day"
label1 = "Match Day"
label2 = "'Normal' Day"
end
for x in type
if x == "yes"
push!(newType, label1)
else
push!(newType, label2)
end
end
df_new =
DataFrame(station = (stations), quantile = (quantile), type = (newType))
memory.size = (2000, 1000)
memory.stationList = copy(stationList)
x = plotBars(df_new, memory, settings)
x = plot!(
df[!, "average_yes"],
linewidth = 3,
linecolor = :orange,
label = "Average Delay " * label1,
)
x = plot!(
df[!, "average_no"],
linewidth = 3,
linecolor = :blue,
label = "Average Delay " * label2,
)
return x
end
function plotBars(df, memory, settings)
if settings.analyzedLine == "11" &&
memory.title == "Line 11 - Direction Herrenberg"
memory.yTicks = ((0-240):120:3000)
end
x = groupedbar(
df.quantile,
xticks = ([1:1:size(memory.stationList, 1);], memory.stationList),
yticks = memory.yTicks,
group = df.type,
ylabel = memory.yLabel,
xlabel = memory.xLabel,
title = memory.title,
size = memory.size,
bar_width = memory.barwidth,
linewidth = memory.linewidth,
tickfontsize = memory.tickSize,
legendfontsize = memory.legendSize,
guidefontsize = memory.guidefontsize,
titlefontsize = memory.titleSize,
xtickfontrotation = memory.xRotation,
legend = memory.legendPosition,
)
return x
end
end