1374 lines
126 KiB
HTML
1374 lines
126 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||
|
||
<meta charset="utf-8">
|
||
<meta name="generator" content="quarto-1.6.42">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||
|
||
|
||
<title>Höhere Fahrradzählungen im Jahr 2023:</title>
|
||
<style>
|
||
code{white-space: pre-wrap;}
|
||
span.smallcaps{font-variant: small-caps;}
|
||
div.columns{display: flex; gap: min(4vw, 1.5em);}
|
||
div.column{flex: auto; overflow-x: auto;}
|
||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||
ul.task-list{list-style: none;}
|
||
ul.task-list li input[type="checkbox"] {
|
||
width: 0.8em;
|
||
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
|
||
vertical-align: middle;
|
||
}
|
||
/* CSS for syntax highlighting */
|
||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||
pre > code.sourceCode > span { line-height: 1.25; }
|
||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||
.sourceCode { overflow: visible; }
|
||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||
div.sourceCode { margin: 1em 0; }
|
||
pre.sourceCode { margin: 0; }
|
||
@media screen {
|
||
div.sourceCode { overflow: auto; }
|
||
}
|
||
@media print {
|
||
pre > code.sourceCode { white-space: pre-wrap; }
|
||
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
|
||
}
|
||
pre.numberSource code
|
||
{ counter-reset: source-line 0; }
|
||
pre.numberSource code > span
|
||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||
pre.numberSource code > span > a:first-child::before
|
||
{ content: counter(source-line);
|
||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||
border: none; display: inline-block;
|
||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||
-khtml-user-select: none; -moz-user-select: none;
|
||
-ms-user-select: none; user-select: none;
|
||
padding: 0 4px; width: 4em;
|
||
}
|
||
pre.numberSource { margin-left: 3em; padding-left: 4px; }
|
||
div.sourceCode
|
||
{ }
|
||
@media screen {
|
||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||
}
|
||
</style>
|
||
|
||
|
||
<script src="bikes_files/libs/clipboard/clipboard.min.js"></script>
|
||
<script src="bikes_files/libs/quarto-html/quarto.js"></script>
|
||
<script src="bikes_files/libs/quarto-html/popper.min.js"></script>
|
||
<script src="bikes_files/libs/quarto-html/tippy.umd.min.js"></script>
|
||
<script src="bikes_files/libs/quarto-html/anchor.min.js"></script>
|
||
<link href="bikes_files/libs/quarto-html/tippy.css" rel="stylesheet">
|
||
<link href="bikes_files/libs/quarto-html/quarto-syntax-highlighting-2f5df379a58b258e96c21c0638c20c03.css" rel="stylesheet" id="quarto-text-highlighting-styles">
|
||
<script src="bikes_files/libs/bootstrap/bootstrap.min.js"></script>
|
||
<link href="bikes_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||
<link href="bikes_files/libs/bootstrap/bootstrap-2edb3e7d1c5a5c188a4a7bf3899b3b30.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
|
||
|
||
|
||
</head>
|
||
|
||
<body class="fullcontent">
|
||
|
||
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
|
||
|
||
<main class="content" id="quarto-document-content">
|
||
|
||
<header id="title-block-header" class="quarto-title-block default">
|
||
<div class="quarto-title">
|
||
<h1 class="title">Höhere Fahrradzählungen im Jahr 2023:</h1>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="quarto-title-meta">
|
||
|
||
|
||
|
||
|
||
</div>
|
||
|
||
|
||
|
||
</header>
|
||
|
||
|
||
<div id="ea7d1a2b" class="cell">
|
||
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
|
||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
|
||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
|
||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> seaborn <span class="im">as</span> sns</span>
|
||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> holidays</span>
|
||
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> glob</span>
|
||
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> os</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<div id="181190f4" class="cell" data-execution_count="26">
|
||
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>pfad <span class="op">=</span> <span class="st">"../data/tageswerte_pro_Jahr/*.csv"</span></span>
|
||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>dateien <span class="op">=</span> glob.glob(pfad)</span>
|
||
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>dfs <span class="op">=</span> []</span>
|
||
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> datei <span class="kw">in</span> dateien:</span>
|
||
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> df_temp <span class="op">=</span> pd.read_csv(datei, low_memory<span class="op">=</span><span class="va">False</span>)</span>
|
||
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> df_temp[<span class="st">"year"</span>] <span class="op">=</span> os.path.basename(datei).split(<span class="st">"_"</span>)[<span class="dv">2</span>].split(<span class="st">"."</span>)[<span class="dv">0</span>]</span>
|
||
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> dfs.append(df_temp.astype({<span class="st">"year"</span>:<span class="bu">int</span>}))</span>
|
||
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.concat(dfs, ignore_index<span class="op">=</span><span class="va">True</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<div id="d3f4c318" class="cell" data-execution_count="3">
|
||
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Daten bereinigen</span></span>
|
||
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="co"># replace various forms of 'na' with np.nan</span></span>
|
||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> df.replace([<span class="st">"na"</span>, <span class="st">"NA"</span>, <span class="st">"Na"</span>], np.nan)</span>
|
||
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>df_backup <span class="op">=</span> df.copy()</span>
|
||
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.isna().<span class="bu">sum</span>())</span>
|
||
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co"># </span><span class="al">TODO</span><span class="co"> Find if temp rain and snow values are missing in a pattern or at random</span></span>
|
||
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="co"># convert numeric values to corresponding dtypes</span></span>
|
||
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"site_temperature"</span>] <span class="op">=</span> df[<span class="st">"site_temperature"</span>].astype(<span class="bu">float</span>)</span>
|
||
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"site_rain_accumulation"</span>] <span class="op">=</span> df[<span class="st">"site_rain_accumulation"</span>].astype(<span class="bu">float</span>)</span>
|
||
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"site_snow_accumulation"</span>] <span class="op">=</span> df[<span class="st">"site_snow_accumulation"</span>].astype(<span class="bu">float</span>)</span>
|
||
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="co"># Mannheim subset, saved to file</span></span>
|
||
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a>df_ma <span class="op">=</span> df[df[<span class="st">"domain_name"</span>] <span class="op">==</span> <span class="st">'Stadt Mannheim'</span>]</span>
|
||
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a>df_ma.to_csv(<span class="st">"../data/processed/daily_bikes_mannheim.csv"</span>, index<span class="op">=</span><span class="va">False</span>)</span>
|
||
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a><span class="co">#print(df_ma.isna().sum())</span></span>
|
||
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a><span class="co"># Wie ist das verhältnis über den gesammten Zeitraum von Regen und Schnee?</span></span>
|
||
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> [</span>
|
||
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a> <span class="st">"Januar"</span>, <span class="st">"Februar"</span>, <span class="st">"März"</span>, <span class="st">"April"</span>, <span class="st">"Mai"</span>, <span class="st">"Juni"</span>,</span>
|
||
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a> <span class="st">"Juli"</span>, <span class="st">"August"</span>, <span class="st">"September"</span>, <span class="st">"Oktober"</span>, <span class="st">"November"</span>, <span class="st">"Dezember"</span></span>
|
||
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a>]</span>
|
||
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a>df2 <span class="op">=</span> df.copy()</span>
|
||
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a>df2[<span class="st">"iso_timestamp"</span>] <span class="op">=</span> pd.to_datetime(</span>
|
||
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a> df2[<span class="st">"iso_timestamp"</span>],</span>
|
||
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a> errors<span class="op">=</span><span class="st">"coerce"</span>, <span class="co">#If 'coerce', then invalid parsing will be set as NaT.</span></span>
|
||
<span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a> utc<span class="op">=</span><span class="va">True</span></span>
|
||
<span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a>df_ma <span class="op">=</span> df2.loc[df2[<span class="st">"domain_name"</span>] <span class="op">==</span> <span class="st">"Stadt Mannheim"</span>]</span>
|
||
<span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a>df_ma <span class="op">=</span> df_ma.dropna(subset<span class="op">=</span>[<span class="st">"iso_timestamp"</span>])</span>
|
||
<span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"month"</span>] <span class="op">=</span> df_ma[<span class="st">"iso_timestamp"</span>].dt.month.astype(<span class="bu">int</span>)</span>
|
||
<span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"site_rain_accumulation"</span>] <span class="op">=</span> pd.to_numeric(df_ma[<span class="st">"site_rain_accumulation"</span>], errors<span class="op">=</span><span class="st">"coerce"</span>).clip(lower<span class="op">=</span><span class="dv">0</span>)</span>
|
||
<span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"site_snow_accumulation"</span>] <span class="op">=</span> pd.to_numeric(df_ma[<span class="st">"site_snow_accumulation"</span>], errors<span class="op">=</span><span class="st">"coerce"</span>).clip(lower<span class="op">=</span><span class="dv">0</span>)</span>
|
||
<span id="cb3-42"><a href="#cb3-42" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-43"><a href="#cb3-43" aria-hidden="true" tabindex="-1"></a>monthly <span class="op">=</span> (</span>
|
||
<span id="cb3-44"><a href="#cb3-44" aria-hidden="true" tabindex="-1"></a> df_ma.groupby(<span class="st">"month"</span>)[[<span class="st">"site_rain_accumulation"</span>, <span class="st">"site_snow_accumulation"</span>]]</span>
|
||
<span id="cb3-45"><a href="#cb3-45" aria-hidden="true" tabindex="-1"></a> .<span class="bu">sum</span>()</span>
|
||
<span id="cb3-46"><a href="#cb3-46" aria-hidden="true" tabindex="-1"></a> .reindex(<span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">13</span>), fill_value<span class="op">=</span><span class="dv">0</span>)</span>
|
||
<span id="cb3-47"><a href="#cb3-47" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb3-48"><a href="#cb3-48" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-49"><a href="#cb3-49" aria-hidden="true" tabindex="-1"></a>rain_sum <span class="op">=</span> monthly[<span class="st">"site_rain_accumulation"</span>].to_numpy()</span>
|
||
<span id="cb3-50"><a href="#cb3-50" aria-hidden="true" tabindex="-1"></a>snow_sum <span class="op">=</span> monthly[<span class="st">"site_snow_accumulation"</span>].to_numpy()</span>
|
||
<span id="cb3-51"><a href="#cb3-51" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-52"><a href="#cb3-52" aria-hidden="true" tabindex="-1"></a>x <span class="op">=</span> np.arange(<span class="dv">12</span>)</span>
|
||
<span id="cb3-53"><a href="#cb3-53" aria-hidden="true" tabindex="-1"></a>width <span class="op">=</span> <span class="fl">0.35</span></span>
|
||
<span id="cb3-54"><a href="#cb3-54" aria-hidden="true" tabindex="-1"></a>fig, ax <span class="op">=</span> plt.subplots()</span>
|
||
<span id="cb3-55"><a href="#cb3-55" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-56"><a href="#cb3-56" aria-hidden="true" tabindex="-1"></a>rects1 <span class="op">=</span> ax.bar(x <span class="op">-</span> width<span class="op">/</span><span class="dv">2</span>, rain_sum, width, label<span class="op">=</span><span class="st">"Rain"</span>)</span>
|
||
<span id="cb3-57"><a href="#cb3-57" aria-hidden="true" tabindex="-1"></a>rects2 <span class="op">=</span> ax.bar(x <span class="op">+</span> width<span class="op">/</span><span class="dv">2</span>, snow_sum, width, label<span class="op">=</span><span class="st">"Snow"</span>)</span>
|
||
<span id="cb3-58"><a href="#cb3-58" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-59"><a href="#cb3-59" aria-hidden="true" tabindex="-1"></a>ax.set_ylabel(<span class="st">"Akkumulation"</span>)</span>
|
||
<span id="cb3-60"><a href="#cb3-60" aria-hidden="true" tabindex="-1"></a>ax.set_title(<span class="st">"Monatliche Summe Regen vs. Schnee (Mannheim)"</span>)</span>
|
||
<span id="cb3-61"><a href="#cb3-61" aria-hidden="true" tabindex="-1"></a>ax.set_xticks(x, labels, rotation<span class="op">=</span><span class="dv">45</span>, ha<span class="op">=</span><span class="st">"right"</span>)</span>
|
||
<span id="cb3-62"><a href="#cb3-62" aria-hidden="true" tabindex="-1"></a>ax.legend()</span>
|
||
<span id="cb3-63"><a href="#cb3-63" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-64"><a href="#cb3-64" aria-hidden="true" tabindex="-1"></a>ax.bar_label(rects1, padding<span class="op">=</span><span class="dv">3</span>, fmt<span class="op">=</span><span class="st">"</span><span class="sc">%.1f</span><span class="st">"</span>)</span>
|
||
<span id="cb3-65"><a href="#cb3-65" aria-hidden="true" tabindex="-1"></a>ax.bar_label(rects2, padding<span class="op">=</span><span class="dv">3</span>, fmt<span class="op">=</span><span class="st">"</span><span class="sc">%.1f</span><span class="st">"</span>)</span>
|
||
<span id="cb3-66"><a href="#cb3-66" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-67"><a href="#cb3-67" aria-hidden="true" tabindex="-1"></a>fig.tight_layout()</span>
|
||
<span id="cb3-68"><a href="#cb3-68" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
|
||
<span id="cb3-69"><a href="#cb3-69" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-70"><a href="#cb3-70" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb3-71"><a href="#cb3-71" aria-hidden="true" tabindex="-1"></a><span class="co"># IDEE: Man könnte noch zeigen wie akurat Schnee / Regen sind, in dem man farblich darstellt ob die Temperatur > oder < 0 war.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>operator_name 0
|
||
domain_name 0
|
||
domain_id 0
|
||
counter_site 0
|
||
counter_site_id 0
|
||
counter_serial 1593
|
||
longitude 0
|
||
latitude 0
|
||
timezone 0
|
||
iso_timestamp 0
|
||
channels_in 26510
|
||
channels_out 26559
|
||
channels_unknown 220492
|
||
channels_all 0
|
||
site_temperature 20642
|
||
site_rain_accumulation 20642
|
||
site_snow_accumulation 20656
|
||
year 0
|
||
dtype: int64</code></pre>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-4-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="eb082a0d" class="cell" data-execution_count="4">
|
||
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Shorten der Labels für bessere Lesbarkeit im Plot</span></span>
|
||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>max_length <span class="op">=</span> <span class="dv">15</span></span>
|
||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"short_labels"</span>] <span class="op">=</span> df_ma[<span class="st">"counter_site"</span>].<span class="bu">apply</span>(<span class="kw">lambda</span> x: x[:max_length] <span class="op">+</span> (<span class="st">"…"</span> <span class="cf">if</span> <span class="bu">len</span>(x) <span class="op">></span> max_length <span class="cf">else</span> <span class="st">""</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<div id="ea13a213" class="cell" data-execution_count="5">
|
||
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>avg_per_year <span class="op">=</span> df_ma.groupby(<span class="st">"year"</span>)[<span class="st">"channels_all"</span>].mean()</span>
|
||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>years <span class="op">=</span> avg_per_year.index.astype(<span class="bu">str</span>)</span>
|
||
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>fig, axs <span class="op">=</span> plt.subplots(<span class="dv">1</span>,<span class="dv">2</span>, figsize<span class="op">=</span>(<span class="dv">15</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a>sns.barplot(x<span class="op">=</span>years, y<span class="op">=</span>avg_per_year.values, ax<span class="op">=</span>axs[<span class="dv">0</span>])</span>
|
||
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_yticks(<span class="bu">range</span>(<span class="dv">0</span>, <span class="dv">3501</span>, <span class="dv">500</span>))</span>
|
||
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">0</span>].set_yticklabels(<span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">3501</span>, <span class="dv">500</span>))</span>
|
||
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">0</span>].set_xlabel(<span class="st">"Year"</span>)</span>
|
||
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">0</span>].set_ylabel(<span class="st">"Average channels_all"</span>)</span>
|
||
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">0</span>].set_title(<span class="st">"Average daily observations per year (Mannheim 2014-2025)"</span>, fontsize<span class="op">=</span><span class="dv">16</span>)</span>
|
||
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a><span class="co"># WICHTIG Hier kommt der Abfall des Mittelwerts dadurch, dass mehr Zählstationen hinzugefügt wurden, die nicht an einem Hotspot standen, also Mittelwert sinkt.</span></span>
|
||
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a>plot_data <span class="op">=</span> pd.DataFrame({</span>
|
||
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> <span class="st">'Year'</span>: years,</span>
|
||
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> <span class="st">'Average'</span>: avg_per_year</span>
|
||
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a>})</span>
|
||
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a>sns.lineplot(x<span class="op">=</span><span class="st">'Year'</span>, y<span class="op">=</span><span class="st">'Average'</span>, data<span class="op">=</span>plot_data, marker<span class="op">=</span><span class="st">'o'</span>, ax<span class="op">=</span>axs[<span class="dv">1</span>])</span>
|
||
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_yticks(<span class="bu">range</span>(<span class="dv">0</span>, <span class="dv">3501</span>, <span class="dv">500</span>))</span>
|
||
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_yticklabels(<span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">3501</span>, <span class="dv">500</span>))</span>
|
||
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_title(<span class="st">"Average daily observations (Mannheim 2014-2025)"</span>, fontsize<span class="op">=</span><span class="dv">16</span>)</span>
|
||
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_xlabel(<span class="st">"Year"</span>, fontsize<span class="op">=</span><span class="dv">12</span>)</span>
|
||
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_ylabel(<span class="st">"Average Number of bikes"</span>, fontsize<span class="op">=</span><span class="dv">12</span>)</span>
|
||
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>/tmp/ipykernel_433306/1295171260.py:7: UserWarning: set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator.
|
||
axs[0].set_yticklabels(range(0,3501, 500))</code></pre>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-6-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="cf4ab2b6" class="cell" data-execution_count="6">
|
||
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>y <span class="op">=</span> df_ma.groupby(by<span class="op">=</span><span class="st">"year"</span>)[<span class="st">"counter_site"</span>].nunique()</span>
|
||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">6</span>))</span>
|
||
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>sns.lineplot(x<span class="op">=</span>y.index, y<span class="op">=</span>y.values, marker<span class="op">=</span><span class="st">'o'</span>)</span>
|
||
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Number of counting stations per year in Mannheim"</span>, fontsize<span class="op">=</span><span class="dv">16</span>)</span>
|
||
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Year"</span>, fontsize<span class="op">=</span><span class="dv">12</span>)</span>
|
||
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Number of stations"</span>, fontsize<span class="op">=</span><span class="dv">12</span>)</span>
|
||
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-7-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="f938cd69" class="cell" data-execution_count="7">
|
||
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># sollte man das normieren?</span></span>
|
||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>sum_per_year <span class="op">=</span> df_ma.groupby(<span class="st">"year"</span>)[<span class="st">"channels_all"</span>].<span class="bu">sum</span>()</span>
|
||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>sum_per_year <span class="op">=</span> sum_per_year.sort_index()</span>
|
||
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a>sns.barplot(x<span class="op">=</span>years, y<span class="op">=</span>sum_per_year.values)</span>
|
||
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Year"</span>)</span>
|
||
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Total observations in millions"</span>)</span>
|
||
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Total observations per year (Mannheim)"</span>)</span>
|
||
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-8-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="2993be48" class="cell" data-execution_count="8">
|
||
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"timestamp"</span>] <span class="op">=</span> pd.to_datetime(df_ma[<span class="st">"iso_timestamp"</span>])</span>
|
||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"year"</span>] <span class="op">=</span> df_ma[<span class="st">"timestamp"</span>].dt.year</span>
|
||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"month"</span>] <span class="op">=</span> df_ma[<span class="st">"timestamp"</span>].dt.month</span>
|
||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"date"</span>] <span class="op">=</span> df_ma[<span class="st">"timestamp"</span>].dt.date</span>
|
||
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a>df_2024 <span class="op">=</span> df_ma[df_ma[<span class="st">"year"</span>] <span class="op">==</span> <span class="dv">2024</span>].copy()</span>
|
||
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a>monthly_avg_2024 <span class="op">=</span> (df_2024.groupby(<span class="st">"month"</span>)[<span class="st">"channels_all"</span>].mean().reset_index())</span>
|
||
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a>plt.bar( monthly_avg_2024[<span class="st">"month"</span>], monthly_avg_2024[<span class="st">"channels_all"</span>], color<span class="op">=</span><span class="st">"steelblue"</span> ) </span>
|
||
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Average Daily Bike Counts per Month (Mannheim 2024)"</span>) </span>
|
||
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Month"</span>) </span>
|
||
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Average daily bike counts"</span>) </span>
|
||
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a>plt.xticks(<span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">13</span>)) </span>
|
||
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a>plt.grid(axis<span class="op">=</span><span class="st">"y"</span>, alpha<span class="op">=</span><span class="fl">0.3</span>) </span>
|
||
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true" tabindex="-1"></a>plt.tight_layout() </span>
|
||
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-9-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="46d1936b" class="cell" data-execution_count="9">
|
||
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"timestamp"</span>] <span class="op">=</span> pd.to_datetime(df_ma[<span class="st">"iso_timestamp"</span>])</span>
|
||
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"year"</span>] <span class="op">=</span> df_ma[<span class="st">"timestamp"</span>].dt.year</span>
|
||
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"month"</span>] <span class="op">=</span> df_ma[<span class="st">"timestamp"</span>].dt.month</span>
|
||
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"date"</span>] <span class="op">=</span> df_ma[<span class="st">"timestamp"</span>].dt.date</span>
|
||
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a>df_2023 <span class="op">=</span> df_ma[df_ma[<span class="st">"year"</span>] <span class="op">==</span> <span class="dv">2023</span>].copy()</span>
|
||
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a>monthly_avg_2023 <span class="op">=</span> (df_2023.groupby(<span class="st">"month"</span>)[<span class="st">"channels_all"</span>].mean().reset_index())</span>
|
||
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a>plt.bar( monthly_avg_2023[<span class="st">"month"</span>], monthly_avg_2023[<span class="st">"channels_all"</span>], color<span class="op">=</span><span class="st">"steelblue"</span> ) </span>
|
||
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Average Daily Bike Counts per Month (Mannheim 2023)"</span>) </span>
|
||
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Month"</span>) </span>
|
||
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Average daily bike counts"</span>) </span>
|
||
<span id="cb11-16"><a href="#cb11-16" aria-hidden="true" tabindex="-1"></a>plt.xticks(<span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">13</span>)) </span>
|
||
<span id="cb11-17"><a href="#cb11-17" aria-hidden="true" tabindex="-1"></a>plt.grid(axis<span class="op">=</span><span class="st">"y"</span>, alpha<span class="op">=</span><span class="fl">0.3</span>) </span>
|
||
<span id="cb11-18"><a href="#cb11-18" aria-hidden="true" tabindex="-1"></a>plt.tight_layout() </span>
|
||
<span id="cb11-19"><a href="#cb11-19" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-10-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Einfluss der BUGA‑Saison auf den Radverkehr in Mannheim? (14 April bis 8 Oktober)</p>
|
||
<div id="69ff22d9" class="cell" data-execution_count="10">
|
||
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>df_22_23_24 <span class="op">=</span> df_ma[df_ma[<span class="st">"year"</span>].isin([<span class="dv">2022</span>, <span class="dv">2023</span>,<span class="dv">2024</span>])].copy()</span>
|
||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a>monthly_sum <span class="op">=</span> (df_22_23_24.groupby([<span class="st">"year"</span>, <span class="st">"month"</span>])[<span class="st">"channels_all"</span>].<span class="bu">sum</span>().reset_index())</span>
|
||
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a>monthly_pivot <span class="op">=</span> monthly_sum.pivot(index<span class="op">=</span><span class="st">"month"</span>, columns<span class="op">=</span><span class="st">"year"</span>, values<span class="op">=</span><span class="st">"channels_all"</span>)</span>
|
||
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">12</span>, <span class="dv">6</span>))</span>
|
||
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a>monthly_pivot.plot(kind<span class="op">=</span><span class="st">"bar"</span>, figsize<span class="op">=</span>(<span class="dv">12</span>,<span class="dv">6</span>), width<span class="op">=</span><span class="fl">0.8</span>)</span>
|
||
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Total Monthly Bike Counts in Mannheim (2022 vs. 2023)"</span>)</span>
|
||
<span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Month"</span>)</span>
|
||
<span id="cb12-13"><a href="#cb12-13" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Total bike counts per month"</span>)</span>
|
||
<span id="cb12-14"><a href="#cb12-14" aria-hidden="true" tabindex="-1"></a>plt.xticks(<span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">12</span>), <span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">13</span>), rotation<span class="op">=</span><span class="dv">0</span>)</span>
|
||
<span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a>plt.grid(axis<span class="op">=</span><span class="st">"y"</span>, alpha<span class="op">=</span><span class="fl">0.3</span>)</span>
|
||
<span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a>plt.legend(title<span class="op">=</span><span class="st">"Year"</span>)</span>
|
||
<span id="cb12-17"><a href="#cb12-17" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-18"><a href="#cb12-18" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<pre><code><Figure size 1200x600 with 0 Axes></code></pre>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-11-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="5e75d24f" class="cell" data-execution_count="11">
|
||
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"t"</span>] <span class="op">=</span> pd.to_datetime(df_ma[<span class="st">"iso_timestamp"</span>], utc<span class="op">=</span><span class="va">True</span>).dt.tz_convert(<span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"metric"</span>] <span class="op">=</span> pd.to_numeric(df_ma[<span class="st">"channels_all"</span>], errors<span class="op">=</span><span class="st">"coerce"</span>)</span>
|
||
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> buga_sum(y):</span>
|
||
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> pd.Timestamp(<span class="ss">f"</span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">-04-14"</span>, tz<span class="op">=</span><span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> pd.Timestamp(<span class="ss">f"</span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">-10-08"</span>, tz<span class="op">=</span><span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> df_ma.loc[(df_ma[<span class="st">"t"</span>] <span class="op">>=</span> start) <span class="op">&</span> (df_ma[<span class="st">"t"</span>] <span class="op"><=</span> end), <span class="st">"metric"</span>].<span class="bu">sum</span>()</span>
|
||
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a>vals <span class="op">=</span> {y: buga_sum(y) <span class="cf">for</span> y <span class="kw">in</span> [<span class="dv">2022</span>, <span class="dv">2023</span>, <span class="dv">2024</span>]}</span>
|
||
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">8</span>,<span class="dv">5</span>))</span>
|
||
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a>bars <span class="op">=</span> plt.bar([<span class="st">"2022"</span>, <span class="st">"2023 (BUGA)"</span>, <span class="st">"2024"</span>], [vals[<span class="dv">2022</span>], vals[<span class="dv">2023</span>], vals[<span class="dv">2024</span>]],)</span>
|
||
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a>plt.bar_label(bars, padding<span class="op">=</span><span class="dv">3</span>, fmt<span class="op">=</span><span class="st">"%.0f"</span>)</span>
|
||
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Bike Counts During BUGA Period (14 Apr – 8 Oct)"</span>)</span>
|
||
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Total bike counts"</span>)</span>
|
||
<span id="cb14-17"><a href="#cb14-17" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb14-18"><a href="#cb14-18" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-12-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="a7193c9a" class="cell" data-execution_count="12">
|
||
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"year"</span>] <span class="op">=</span> df_ma[<span class="st">"t"</span>].dt.year</span>
|
||
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"month"</span>] <span class="op">=</span> df_ma[<span class="st">"t"</span>].dt.month</span>
|
||
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>df_buga <span class="op">=</span> df_ma[df_ma[<span class="st">"month"</span>].between(<span class="dv">4</span>, <span class="dv">10</span>)]</span>
|
||
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a>monthly <span class="op">=</span> df_buga.groupby([<span class="st">"year"</span>, <span class="st">"month"</span>])[<span class="st">"metric"</span>].<span class="bu">sum</span>().reset_index()</span>
|
||
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>,<span class="dv">6</span>))</span>
|
||
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> y <span class="kw">in</span> [<span class="dv">2022</span>, <span class="dv">2023</span>, <span class="dv">2024</span>]:</span>
|
||
<span id="cb15-10"><a href="#cb15-10" aria-hidden="true" tabindex="-1"></a> subset <span class="op">=</span> monthly[monthly[<span class="st">"year"</span>] <span class="op">==</span> y]</span>
|
||
<span id="cb15-11"><a href="#cb15-11" aria-hidden="true" tabindex="-1"></a> plt.plot(subset[<span class="st">"month"</span>], subset[<span class="st">"metric"</span>], marker<span class="op">=</span><span class="st">"o"</span>, label<span class="op">=</span><span class="bu">str</span>(y))</span>
|
||
<span id="cb15-12"><a href="#cb15-12" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb15-13"><a href="#cb15-13" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Monthly Bike Counts (April–October)"</span>)</span>
|
||
<span id="cb15-14"><a href="#cb15-14" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Month"</span>)</span>
|
||
<span id="cb15-15"><a href="#cb15-15" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Total bike counts"</span>)</span>
|
||
<span id="cb15-16"><a href="#cb15-16" aria-hidden="true" tabindex="-1"></a>plt.xticks(<span class="bu">range</span>(<span class="dv">4</span>,<span class="dv">11</span>))</span>
|
||
<span id="cb15-17"><a href="#cb15-17" aria-hidden="true" tabindex="-1"></a>plt.legend()</span>
|
||
<span id="cb15-18"><a href="#cb15-18" aria-hidden="true" tabindex="-1"></a>plt.grid(alpha<span class="op">=</span><span class="fl">0.3</span>)</span>
|
||
<span id="cb15-19"><a href="#cb15-19" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-13-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Im August gehen wohl viele in den Urlaub aber im September hat 2023 deutlich mehr Fahrradzählungen als 2022 und 2024</p>
|
||
<div id="f3c0497b" class="cell" data-execution_count="13">
|
||
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"weekday"</span>] <span class="op">=</span> df_ma[<span class="st">"t"</span>].dt.day_name()</span>
|
||
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> buga_period(y):</span>
|
||
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> pd.Timestamp(<span class="ss">f"</span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">-04-14"</span>, tz<span class="op">=</span><span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> pd.Timestamp(<span class="ss">f"</span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">-10-08"</span>, tz<span class="op">=</span><span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> df_ma[(df_ma[<span class="st">"t"</span>] <span class="op">>=</span> start) <span class="op">&</span> (df_ma[<span class="st">"t"</span>] <span class="op"><=</span> end)]</span>
|
||
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a>df_buga_2023 <span class="op">=</span> buga_period(<span class="dv">2023</span>)</span>
|
||
<span id="cb16-9"><a href="#cb16-9" aria-hidden="true" tabindex="-1"></a>df_buga_2022 <span class="op">=</span> buga_period(<span class="dv">2022</span>)</span>
|
||
<span id="cb16-10"><a href="#cb16-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb16-11"><a href="#cb16-11" aria-hidden="true" tabindex="-1"></a>sns.boxplot(data<span class="op">=</span>pd.concat([df_buga_2022.assign(year<span class="op">=</span><span class="st">"2022"</span>),df_buga_2023.assign(year<span class="op">=</span><span class="st">"2023"</span>)]), x<span class="op">=</span><span class="st">"weekday"</span>, y<span class="op">=</span><span class="st">"metric"</span>, hue<span class="op">=</span><span class="st">"year"</span>)</span>
|
||
<span id="cb16-12"><a href="#cb16-12" aria-hidden="true" tabindex="-1"></a>plt.xticks(rotation<span class="op">=</span><span class="dv">45</span>)</span>
|
||
<span id="cb16-13"><a href="#cb16-13" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Weekday Bike Counts During BUGA vs. 2022"</span>)</span>
|
||
<span id="cb16-14"><a href="#cb16-14" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-14-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="eed0fab8" class="cell" data-execution_count="14">
|
||
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>sns.barplot(df_ma[df_ma[<span class="st">"counter_site"</span>] <span class="op">==</span> <span class="st">"Renzstraße"</span>],x<span class="op">=</span><span class="st">"year"</span>, y<span class="op">=</span><span class="st">"channels_all"</span>)</span>
|
||
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="co"># sns.barplot(df_ma[df_ma["counter_site"] == "Kurpfalzbrücke"],x="year", y="channels_all")</span></span>
|
||
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-15-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="ab084d8d" class="cell" data-execution_count="15">
|
||
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>station_sum <span class="op">=</span> df_ma.groupby(<span class="st">"short_labels"</span>)[<span class="st">"channels_all"</span>].<span class="bu">sum</span>().sort_values(ascending<span class="op">=</span><span class="va">False</span>)</span>
|
||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>year_counts <span class="op">=</span> df_ma.groupby(<span class="st">"short_labels"</span>)[<span class="st">"year"</span>].nunique()</span>
|
||
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>year_counts <span class="op">=</span> year_counts[station_sum.index] <span class="co"># Reorder year_counts to match the sorted station_sum</span></span>
|
||
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">7</span>))</span>
|
||
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a>sns.barplot(x<span class="op">=</span>station_sum.index, y<span class="op">=</span>station_sum.values, hue<span class="op">=</span>year_counts)</span>
|
||
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a>plt.xticks(rotation<span class="op">=</span><span class="dv">75</span>)</span>
|
||
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Station"</span>)</span>
|
||
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Total observations (2014-2024) in millions"</span>)</span>
|
||
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Total observations per station (Mannheim)"</span>)</span>
|
||
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-16-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<section id="größtes-radverkehrsaufkommen-nach-städten" class="level3">
|
||
<h3 class="anchored" data-anchor-id="größtes-radverkehrsaufkommen-nach-städten">Größtes Radverkehrsaufkommen nach Städten</h3>
|
||
<p>Da die Zählstellen zu unterschiedlichen Zeitpunkten in Betrieb genommen wurden, wurde für vergleichende Analysen ein gemeinsamer Beobachtungszeitraum definiert. Hierzu wurde für jede Zählstelle das erste Jahr mit verfügbaren Messdaten bestimmt. In der Analyse wurden ausschließlich Zählstellen berücksichtigt, die spätestens ab dem Jahr 2018 Daten aufweisen.</p>
|
||
<div id="65437931" class="cell" data-execution_count="16">
|
||
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>station_coverage <span class="op">=</span> (</span>
|
||
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> df.groupby(<span class="st">"counter_site"</span>)</span>
|
||
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> .agg(</span>
|
||
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> first_year<span class="op">=</span>(<span class="st">"year"</span>, <span class="st">"min"</span>),</span>
|
||
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> last_year<span class="op">=</span>(<span class="st">"year"</span>, <span class="st">"max"</span>),</span>
|
||
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> n_observations<span class="op">=</span>(<span class="st">"year"</span>, <span class="st">"count"</span>)</span>
|
||
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a> )</span>
|
||
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a> .reset_index()</span>
|
||
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a>station_coverage.sort_values(<span class="st">"first_year"</span>)</span>
|
||
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a>valid_stations <span class="op">=</span> station_coverage[</span>
|
||
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a> station_coverage[<span class="st">"first_year"</span>] <span class="op"><=</span> <span class="dv">2018</span></span>
|
||
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a>][<span class="st">"counter_site"</span>]</span>
|
||
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb19-16"><a href="#cb19-16" aria-hidden="true" tabindex="-1"></a>df_common <span class="op">=</span> df[</span>
|
||
<span id="cb19-17"><a href="#cb19-17" aria-hidden="true" tabindex="-1"></a> (df[<span class="st">"counter_site"</span>].isin(valid_stations)) <span class="op">&</span></span>
|
||
<span id="cb19-18"><a href="#cb19-18" aria-hidden="true" tabindex="-1"></a> (df[<span class="st">"year"</span>] <span class="op">>=</span> <span class="dv">2018</span>)</span>
|
||
<span id="cb19-19"><a href="#cb19-19" aria-hidden="true" tabindex="-1"></a>]</span>
|
||
<span id="cb19-20"><a href="#cb19-20" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">6</span>))</span>
|
||
<span id="cb19-21"><a href="#cb19-21" aria-hidden="true" tabindex="-1"></a>sns.histplot(station_coverage[<span class="st">"first_year"</span>], bins<span class="op">=</span><span class="bu">range</span>(<span class="dv">2013</span>, <span class="dv">2026</span>))</span>
|
||
<span id="cb19-22"><a href="#cb19-22" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Startjahr der Zählstelle"</span>)</span>
|
||
<span id="cb19-23"><a href="#cb19-23" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Anzahl der Zählstellen"</span>)</span>
|
||
<span id="cb19-24"><a href="#cb19-24" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Verteilung der Inbetriebnahmejahre der Fahrradzählstellen"</span>)</span>
|
||
<span id="cb19-25"><a href="#cb19-25" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
|
||
<span id="cb19-26"><a href="#cb19-26" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb19-27"><a href="#cb19-27" aria-hidden="true" tabindex="-1"></a><span class="co"># Top 10 Zählstellen</span></span>
|
||
<span id="cb19-28"><a href="#cb19-28" aria-hidden="true" tabindex="-1"></a>avg_df_common <span class="op">=</span> (</span>
|
||
<span id="cb19-29"><a href="#cb19-29" aria-hidden="true" tabindex="-1"></a> df_common.groupby([<span class="st">"counter_site"</span>, <span class="st">"domain_name"</span>], as_index<span class="op">=</span><span class="va">False</span>)</span>
|
||
<span id="cb19-30"><a href="#cb19-30" aria-hidden="true" tabindex="-1"></a> .agg(avg_daily_counts<span class="op">=</span>(<span class="st">"channels_all"</span>, <span class="st">"mean"</span>))</span>
|
||
<span id="cb19-31"><a href="#cb19-31" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb19-32"><a href="#cb19-32" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb19-33"><a href="#cb19-33" aria-hidden="true" tabindex="-1"></a><span class="co"># Top 10 Zählstellen basierend auf dem durchschnittlichen täglichen Fahrradverkehr</span></span>
|
||
<span id="cb19-34"><a href="#cb19-34" aria-hidden="true" tabindex="-1"></a>top10_common <span class="op">=</span> avg_df_common.sort_values(<span class="st">"avg_daily_counts"</span>, ascending<span class="op">=</span><span class="va">False</span>).head(<span class="dv">10</span>)</span>
|
||
<span id="cb19-35"><a href="#cb19-35" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb19-36"><a href="#cb19-36" aria-hidden="true" tabindex="-1"></a><span class="co"># Visualisierung der Top 10 Zählstellen</span></span>
|
||
<span id="cb19-37"><a href="#cb19-37" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">15</span>, <span class="dv">6</span>))</span>
|
||
<span id="cb19-38"><a href="#cb19-38" aria-hidden="true" tabindex="-1"></a>sns.barplot(</span>
|
||
<span id="cb19-39"><a href="#cb19-39" aria-hidden="true" tabindex="-1"></a> data<span class="op">=</span>top10_common,</span>
|
||
<span id="cb19-40"><a href="#cb19-40" aria-hidden="true" tabindex="-1"></a> y<span class="op">=</span><span class="st">"counter_site"</span>,</span>
|
||
<span id="cb19-41"><a href="#cb19-41" aria-hidden="true" tabindex="-1"></a> x<span class="op">=</span><span class="st">"avg_daily_counts"</span>,</span>
|
||
<span id="cb19-42"><a href="#cb19-42" aria-hidden="true" tabindex="-1"></a> hue<span class="op">=</span><span class="st">"domain_name"</span></span>
|
||
<span id="cb19-43"><a href="#cb19-43" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb19-44"><a href="#cb19-44" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb19-45"><a href="#cb19-45" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Zählstelle"</span>)</span>
|
||
<span id="cb19-46"><a href="#cb19-46" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Durchschnittliche Fahrradfahrten pro Tag"</span>)</span>
|
||
<span id="cb19-47"><a href="#cb19-47" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Top 10 Zählstellen mit dem höchsten durchschnittlichen Fahrradverkehr</span><span class="ch">\n</span><span class="st">(2018 und später)"</span>)</span>
|
||
<span id="cb19-48"><a href="#cb19-48" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb19-49"><a href="#cb19-49" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-17-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-17-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="d239120a" class="cell" data-execution_count="17">
|
||
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co"># </span><span class="al">TODO</span><span class="co"> welche Station hat den größten Messwertsprung innerhalb eines Jahres erlebt -> woran lag das</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<div id="ad8639cd" class="cell" data-execution_count="18">
|
||
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>max_length <span class="op">=</span> <span class="dv">15</span></span>
|
||
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">20</span>, <span class="dv">10</span>))</span>
|
||
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a>sns.boxplot(data<span class="op">=</span>df_ma, x<span class="op">=</span><span class="st">"short_labels"</span>, y<span class="op">=</span><span class="st">"channels_all"</span>)</span>
|
||
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a>plt.xticks(rotation<span class="op">=</span><span class="dv">45</span>)</span>
|
||
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Daily observations"</span>)</span>
|
||
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Distribution of daily counts per station (Stadt Mannheim - 2014-2024)"</span>)</span>
|
||
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
|
||
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"timestamp"</span>] <span class="op">=</span> pd.to_datetime(df_ma[<span class="st">"iso_timestamp"</span>])</span>
|
||
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a><span class="co">#Renzstraße</span></span>
|
||
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a>renz <span class="op">=</span> df_ma[df_ma[<span class="st">"counter_site"</span>] <span class="op">==</span> <span class="st">"Renzstraße"</span>]</span>
|
||
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a>renz_max <span class="op">=</span> renz.loc[renz[<span class="st">"channels_all"</span>].idxmax()]</span>
|
||
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Renzstraße - Höchster Wert:"</span>)</span>
|
||
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(renz_max[[<span class="st">"date"</span>, <span class="st">"channels_all"</span>]])</span>
|
||
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a><span class="co">#Kurpfalzbrücke</span></span>
|
||
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true" tabindex="-1"></a>kurp <span class="op">=</span> df_ma[df_ma[<span class="st">"counter_site"</span>] <span class="op">==</span> <span class="st">"Kurpfalzbrücke"</span>]</span>
|
||
<span id="cb21-21"><a href="#cb21-21" aria-hidden="true" tabindex="-1"></a>kurp_max <span class="op">=</span> kurp.loc[kurp[<span class="st">"channels_all"</span>].idxmax()]</span>
|
||
<span id="cb21-22"><a href="#cb21-22" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Kurpfalzbrücke - Höchster Wert:"</span>)</span>
|
||
<span id="cb21-23"><a href="#cb21-23" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(kurp_max[[<span class="st">"date"</span>, <span class="st">"channels_all"</span>]]) </span>
|
||
<span id="cb21-24"><a href="#cb21-24" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb21-25"><a href="#cb21-25" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"am 11.04.2018 gab es in Mannheim einen Warnstreik im öffentlichen Dienst und mehrere tausend Menschen nahmen an Demonstrationen teil."</span>)</span>
|
||
<span id="cb21-26"><a href="#cb21-26" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Das erklärt die Ausreißer bei den Fahrradzählungen an diesem Tag."</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-19-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>Renzstraße - Höchster Wert:
|
||
date 2018-04-10
|
||
channels_all 7638
|
||
Name: 239807, dtype: object
|
||
Kurpfalzbrücke - Höchster Wert:
|
||
date 2018-04-10
|
||
channels_all 10558
|
||
Name: 245866, dtype: object
|
||
am 11.04.2018 gab es in Mannheim einen Warnstreik im öffentlichen Dienst und mehrere tausend Menschen nahmen an Demonstrationen teil.
|
||
Das erklärt die Ausreißer bei den Fahrradzählungen an diesem Tag.</code></pre>
|
||
</div>
|
||
</div>
|
||
<div id="de88a7d7" class="cell" data-execution_count="19">
|
||
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">'day_of_week'</span>] <span class="op">=</span> df_ma[<span class="st">'timestamp'</span>].dt.dayofweek <span class="co"># 0=Monday, 6=Sunday</span></span>
|
||
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">'month'</span>] <span class="op">=</span> df_ma[<span class="st">'timestamp'</span>].dt.month</span>
|
||
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">'is_weekend'</span>] <span class="op">=</span> df_ma[<span class="st">'day_of_week'</span>].isin([<span class="dv">5</span>, <span class="dv">6</span>]).astype(<span class="bu">int</span>) <span class="co"># 1 if weekend, 0 if weekday</span></span>
|
||
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a><span class="co"># You can manually add holidays or use a library like `holidays` to check if the day is a public holiday.</span></span>
|
||
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a>de_holidays <span class="op">=</span> holidays.Germany(years<span class="op">=</span><span class="dv">2023</span>) <span class="co"># For Germany, for example</span></span>
|
||
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">'is_holiday'</span>] <span class="op">=</span> df_ma[<span class="st">'timestamp'</span>].dt.date.isin(de_holidays.keys()).astype(<span class="bu">int</span>)</span>
|
||
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a>variables <span class="op">=</span> [<span class="st">'day_of_week'</span>,<span class="st">'month'</span>,<span class="st">'is_weekend'</span>, <span class="st">'is_holiday'</span>, <span class="st">'channels_all'</span>, <span class="st">'site_temperature'</span>, <span class="st">'site_rain_accumulation'</span>]</span>
|
||
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true" tabindex="-1"></a><span class="co"># Create the scatterplot matrix using Seaborn's pairplot</span></span>
|
||
<span id="cb23-12"><a href="#cb23-12" aria-hidden="true" tabindex="-1"></a>sns.pairplot(df_ma[variables], diag_kind<span class="op">=</span><span class="st">'kde'</span>, plot_kws<span class="op">=</span>{<span class="st">'alpha'</span>: <span class="fl">0.5</span>, <span class="st">'s'</span>: <span class="dv">50</span>}, hue<span class="op">=</span><span class="st">"is_holiday"</span>)</span>
|
||
<span id="cb23-13"><a href="#cb23-13" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb23-14"><a href="#cb23-14" aria-hidden="true" tabindex="-1"></a><span class="co"># Customize the plot</span></span>
|
||
<span id="cb23-15"><a href="#cb23-15" aria-hidden="true" tabindex="-1"></a>plt.suptitle(<span class="st">'Matrix Scatterplot of Bike Counting Data'</span>, fontsize<span class="op">=</span><span class="dv">16</span>)</span>
|
||
<span id="cb23-16"><a href="#cb23-16" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-20-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="121023cc" class="cell" data-execution_count="20">
|
||
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co"># is_weekend und channels_all vergleichen</span></span>
|
||
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>sum_weekdays <span class="op">=</span> <span class="bu">sum</span>(df_ma[df_ma[<span class="st">"is_weekend"</span>]<span class="op">==</span><span class="dv">0</span>][<span class="st">"channels_all"</span>])</span>
|
||
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>sum_weekends <span class="op">=</span> <span class="bu">sum</span>(df_ma[df_ma[<span class="st">"is_weekend"</span>]<span class="op">==</span><span class="dv">1</span>][<span class="st">"channels_all"</span>])</span>
|
||
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a>num_weekdays <span class="op">=</span> <span class="bu">len</span>(df_ma[df_ma[<span class="st">"is_weekend"</span>]<span class="op">==</span><span class="dv">0</span>])<span class="co"># Since each row is an hour, divide by 24 to get the number of days</span></span>
|
||
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a>num_weekends <span class="op">=</span> <span class="bu">len</span>(df_ma[df_ma[<span class="st">"is_weekend"</span>]<span class="op">==</span><span class="dv">1</span>]) </span>
|
||
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a>avg_weekdays <span class="op">=</span> sum_weekdays <span class="op">/</span> num_weekdays</span>
|
||
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a>avg_weekends <span class="op">=</span> sum_weekends <span class="op">/</span> num_weekends</span>
|
||
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="korrelation-zwischen-temperatur-und-fahrradnutzung" class="level1">
|
||
<h1>Korrelation zwischen Temperatur und Fahrradnutzung</h1>
|
||
<div id="95626fa7" class="cell" data-execution_count="21">
|
||
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>fig, axs <span class="op">=</span> plt.subplots(<span class="dv">1</span>,<span class="dv">2</span>,figsize<span class="op">=</span>(<span class="dv">15</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>sns.scatterplot(data<span class="op">=</span>df_ma, y<span class="op">=</span><span class="st">"site_temperature"</span>, x<span class="op">=</span><span class="st">"channels_all"</span>, ax<span class="op">=</span>axs[<span class="dv">0</span>])</span>
|
||
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a>sns.regplot(data<span class="op">=</span>df_ma, y<span class="op">=</span><span class="st">"site_temperature"</span>, x<span class="op">=</span><span class="st">"channels_all"</span>,scatter_kws<span class="op">=</span>{<span class="st">'s'</span>: <span class="dv">10</span>}, line_kws<span class="op">=</span>{<span class="st">'color'</span>: <span class="st">'red'</span>}, ax<span class="op">=</span>axs[<span class="dv">1</span>])</span>
|
||
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Site temperature"</span>)</span>
|
||
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Bike counts"</span>) </span>
|
||
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">0</span>].set_title(<span class="st">"Temperature vs. bicycle counts (Stadt Mannheim)"</span>)</span>
|
||
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
|
||
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Pearson</span></span>
|
||
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a>pearson_ma <span class="op">=</span> df_ma[[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]].corr(method<span class="op">=</span><span class="st">"pearson"</span>).loc[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]</span>
|
||
<span id="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb25-12"><a href="#cb25-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Spearman</span></span>
|
||
<span id="cb25-13"><a href="#cb25-13" aria-hidden="true" tabindex="-1"></a>spearman_ma <span class="op">=</span> df_ma[[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]].corr(method<span class="op">=</span><span class="st">"spearman"</span>).loc[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]</span>
|
||
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb25-15"><a href="#cb25-15" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Pearson: </span><span class="sc">{</span>pearson_ma<span class="sc">:.3f}</span><span class="ss">"</span>)</span>
|
||
<span id="cb25-16"><a href="#cb25-16" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Spearman: </span><span class="sc">{</span>spearman_ma<span class="sc">:.3f}</span><span class="ss">"</span>)</span>
|
||
<span id="cb25-17"><a href="#cb25-17" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb25-18"><a href="#cb25-18" aria-hidden="true" tabindex="-1"></a><span class="co"># Drop all nan values in site_temperature from big dataframe</span></span>
|
||
<span id="cb25-19"><a href="#cb25-19" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"site_temperature"</span>] <span class="op">=</span> df[<span class="st">"site_temperature"</span>].replace([<span class="st">"na"</span>, <span class="st">"NA"</span>, <span class="st">"Na"</span>], np.nan)</span>
|
||
<span id="cb25-20"><a href="#cb25-20" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> df.dropna(subset<span class="op">=</span>[<span class="st">"site_temperature"</span>])</span>
|
||
<span id="cb25-21"><a href="#cb25-21" aria-hidden="true" tabindex="-1"></a>pearson_all <span class="op">=</span> df[[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]].corr(method<span class="op">=</span><span class="st">"pearson"</span>).loc[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]</span>
|
||
<span id="cb25-22"><a href="#cb25-22" aria-hidden="true" tabindex="-1"></a>spearman_all <span class="op">=</span> df[[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]].corr(method<span class="op">=</span><span class="st">"spearman"</span>).loc[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]</span>
|
||
<span id="cb25-23"><a href="#cb25-23" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb25-24"><a href="#cb25-24" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Pearson: </span><span class="sc">{</span>pearson_all<span class="sc">:.3f}</span><span class="ss">"</span>)</span>
|
||
<span id="cb25-25"><a href="#cb25-25" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Spearman: </span><span class="sc">{</span>spearman_all<span class="sc">:.3f}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-22-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>Pearson: 0.202
|
||
Spearman: 0.223
|
||
Pearson: 0.195
|
||
Spearman: 0.272</code></pre>
|
||
</div>
|
||
</div>
|
||
<section id="die-insgesamte-korrelation-zwischen-der-temperatur-und-der-fahrradnutzung-in-mannheim-beträgt-nach-pearson-0.202." class="level4">
|
||
<h4 class="anchored" data-anchor-id="die-insgesamte-korrelation-zwischen-der-temperatur-und-der-fahrradnutzung-in-mannheim-beträgt-nach-pearson-0.202.">Die insgesamte Korrelation zwischen der Temperatur und der Fahrradnutzung in Mannheim beträgt nach Pearson 0.202.</h4>
|
||
</section>
|
||
<section id="betrachtet-man-jedoch-die-korrelation-der-einzelnen-der-messstationen-so-ergeben-sich-folgende-werte." class="level4">
|
||
<h4 class="anchored" data-anchor-id="betrachtet-man-jedoch-die-korrelation-der-einzelnen-der-messstationen-so-ergeben-sich-folgende-werte.">Betrachtet man jedoch die Korrelation der einzelnen der Messstationen so ergeben sich folgende Werte.</h4>
|
||
<div id="46404807" class="cell" data-execution_count="22">
|
||
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>corr_per_station <span class="op">=</span> (</span>
|
||
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> df_ma.groupby(<span class="st">"counter_site"</span>)[[<span class="st">"channels_all"</span>, <span class="st">"site_temperature"</span>]]</span>
|
||
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> .corr(method<span class="op">=</span><span class="st">"pearson"</span>)</span>
|
||
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> .reset_index()</span>
|
||
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a>)</span>
|
||
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Nur die Zeilen channels_all vs. site_temperature extrahieren</span></span>
|
||
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a>corr_per_station <span class="op">=</span> corr_per_station[</span>
|
||
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a> (corr_per_station[<span class="st">"level_1"</span>] <span class="op">==</span> <span class="st">"site_temperature"</span>) <span class="op">&</span></span>
|
||
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a> (corr_per_station[<span class="st">"counter_site"</span>].notna())</span>
|
||
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true" tabindex="-1"></a>][[<span class="st">"counter_site"</span>, <span class="st">"channels_all"</span>]].rename(columns<span class="op">=</span>{<span class="st">"channels_all"</span>: <span class="st">"pearson_corr"</span>})</span>
|
||
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(corr_per_station.sort_values(<span class="st">"pearson_corr"</span>))</span>
|
||
<span id="cb27-14"><a href="#cb27-14" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb27-15"><a href="#cb27-15" aria-hidden="true" tabindex="-1"></a>corr_per_station_sorted <span class="op">=</span> corr_per_station.sort_values(<span class="st">"pearson_corr"</span>)</span>
|
||
<span id="cb27-16"><a href="#cb27-16" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb27-17"><a href="#cb27-17" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">10</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb27-18"><a href="#cb27-18" aria-hidden="true" tabindex="-1"></a>plt.barh(corr_per_station_sorted[<span class="st">"counter_site"</span>], corr_per_station_sorted[<span class="st">"pearson_corr"</span>])</span>
|
||
<span id="cb27-19"><a href="#cb27-19" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"Pearson correlation (site_temperature vs. channels_all)"</span>)</span>
|
||
<span id="cb27-20"><a href="#cb27-20" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"Station"</span>)</span>
|
||
<span id="cb27-21"><a href="#cb27-21" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Correlation between temperature and bicycle counts by station"</span>)</span>
|
||
<span id="cb27-22"><a href="#cb27-22" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb27-23"><a href="#cb27-23" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code> counter_site pearson_corr
|
||
1 B38. RI. AUS 0.011942
|
||
15 Lindenhofüberführung 0.235979
|
||
25 Theodor-Heuss-Anlage. RI. AUS 0.393459
|
||
21 Renzstraße 0.445103
|
||
19 Neckarauer Übergang -Schwetzinger Str. 0.486533
|
||
5 Feudenheimerstr. stadteinwärts 0.510540
|
||
27 Theodor-Heuss-Anlage. RI. IN. 0.510875
|
||
7 Feudenheimstr. stadtauswärts 0.533080
|
||
13 Kurpfalzbrücke 0.548310
|
||
9 Jungbuschbrücke 0.557844
|
||
11 Konrad-Adenauer-Brücke 0.568114
|
||
17 Luzenbergstr. 0.584917
|
||
3 Fernmeldeturm. 0.619432
|
||
23 Schlosspark Lindenhof (Richtung Jugendherberge) 0.629848</code></pre>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-23-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>An den meisten Stationen ist die Korrelation positiv (d.h. wärmere Tage → mehr Radverkehr).“ „Die Stärke variiert jedoch deutlich zwischen den Stationen (von ~0 bis ~0.63).“ „Insbesondere an Stationen X, Y, Z ist der Zusammenhang besonders stark.“</p>
|
||
<div id="56606822" class="cell" data-execution_count="23">
|
||
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="co">#rain category</span></span>
|
||
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"rain_category"</span>] <span class="op">=</span> pd.cut(df_ma[<span class="st">"site_rain_accumulation"</span>], bins<span class="op">=</span>[<span class="op">-</span><span class="fl">0.1</span>, <span class="dv">0</span>, <span class="fl">2.5</span>, <span class="fl">7.6</span>, <span class="dv">50</span>], labels<span class="op">=</span>[<span class="st">"No Rain"</span>, <span class="st">"Light Rain"</span>, <span class="st">"Moderate Rain"</span>, <span class="st">"Heavy Rain"</span>]) </span>
|
||
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a>fig, axs <span class="op">=</span> plt.subplots(<span class="dv">1</span>,<span class="dv">2</span>,figsize<span class="op">=</span>(<span class="dv">15</span>, <span class="dv">5</span>))</span>
|
||
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a>sns.regplot(data<span class="op">=</span>df_ma, y<span class="op">=</span><span class="st">"site_rain_accumulation"</span>, x<span class="op">=</span><span class="st">"channels_all"</span>,scatter_kws<span class="op">=</span>{<span class="st">'s'</span>: <span class="dv">10</span>, <span class="st">'alpha'</span>:<span class="fl">0.5</span>}, line_kws<span class="op">=</span>{<span class="st">'color'</span>: <span class="st">'red'</span>}, ax<span class="op">=</span>axs[<span class="dv">0</span>])</span>
|
||
<span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"rain accumulation"</span>)</span>
|
||
<span id="cb29-10"><a href="#cb29-10" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">"bike counts"</span>) </span>
|
||
<span id="cb29-11"><a href="#cb29-11" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">0</span>].set_title(<span class="st">"rain vs. bicycle counts (Stadt Mannheim)"</span>)</span>
|
||
<span id="cb29-12"><a href="#cb29-12" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb29-13"><a href="#cb29-13" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb29-14"><a href="#cb29-14" aria-hidden="true" tabindex="-1"></a>sns.violinplot( data<span class="op">=</span>df_ma, x<span class="op">=</span><span class="st">"rain_category"</span>, y<span class="op">=</span><span class="st">"channels_all"</span>,palette<span class="op">=</span><span class="st">"Blues"</span>, legend <span class="op">=</span> <span class="va">False</span>, ax<span class="op">=</span>axs[<span class="dv">1</span>]) </span>
|
||
<span id="cb29-15"><a href="#cb29-15" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_title(<span class="st">"Bike counts by rain category"</span>) </span>
|
||
<span id="cb29-16"><a href="#cb29-16" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_xlabel(<span class="st">"Rain category"</span>) </span>
|
||
<span id="cb29-17"><a href="#cb29-17" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].set_ylabel(<span class="st">"Bike counts"</span>) </span>
|
||
<span id="cb29-18"><a href="#cb29-18" aria-hidden="true" tabindex="-1"></a>axs[<span class="dv">1</span>].grid(axis<span class="op">=</span><span class="st">"y"</span>, alpha<span class="op">=</span><span class="fl">0.3</span>)</span>
|
||
<span id="cb29-19"><a href="#cb29-19" aria-hidden="true" tabindex="-1"></a>plt.tight_layout() </span>
|
||
<span id="cb29-20"><a href="#cb29-20" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
|
||
<span id="cb29-21"><a href="#cb29-21" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb29-22"><a href="#cb29-22" aria-hidden="true" tabindex="-1"></a><span class="co"># Spearman</span></span>
|
||
<span id="cb29-23"><a href="#cb29-23" aria-hidden="true" tabindex="-1"></a>spearman_ma <span class="op">=</span> ( df_ma[[<span class="st">"channels_all"</span>, <span class="st">"site_rain_accumulation"</span>]] .corr(method<span class="op">=</span><span class="st">"spearman"</span>) .loc[<span class="st">"channels_all"</span>, <span class="st">"site_rain_accumulation"</span>] ) </span>
|
||
<span id="cb29-24"><a href="#cb29-24" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Spearman (Mannheim): </span><span class="sc">{</span>spearman_ma<span class="sc">:.3f}</span><span class="ss">"</span>) </span>
|
||
<span id="cb29-25"><a href="#cb29-25" aria-hidden="true" tabindex="-1"></a><span class="co"># Gesamtdaten bereinigen </span></span>
|
||
<span id="cb29-26"><a href="#cb29-26" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"site_rain_accumulation"</span>] <span class="op">=</span> df[<span class="st">"site_rain_accumulation"</span>].replace([<span class="st">"na"</span>, <span class="st">"NA"</span>, <span class="st">"Na"</span>], np.nan) </span>
|
||
<span id="cb29-27"><a href="#cb29-27" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> df.dropna(subset<span class="op">=</span>[<span class="st">"site_rain_accumulation"</span>]) </span>
|
||
<span id="cb29-28"><a href="#cb29-28" aria-hidden="true" tabindex="-1"></a>spearman_all <span class="op">=</span> ( df[[<span class="st">"channels_all"</span>, <span class="st">"site_rain_accumulation"</span>]].corr(method<span class="op">=</span><span class="st">"spearman"</span>).loc[<span class="st">"channels_all"</span>, <span class="st">"site_rain_accumulation"</span>] ) </span>
|
||
<span id="cb29-29"><a href="#cb29-29" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Spearman (alle Städte): </span><span class="sc">{</span>spearman_all<span class="sc">:.3f}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>/tmp/ipykernel_433306/3267416270.py:14: FutureWarning:
|
||
|
||
Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same effect.
|
||
|
||
sns.violinplot( data=df_ma, x="rain_category", y="channels_all",palette="Blues", legend = False, ax=axs[1])</code></pre>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-24-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>Spearman (Mannheim): -0.100
|
||
Spearman (alle Städte): -0.114</code></pre>
|
||
</div>
|
||
</div>
|
||
<div id="80a7ede1" class="cell" data-execution_count="24">
|
||
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Auswirkung df_maer Einführung df_maes 9-Euro-Tickets vom 1. Juni 2022 bis zum 31. August 2022 im Vergleich zum Jahr df_maavor undf_ma df_maanach</span></span>
|
||
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a><span class="co">#In Mannheim</span></span>
|
||
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"t"</span>] <span class="op">=</span> pd.to_datetime(df_ma[<span class="st">"iso_timestamp"</span>].astype(<span class="bu">str</span>).<span class="bu">str</span>.strip(), errors<span class="op">=</span><span class="st">"coerce"</span>, utc<span class="op">=</span><span class="va">True</span>)</span>
|
||
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a>df_ma <span class="op">=</span> df_ma.dropna(subset<span class="op">=</span>[<span class="st">"t"</span>])</span>
|
||
<span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-8"><a href="#cb32-8" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-9"><a href="#cb32-9" aria-hidden="true" tabindex="-1"></a>df_ma[<span class="st">"metric"</span>] <span class="op">=</span> pd.to_numeric(df_ma[<span class="st">"channels_all"</span>], errors<span class="op">=</span><span class="st">"coerce"</span>) </span>
|
||
<span id="cb32-10"><a href="#cb32-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-11"><a href="#cb32-11" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> win_sum(y):</span>
|
||
<span id="cb32-12"><a href="#cb32-12" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> pd.Timestamp(<span class="ss">f"</span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">-06-01"</span>, tz<span class="op">=</span><span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb32-13"><a href="#cb32-13" aria-hidden="true" tabindex="-1"></a> b <span class="op">=</span> pd.Timestamp(<span class="ss">f"</span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">-09-01"</span>, tz<span class="op">=</span><span class="st">"Europe/Berlin"</span>)</span>
|
||
<span id="cb32-14"><a href="#cb32-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> df_ma.loc[(df_ma[<span class="st">"t"</span>]<span class="op">>=</span>a) <span class="op">&</span> (df_ma[<span class="st">"t"</span>]<span class="op"><</span>b), <span class="st">"metric"</span>].<span class="bu">sum</span>(skipna<span class="op">=</span><span class="va">True</span>)</span>
|
||
<span id="cb32-15"><a href="#cb32-15" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-16"><a href="#cb32-16" aria-hidden="true" tabindex="-1"></a>vals <span class="op">=</span> {y: win_sum(y) <span class="cf">for</span> y <span class="kw">in</span> [<span class="dv">2020</span>, <span class="dv">2021</span>, <span class="dv">2022</span>, <span class="dv">2023</span>]}</span>
|
||
<span id="cb32-17"><a href="#cb32-17" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(vals, <span class="st">"2022 vs 2021 %:"</span>, (vals[<span class="dv">2022</span>]<span class="op">/</span>vals[<span class="dv">2021</span>]<span class="op">-</span><span class="dv">1</span>)<span class="op">*</span><span class="dv">100</span>, <span class="st">"2022 vs 2023 %:"</span>, (vals[<span class="dv">2022</span>]<span class="op">/</span>vals[<span class="dv">2023</span>]<span class="op">-</span><span class="dv">1</span>)<span class="op">*</span><span class="dv">100</span>)</span>
|
||
<span id="cb32-18"><a href="#cb32-18" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb32-19"><a href="#cb32-19" aria-hidden="true" tabindex="-1"></a>fig, ax <span class="op">=</span> plt.subplots()</span>
|
||
<span id="cb32-20"><a href="#cb32-20" aria-hidden="true" tabindex="-1"></a>bars <span class="op">=</span> ax.bar([<span class="st">"2020"</span>, <span class="st">"2021"</span>,<span class="st">"2022 (9€)"</span>,<span class="st">"2023"</span>], [vals[<span class="dv">2020</span>], vals[<span class="dv">2021</span>], vals[<span class="dv">2022</span>], vals[<span class="dv">2023</span>]])</span>
|
||
<span id="cb32-21"><a href="#cb32-21" aria-hidden="true" tabindex="-1"></a>ax.bar_label(bars, padding<span class="op">=</span><span class="dv">3</span>, fmt<span class="op">=</span><span class="st">"%.0f"</span>)</span>
|
||
<span id="cb32-22"><a href="#cb32-22" aria-hidden="true" tabindex="-1"></a>ax.set_title(<span class="st">"Mannheim: Juni–Aug Vergleich 2021/2022/2023"</span>)</span>
|
||
<span id="cb32-23"><a href="#cb32-23" aria-hidden="true" tabindex="-1"></a>ax.set_ylabel(<span class="st">"Summe channels_all"</span>)</span>
|
||
<span id="cb32-24"><a href="#cb32-24" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb32-25"><a href="#cb32-25" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>{2020: np.int64(1688124), 2021: np.int64(1525185), 2022: np.int64(1789267), 2023: np.int64(2262742)} 2022 vs 2021 %: 17.31475198090724 2022 vs 2023 %: -20.924833675248877</code></pre>
|
||
</div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-25-output-2.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="47cc2917" class="cell" data-execution_count="25">
|
||
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Monatliche Beobachtungen im Jahr 2021, 2022, 2023 in Mannheim als Liniendiagramm</span></span>
|
||
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>years <span class="op">=</span> [<span class="dv">2021</span>, <span class="dv">2022</span>, <span class="dv">2023</span>]</span>
|
||
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> [<span class="st">"Jan"</span>,<span class="st">"Feb"</span>,<span class="st">"Mär"</span>,<span class="st">"Apr"</span>,<span class="st">"Mai"</span>,<span class="st">"Jun"</span>,<span class="st">"Jul"</span>,<span class="st">"Aug"</span>,<span class="st">"Sep"</span>,<span class="st">"Okt"</span>,<span class="st">"Nov"</span>,<span class="st">"Dez"</span>]</span>
|
||
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a><span class="co"># --- 1) vorbereiten + filtern ---</span></span>
|
||
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a>d <span class="op">=</span> df_backup.copy()</span>
|
||
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a>d[<span class="st">"t"</span>] <span class="op">=</span> (pd.to_datetime(d[<span class="st">"iso_timestamp"</span>], errors<span class="op">=</span><span class="st">"coerce"</span>, utc<span class="op">=</span><span class="va">True</span>)</span>
|
||
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> .dt.tz_convert(<span class="st">"Europe/Berlin"</span>))</span>
|
||
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a>d <span class="op">=</span> d[</span>
|
||
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> (d[<span class="st">"domain_name"</span>].astype(<span class="bu">str</span>).<span class="bu">str</span>.strip() <span class="op">==</span> <span class="st">"Stadt Mannheim"</span>) <span class="op">&</span></span>
|
||
<span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></a> (d[<span class="st">"counter_site"</span>].astype(<span class="bu">str</span>).<span class="bu">str</span>.strip() <span class="op">==</span> <span class="st">"Kurpfalzbrücke"</span>) <span class="op">&</span></span>
|
||
<span id="cb34-13"><a href="#cb34-13" aria-hidden="true" tabindex="-1"></a> d[<span class="st">"t"</span>].notna()</span>
|
||
<span id="cb34-14"><a href="#cb34-14" aria-hidden="true" tabindex="-1"></a>]</span>
|
||
<span id="cb34-15"><a href="#cb34-15" aria-hidden="true" tabindex="-1"></a>d[<span class="st">"metric"</span>] <span class="op">=</span> pd.to_numeric(d[<span class="st">"channels_all"</span>], errors<span class="op">=</span><span class="st">"coerce"</span>).fillna(<span class="dv">0</span>)</span>
|
||
<span id="cb34-16"><a href="#cb34-16" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-17"><a href="#cb34-17" aria-hidden="true" tabindex="-1"></a><span class="co"># --- 2) Tageswerte -> Monatssummen je Jahr ---</span></span>
|
||
<span id="cb34-18"><a href="#cb34-18" aria-hidden="true" tabindex="-1"></a>daily <span class="op">=</span> d.groupby(d[<span class="st">"t"</span>].dt.floor(<span class="st">"D"</span>))[<span class="st">"metric"</span>].<span class="bu">sum</span>().reset_index(name<span class="op">=</span><span class="st">"metric"</span>)</span>
|
||
<span id="cb34-19"><a href="#cb34-19" aria-hidden="true" tabindex="-1"></a>daily[<span class="st">"year"</span>] <span class="op">=</span> daily[<span class="st">"t"</span>].dt.year</span>
|
||
<span id="cb34-20"><a href="#cb34-20" aria-hidden="true" tabindex="-1"></a>daily[<span class="st">"month"</span>] <span class="op">=</span> daily[<span class="st">"t"</span>].dt.month</span>
|
||
<span id="cb34-21"><a href="#cb34-21" aria-hidden="true" tabindex="-1"></a>daily <span class="op">=</span> daily[daily[<span class="st">"year"</span>].isin(years)]</span>
|
||
<span id="cb34-22"><a href="#cb34-22" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-23"><a href="#cb34-23" aria-hidden="true" tabindex="-1"></a>m <span class="op">=</span> daily.groupby([<span class="st">"year"</span>, <span class="st">"month"</span>])[<span class="st">"metric"</span>].<span class="bu">sum</span>().reset_index(name<span class="op">=</span><span class="st">"month_sum"</span>)</span>
|
||
<span id="cb34-24"><a href="#cb34-24" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-25"><a href="#cb34-25" aria-hidden="true" tabindex="-1"></a><span class="co"># Pivot für Plot (Monate 1..12 als Index, Jahre als Spalten)</span></span>
|
||
<span id="cb34-26"><a href="#cb34-26" aria-hidden="true" tabindex="-1"></a>sum_pivot <span class="op">=</span> (m.pivot(index<span class="op">=</span><span class="st">"month"</span>, columns<span class="op">=</span><span class="st">"year"</span>, values<span class="op">=</span><span class="st">"month_sum"</span>)</span>
|
||
<span id="cb34-27"><a href="#cb34-27" aria-hidden="true" tabindex="-1"></a> .reindex(<span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">13</span>), fill_value<span class="op">=</span><span class="dv">0</span>))</span>
|
||
<span id="cb34-28"><a href="#cb34-28" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-29"><a href="#cb34-29" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-30"><a href="#cb34-30" aria-hidden="true" tabindex="-1"></a>daily <span class="op">=</span> d.groupby(d[<span class="st">"t"</span>].dt.floor(<span class="st">"D"</span>))[<span class="st">"metric"</span>].<span class="bu">sum</span>().reset_index(name<span class="op">=</span><span class="st">"metric"</span>)</span>
|
||
<span id="cb34-31"><a href="#cb34-31" aria-hidden="true" tabindex="-1"></a>daily[<span class="st">"year"</span>] <span class="op">=</span> daily[<span class="st">"t"</span>].dt.year</span>
|
||
<span id="cb34-32"><a href="#cb34-32" aria-hidden="true" tabindex="-1"></a>daily[<span class="st">"month"</span>] <span class="op">=</span> daily[<span class="st">"t"</span>].dt.month</span>
|
||
<span id="cb34-33"><a href="#cb34-33" aria-hidden="true" tabindex="-1"></a>daily <span class="op">=</span> daily[daily[<span class="st">"year"</span>].isin(years)]</span>
|
||
<span id="cb34-34"><a href="#cb34-34" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-35"><a href="#cb34-35" aria-hidden="true" tabindex="-1"></a>daily[<span class="st">"is_holiday"</span>] <span class="op">=</span> <span class="va">False</span></span>
|
||
<span id="cb34-36"><a href="#cb34-36" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-37"><a href="#cb34-37" aria-hidden="true" tabindex="-1"></a>m <span class="op">=</span> (daily.groupby([<span class="st">"year"</span>,<span class="st">"month"</span>])</span>
|
||
<span id="cb34-38"><a href="#cb34-38" aria-hidden="true" tabindex="-1"></a> .agg(month_sum<span class="op">=</span>(<span class="st">"metric"</span>,<span class="st">"sum"</span>),</span>
|
||
<span id="cb34-39"><a href="#cb34-39" aria-hidden="true" tabindex="-1"></a> holiday_share<span class="op">=</span>(<span class="st">"is_holiday"</span>,<span class="st">"mean"</span>)) <span class="co"># Anteil Ferientage im Monat</span></span>
|
||
<span id="cb34-40"><a href="#cb34-40" aria-hidden="true" tabindex="-1"></a> .reset_index())</span>
|
||
<span id="cb34-41"><a href="#cb34-41" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-42"><a href="#cb34-42" aria-hidden="true" tabindex="-1"></a>sum_pivot <span class="op">=</span> (m.pivot(index<span class="op">=</span><span class="st">"month"</span>, columns<span class="op">=</span><span class="st">"year"</span>, values<span class="op">=</span><span class="st">"month_sum"</span>)</span>
|
||
<span id="cb34-43"><a href="#cb34-43" aria-hidden="true" tabindex="-1"></a> .reindex(<span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">13</span>), fill_value<span class="op">=</span><span class="dv">0</span>))</span>
|
||
<span id="cb34-44"><a href="#cb34-44" aria-hidden="true" tabindex="-1"></a>hol_pivot <span class="op">=</span> (m.pivot(index<span class="op">=</span><span class="st">"month"</span>, columns<span class="op">=</span><span class="st">"year"</span>, values<span class="op">=</span><span class="st">"holiday_share"</span>)</span>
|
||
<span id="cb34-45"><a href="#cb34-45" aria-hidden="true" tabindex="-1"></a> .reindex(<span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">13</span>), fill_value<span class="op">=</span><span class="dv">0</span>))</span>
|
||
<span id="cb34-46"><a href="#cb34-46" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-47"><a href="#cb34-47" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-48"><a href="#cb34-48" aria-hidden="true" tabindex="-1"></a>x <span class="op">=</span> np.arange(<span class="dv">1</span>, <span class="dv">13</span>)</span>
|
||
<span id="cb34-49"><a href="#cb34-49" aria-hidden="true" tabindex="-1"></a>fig, ax <span class="op">=</span> plt.subplots()</span>
|
||
<span id="cb34-50"><a href="#cb34-50" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-51"><a href="#cb34-51" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> y <span class="kw">in</span> years:</span>
|
||
<span id="cb34-52"><a href="#cb34-52" aria-hidden="true" tabindex="-1"></a> ax.plot(x, sum_pivot[y].to_numpy(), label<span class="op">=</span><span class="bu">str</span>(y))</span>
|
||
<span id="cb34-53"><a href="#cb34-53" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-54"><a href="#cb34-54" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-55"><a href="#cb34-55" aria-hidden="true" tabindex="-1"></a>ax.set_xticks(x, labels)</span>
|
||
<span id="cb34-56"><a href="#cb34-56" aria-hidden="true" tabindex="-1"></a>ax.set_xlabel(<span class="st">"Monat"</span>)</span>
|
||
<span id="cb34-57"><a href="#cb34-57" aria-hidden="true" tabindex="-1"></a>ax.set_ylabel(<span class="st">"Summe channels_all"</span>)</span>
|
||
<span id="cb34-58"><a href="#cb34-58" aria-hidden="true" tabindex="-1"></a>ax.set_title(<span class="st">"Mannheim: Monatssumme + Ferienanteil"</span>)</span>
|
||
<span id="cb34-59"><a href="#cb34-59" aria-hidden="true" tabindex="-1"></a>ax.legend()</span>
|
||
<span id="cb34-60"><a href="#cb34-60" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
|
||
<span id="cb34-61"><a href="#cb34-61" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
|
||
<span id="cb34-62"><a href="#cb34-62" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-63"><a href="#cb34-63" aria-hidden="true" tabindex="-1"></a><span class="co"># Prognose berechnen für Wachstum im Jahr 2022 mit bezug ob das 9$ Ticket gedämpft hat</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display">
|
||
<div>
|
||
<figure class="figure">
|
||
<p><img src="bikes_files/figure-html/cell-26-output-1.png" class="img-fluid figure-img"></p>
|
||
</figure>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div id="6d892782" class="cell" data-execution_count="104">
|
||
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>grouped_df <span class="op">=</span> df.groupby(<span class="st">"domain_name"</span>)</span>
|
||
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Städte mit Messstationen ab 2014:</span><span class="ch">\n</span><span class="st"> "</span>)</span>
|
||
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> name, group <span class="kw">in</span> grouped_df:</span>
|
||
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a> first_year <span class="op">=</span> <span class="bu">min</span>(group[<span class="st">"year"</span>])</span>
|
||
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> first_year <span class="op"><=</span> <span class="dv">2014</span>:</span>
|
||
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(group[<span class="st">"domain_name"</span>].iloc[<span class="dv">0</span>])</span>
|
||
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a> means_2014 <span class="op">=</span> group.groupby(<span class="st">"counter_site"</span>)[<span class="st">"channels_all"</span>].mean()</span>
|
||
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a> biggest_station_in_city <span class="op">=</span> means_2014.idxmax()</span>
|
||
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(biggest_station_in_city)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>Städte mit Messstationen ab 2014:
|
||
|
||
Landeshauptstadt Stuttgart
|
||
König-Karls-Brücke Barometer
|
||
Stadt Freiburg
|
||
Wiwilibrücke
|
||
Stadt Heidelberg
|
||
Thedor-Heuss-Brücke Querschnitt
|
||
Stadt Heilbronn
|
||
Neckarufer
|
||
Stadt Karlsruhe
|
||
Erbprinzenstraße
|
||
Stadt Kirchheim Unter Teck
|
||
Barometer Kirchheim u. Teck
|
||
Stadt Lörrach
|
||
Untere Hartmattenstraße / Hauptfriedhof
|
||
Stadt Mannheim
|
||
Kurpfalzbrücke
|
||
Stadt Tübingen
|
||
Unterführung Steinlach/Karlstraße Südseite - Steinlachallee</code></pre>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
|
||
</main>
|
||
<!-- /main column -->
|
||
<script id="quarto-html-after-body" type="application/javascript">
|
||
window.document.addEventListener("DOMContentLoaded", function (event) {
|
||
const toggleBodyColorMode = (bsSheetEl) => {
|
||
const mode = bsSheetEl.getAttribute("data-mode");
|
||
const bodyEl = window.document.querySelector("body");
|
||
if (mode === "dark") {
|
||
bodyEl.classList.add("quarto-dark");
|
||
bodyEl.classList.remove("quarto-light");
|
||
} else {
|
||
bodyEl.classList.add("quarto-light");
|
||
bodyEl.classList.remove("quarto-dark");
|
||
}
|
||
}
|
||
const toggleBodyColorPrimary = () => {
|
||
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
|
||
if (bsSheetEl) {
|
||
toggleBodyColorMode(bsSheetEl);
|
||
}
|
||
}
|
||
toggleBodyColorPrimary();
|
||
const icon = "";
|
||
const anchorJS = new window.AnchorJS();
|
||
anchorJS.options = {
|
||
placement: 'right',
|
||
icon: icon
|
||
};
|
||
anchorJS.add('.anchored');
|
||
const isCodeAnnotation = (el) => {
|
||
for (const clz of el.classList) {
|
||
if (clz.startsWith('code-annotation-')) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
const onCopySuccess = function(e) {
|
||
// button target
|
||
const button = e.trigger;
|
||
// don't keep focus
|
||
button.blur();
|
||
// flash "checked"
|
||
button.classList.add('code-copy-button-checked');
|
||
var currentTitle = button.getAttribute("title");
|
||
button.setAttribute("title", "Copied!");
|
||
let tooltip;
|
||
if (window.bootstrap) {
|
||
button.setAttribute("data-bs-toggle", "tooltip");
|
||
button.setAttribute("data-bs-placement", "left");
|
||
button.setAttribute("data-bs-title", "Copied!");
|
||
tooltip = new bootstrap.Tooltip(button,
|
||
{ trigger: "manual",
|
||
customClass: "code-copy-button-tooltip",
|
||
offset: [0, -8]});
|
||
tooltip.show();
|
||
}
|
||
setTimeout(function() {
|
||
if (tooltip) {
|
||
tooltip.hide();
|
||
button.removeAttribute("data-bs-title");
|
||
button.removeAttribute("data-bs-toggle");
|
||
button.removeAttribute("data-bs-placement");
|
||
}
|
||
button.setAttribute("title", currentTitle);
|
||
button.classList.remove('code-copy-button-checked');
|
||
}, 1000);
|
||
// clear code selection
|
||
e.clearSelection();
|
||
}
|
||
const getTextToCopy = function(trigger) {
|
||
const codeEl = trigger.previousElementSibling.cloneNode(true);
|
||
for (const childEl of codeEl.children) {
|
||
if (isCodeAnnotation(childEl)) {
|
||
childEl.remove();
|
||
}
|
||
}
|
||
return codeEl.innerText;
|
||
}
|
||
const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
|
||
text: getTextToCopy
|
||
});
|
||
clipboard.on('success', onCopySuccess);
|
||
if (window.document.getElementById('quarto-embedded-source-code-modal')) {
|
||
const clipboardModal = new window.ClipboardJS('.code-copy-button[data-in-quarto-modal]', {
|
||
text: getTextToCopy,
|
||
container: window.document.getElementById('quarto-embedded-source-code-modal')
|
||
});
|
||
clipboardModal.on('success', onCopySuccess);
|
||
}
|
||
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
|
||
var mailtoRegex = new RegExp(/^mailto:/);
|
||
var filterRegex = new RegExp('/' + window.location.host + '/');
|
||
var isInternal = (href) => {
|
||
return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
|
||
}
|
||
// Inspect non-navigation links and adorn them if external
|
||
var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool):not(.about-link)');
|
||
for (var i=0; i<links.length; i++) {
|
||
const link = links[i];
|
||
if (!isInternal(link.href)) {
|
||
// undo the damage that might have been done by quarto-nav.js in the case of
|
||
// links that we want to consider external
|
||
if (link.dataset.originalHref !== undefined) {
|
||
link.href = link.dataset.originalHref;
|
||
}
|
||
}
|
||
}
|
||
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
|
||
const config = {
|
||
allowHTML: true,
|
||
maxWidth: 500,
|
||
delay: 100,
|
||
arrow: false,
|
||
appendTo: function(el) {
|
||
return el.parentElement;
|
||
},
|
||
interactive: true,
|
||
interactiveBorder: 10,
|
||
theme: 'quarto',
|
||
placement: 'bottom-start',
|
||
};
|
||
if (contentFn) {
|
||
config.content = contentFn;
|
||
}
|
||
if (onTriggerFn) {
|
||
config.onTrigger = onTriggerFn;
|
||
}
|
||
if (onUntriggerFn) {
|
||
config.onUntrigger = onUntriggerFn;
|
||
}
|
||
window.tippy(el, config);
|
||
}
|
||
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
|
||
for (var i=0; i<noterefs.length; i++) {
|
||
const ref = noterefs[i];
|
||
tippyHover(ref, function() {
|
||
// use id or data attribute instead here
|
||
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
|
||
try { href = new URL(href).hash; } catch {}
|
||
const id = href.replace(/^#\/?/, "");
|
||
const note = window.document.getElementById(id);
|
||
if (note) {
|
||
return note.innerHTML;
|
||
} else {
|
||
return "";
|
||
}
|
||
});
|
||
}
|
||
const xrefs = window.document.querySelectorAll('a.quarto-xref');
|
||
const processXRef = (id, note) => {
|
||
// Strip column container classes
|
||
const stripColumnClz = (el) => {
|
||
el.classList.remove("page-full", "page-columns");
|
||
if (el.children) {
|
||
for (const child of el.children) {
|
||
stripColumnClz(child);
|
||
}
|
||
}
|
||
}
|
||
stripColumnClz(note)
|
||
if (id === null || id.startsWith('sec-')) {
|
||
// Special case sections, only their first couple elements
|
||
const container = document.createElement("div");
|
||
if (note.children && note.children.length > 2) {
|
||
container.appendChild(note.children[0].cloneNode(true));
|
||
for (let i = 1; i < note.children.length; i++) {
|
||
const child = note.children[i];
|
||
if (child.tagName === "P" && child.innerText === "") {
|
||
continue;
|
||
} else {
|
||
container.appendChild(child.cloneNode(true));
|
||
break;
|
||
}
|
||
}
|
||
if (window.Quarto?.typesetMath) {
|
||
window.Quarto.typesetMath(container);
|
||
}
|
||
return container.innerHTML
|
||
} else {
|
||
if (window.Quarto?.typesetMath) {
|
||
window.Quarto.typesetMath(note);
|
||
}
|
||
return note.innerHTML;
|
||
}
|
||
} else {
|
||
// Remove any anchor links if they are present
|
||
const anchorLink = note.querySelector('a.anchorjs-link');
|
||
if (anchorLink) {
|
||
anchorLink.remove();
|
||
}
|
||
if (window.Quarto?.typesetMath) {
|
||
window.Quarto.typesetMath(note);
|
||
}
|
||
if (note.classList.contains("callout")) {
|
||
return note.outerHTML;
|
||
} else {
|
||
return note.innerHTML;
|
||
}
|
||
}
|
||
}
|
||
for (var i=0; i<xrefs.length; i++) {
|
||
const xref = xrefs[i];
|
||
tippyHover(xref, undefined, function(instance) {
|
||
instance.disable();
|
||
let url = xref.getAttribute('href');
|
||
let hash = undefined;
|
||
if (url.startsWith('#')) {
|
||
hash = url;
|
||
} else {
|
||
try { hash = new URL(url).hash; } catch {}
|
||
}
|
||
if (hash) {
|
||
const id = hash.replace(/^#\/?/, "");
|
||
const note = window.document.getElementById(id);
|
||
if (note !== null) {
|
||
try {
|
||
const html = processXRef(id, note.cloneNode(true));
|
||
instance.setContent(html);
|
||
} finally {
|
||
instance.enable();
|
||
instance.show();
|
||
}
|
||
} else {
|
||
// See if we can fetch this
|
||
fetch(url.split('#')[0])
|
||
.then(res => res.text())
|
||
.then(html => {
|
||
const parser = new DOMParser();
|
||
const htmlDoc = parser.parseFromString(html, "text/html");
|
||
const note = htmlDoc.getElementById(id);
|
||
if (note !== null) {
|
||
const html = processXRef(id, note);
|
||
instance.setContent(html);
|
||
}
|
||
}).finally(() => {
|
||
instance.enable();
|
||
instance.show();
|
||
});
|
||
}
|
||
} else {
|
||
// See if we can fetch a full url (with no hash to target)
|
||
// This is a special case and we should probably do some content thinning / targeting
|
||
fetch(url)
|
||
.then(res => res.text())
|
||
.then(html => {
|
||
const parser = new DOMParser();
|
||
const htmlDoc = parser.parseFromString(html, "text/html");
|
||
const note = htmlDoc.querySelector('main.content');
|
||
if (note !== null) {
|
||
// This should only happen for chapter cross references
|
||
// (since there is no id in the URL)
|
||
// remove the first header
|
||
if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
|
||
note.children[0].remove();
|
||
}
|
||
const html = processXRef(null, note);
|
||
instance.setContent(html);
|
||
}
|
||
}).finally(() => {
|
||
instance.enable();
|
||
instance.show();
|
||
});
|
||
}
|
||
}, function(instance) {
|
||
});
|
||
}
|
||
let selectedAnnoteEl;
|
||
const selectorForAnnotation = ( cell, annotation) => {
|
||
let cellAttr = 'data-code-cell="' + cell + '"';
|
||
let lineAttr = 'data-code-annotation="' + annotation + '"';
|
||
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
|
||
return selector;
|
||
}
|
||
const selectCodeLines = (annoteEl) => {
|
||
const doc = window.document;
|
||
const targetCell = annoteEl.getAttribute("data-target-cell");
|
||
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
|
||
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
|
||
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
|
||
const lineIds = lines.map((line) => {
|
||
return targetCell + "-" + line;
|
||
})
|
||
let top = null;
|
||
let height = null;
|
||
let parent = null;
|
||
if (lineIds.length > 0) {
|
||
//compute the position of the single el (top and bottom and make a div)
|
||
const el = window.document.getElementById(lineIds[0]);
|
||
top = el.offsetTop;
|
||
height = el.offsetHeight;
|
||
parent = el.parentElement.parentElement;
|
||
if (lineIds.length > 1) {
|
||
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
|
||
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
|
||
height = bottom - top;
|
||
}
|
||
if (top !== null && height !== null && parent !== null) {
|
||
// cook up a div (if necessary) and position it
|
||
let div = window.document.getElementById("code-annotation-line-highlight");
|
||
if (div === null) {
|
||
div = window.document.createElement("div");
|
||
div.setAttribute("id", "code-annotation-line-highlight");
|
||
div.style.position = 'absolute';
|
||
parent.appendChild(div);
|
||
}
|
||
div.style.top = top - 2 + "px";
|
||
div.style.height = height + 4 + "px";
|
||
div.style.left = 0;
|
||
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
|
||
if (gutterDiv === null) {
|
||
gutterDiv = window.document.createElement("div");
|
||
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
|
||
gutterDiv.style.position = 'absolute';
|
||
const codeCell = window.document.getElementById(targetCell);
|
||
const gutter = codeCell.querySelector('.code-annotation-gutter');
|
||
gutter.appendChild(gutterDiv);
|
||
}
|
||
gutterDiv.style.top = top - 2 + "px";
|
||
gutterDiv.style.height = height + 4 + "px";
|
||
}
|
||
selectedAnnoteEl = annoteEl;
|
||
}
|
||
};
|
||
const unselectCodeLines = () => {
|
||
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
|
||
elementsIds.forEach((elId) => {
|
||
const div = window.document.getElementById(elId);
|
||
if (div) {
|
||
div.remove();
|
||
}
|
||
});
|
||
selectedAnnoteEl = undefined;
|
||
};
|
||
// Handle positioning of the toggle
|
||
window.addEventListener(
|
||
"resize",
|
||
throttle(() => {
|
||
elRect = undefined;
|
||
if (selectedAnnoteEl) {
|
||
selectCodeLines(selectedAnnoteEl);
|
||
}
|
||
}, 10)
|
||
);
|
||
function throttle(fn, ms) {
|
||
let throttle = false;
|
||
let timer;
|
||
return (...args) => {
|
||
if(!throttle) { // first call gets through
|
||
fn.apply(this, args);
|
||
throttle = true;
|
||
} else { // all the others get throttled
|
||
if(timer) clearTimeout(timer); // cancel #2
|
||
timer = setTimeout(() => {
|
||
fn.apply(this, args);
|
||
timer = throttle = false;
|
||
}, ms);
|
||
}
|
||
};
|
||
}
|
||
// Attach click handler to the DT
|
||
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
|
||
for (const annoteDlNode of annoteDls) {
|
||
annoteDlNode.addEventListener('click', (event) => {
|
||
const clickedEl = event.target;
|
||
if (clickedEl !== selectedAnnoteEl) {
|
||
unselectCodeLines();
|
||
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
|
||
if (activeEl) {
|
||
activeEl.classList.remove('code-annotation-active');
|
||
}
|
||
selectCodeLines(clickedEl);
|
||
clickedEl.classList.add('code-annotation-active');
|
||
} else {
|
||
// Unselect the line
|
||
unselectCodeLines();
|
||
clickedEl.classList.remove('code-annotation-active');
|
||
}
|
||
});
|
||
}
|
||
const findCites = (el) => {
|
||
const parentEl = el.parentElement;
|
||
if (parentEl) {
|
||
const cites = parentEl.dataset.cites;
|
||
if (cites) {
|
||
return {
|
||
el,
|
||
cites: cites.split(' ')
|
||
};
|
||
} else {
|
||
return findCites(el.parentElement)
|
||
}
|
||
} else {
|
||
return undefined;
|
||
}
|
||
};
|
||
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
|
||
for (var i=0; i<bibliorefs.length; i++) {
|
||
const ref = bibliorefs[i];
|
||
const citeInfo = findCites(ref);
|
||
if (citeInfo) {
|
||
tippyHover(citeInfo.el, function() {
|
||
var popup = window.document.createElement('div');
|
||
citeInfo.cites.forEach(function(cite) {
|
||
var citeDiv = window.document.createElement('div');
|
||
citeDiv.classList.add('hanging-indent');
|
||
citeDiv.classList.add('csl-entry');
|
||
var biblioDiv = window.document.getElementById('ref-' + cite);
|
||
if (biblioDiv) {
|
||
citeDiv.innerHTML = biblioDiv.innerHTML;
|
||
}
|
||
popup.appendChild(citeDiv);
|
||
});
|
||
return popup.innerHTML;
|
||
});
|
||
}
|
||
}
|
||
});
|
||
</script>
|
||
</div> <!-- /content -->
|
||
|
||
|
||
|
||
|
||
</body></html> |