-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_trips_vs_temp.py
76 lines (61 loc) · 2.33 KB
/
plot_trips_vs_temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Plot average trips on weekdays between 08:00-09:00 versus the feels-like
temperature.
"""
import sys
import os
import argparse
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Args ------------------------------------------------------------------------
parser = argparse.ArgumentParser(description="Plotting script.")
parser.add_argument('--pdf', default=False, action="store_true",
help="Output PDF file.")
parser.add_argument('--png', default=False, action="store_true",
help="Output PNG file.")
parser.add_argument("--show", default=False, action="store_true",
help="Show plot in GUI.")
parser.add_argument("--tex", default=False, action="store_true",
help="Render with TeX.")
parser.add_argument("--start", default='2015-01-04', help="Start date.",
type=str)
parser.add_argument("--end", default='2017-01-03', help="End date.",
type=str)
opts, unknown = parser.parse_known_args()
print('opts:', opts)
# Data ------------------------------------------------------------------------
FPATH = 'data/raw/london_merged.csv'
START_DATE = opts.start
END_DATE = opts.end
df_raw = pd.read_csv(filepath_or_buffer=FPATH, index_col=0, parse_dates=[0],
dtype={'weather_code': np.int, 'is_holiday': np.bool, 'is_weekend': bool,
'season': int})
df = df_raw.loc[START_DATE:END_DATE]
df['dayofweek'] = df.index.dayofweek
df['hour'] = df.index.hour
df['dry'] = df['weather_code'].isin([1,2,3,4])
df = df[df['hour'] == 8] # Only trips between 8AM and 9AM
df = df[df['is_weekend'] == False] # On weekdays
df = df[df['is_holiday'] == False]
df = df[df['dry'] == True]
# Plot ------------------------------------------------------------------------
sns.set_style('darkgrid')
pal = sns.color_palette('deep')
if opts.tex:
import matplotlib
matplotlib.rcParams['text.usetex'] = True
jp = sns.jointplot(x='t1', y='cnt', data=df, kind='reg',
joint_kws={'scatter_kws':dict(alpha=0.2)}, height=8)
jp.ax_joint.set_xlabel('Temperature (Feels Like)')
jp.ax_joint.set_ylabel('Trips per Hour')
jp.ax_joint.set_ylim([0, 8000])
plt.tight_layout()
# Output ----------------------------------------------------------------------
path_no_ext = os.path.splitext(sys.argv[0])[0]
if opts.pdf:
plt.savefig(path_no_ext + '.pdf')
if opts.png:
plt.savefig(path_no_ext + '.png')
if opts.show:
plt.show()