Skip to content

Instantly share code, notes, and snippets.

@rometsch
Created November 15, 2019 18:23
Show Gist options
  • Save rometsch/6b84363f572efd0ac7f3f34697d24c7a to your computer and use it in GitHub Desktop.
Save rometsch/6b84363f572efd0ac7f3f34697d24c7a to your computer and use it in GitHub Desktop.
Compare prices for bouldering hall in Tübingen using python and matplotlib.
#!/usr/bin/env python3
# Compare different scenarios for Boulder entry fee in Tübingen
import numpy as np
import matplotlib.pyplot as plt
from pprint import pprint
def main():
Ns = np.arange(20,80)
scenarios = build_scenarios()
costs = { }
for key, scen in scenarios.items():
costs[key] = calculate_prices(scen, Ns)
costs_to_plot = { k : costs[k] for k in costs if k[0] == "S"}
plot_costs(costs_to_plot, title="Student")#, filename="student.png")
# costs_to_plot = { k : costs[k] for k in costs if k[0] == "N"}
# plot_costs(costs_to_plot, title="Normal")#, filename="normal.png")
def plot_costs(costs, title=None, filename=None):
fig, ax = plt.subplots(figsize = [6.4, 4.8])
for key, val in costs.items():
plot_cost_to_ax(ax, key, val)
ax.set_xlabel("Anzahl Eintritte")
ax.set_ylabel("Kosten [€]")
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.grid()
if title is not None:
plt.title(title)
if filename is not None:
fig.savefig(filename, dpi=600)
else:
plt.show()
def plot_cost_to_ax(ax, key, vals):
style = linestyles[key]
ax.plot( vals[0], vals[1], label=key, color=style[0], linestyle=style[1])
def build_scenarios():
# build derived scenarios with 11er card and year card
scenarios = { k:v for k,v in base_scenarios.items()}
# add scenarios with year card
for k,v in base_scenarios.items():
newv = dict(v)
newv["year"] = True
scenarios[k+"Y"] = newv
# add scenarios using cards of 11
for k,v in base_scenarios.items():
newv = dict(v)
newv["11er"] = True
scenarios[k+"11"] = newv
# add scenarios using climbing card
for k,v in base_scenarios.items():
if "climbing card" in prices_lib[k]:
newv = dict(v)
newv["climbing card"] = True
scenarios[k+"CC"] = newv
return scenarios
def calculate_prices(scen, num_entry):
prices = prices_lib[scen["prices"]]
cost = 0
# fix costs
for key in ["DAV", "year", "climbing card"]:
if scen[key]:
cost += prices[key]
if scen["year"]:
return (num_entry, cost*np.ones(len(num_entry)))
#num_11 = 0 if not scen["11er"] else np.floor(num_entry/11)
#cost += num_11*prices["11er"]
if scen["climbing card"]:
single_price = prices["climbing card entry"]
elif scen["11er"]:
single_price = prices["11er"] / 11
else:
single_price = prices["entry"]
num_single = num_entry
cost += single_price*num_single
return (num_entry, cost)
prices_fixed = {
"shoes" : 3,
"DAV" : 70,
}
prices_lib = {
"N" : {
"entry" : 13,
"11er" : 130,
"year" : 500,
},
"S" : {
"entry" : 11,
"11er" : 110,
"year" : 440,
},
"ND" : {
"entry" : 9,
"11er" : 90,
"year" : 380,
"climbing card" : 120,
"climbing card entry" : 5,
},
"SD" : {
"entry" : 8,
"11er" : 80,
"year" : 340,
"climbing card" : 100,
"climbing card entry" : 5,
}
}
# add fixed prices to each price category
for key, p in prices_lib.items():
for k,v in prices_fixed.items():
p[k] = v
base_scenarios = {
"N" : {
"prices" : "N",
"year" : False,
"11er" : False,
"DAV" : False,
"climbing card" : False
},
"S" : {
"prices" : "S",
"year" : False,
"11er" : False,
"DAV" : False,
"climbing card" : False
},
"ND" : {
"prices" : "ND",
"year" : False,
"11er" : False,
"DAV" : True,
"climbing card" : False
},
"SD" : {
"prices" : "SD",
"year" : False,
"11er" : False,
"DAV" : True,
"climbing card" : False
}
}
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
styles = {
"N" : ":",
"S" : ":",
"ND" : "-",
"SD" : "-"
}
linestyles = {}
for n , key in enumerate(styles):
ls = styles[key]
linestyles[key] = (colors[0], ls)
linestyles[key+"11"] = (colors[1], ls)
linestyles[key+"Y"] = (colors[2], ls)
linestyles[key+"CC"] = (colors[3], ls)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment