Skip to content

Instantly share code, notes, and snippets.

@Chandler
Created December 5, 2021 05:20
Show Gist options
  • Save Chandler/57731f999736b6f09b18f406f4fae07f to your computer and use it in GitHub Desktop.
Save Chandler/57731f999736b6f09b18f406f4fae07f to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import csv
import datetime
import pandas as pd
from matplotlib.colors import ListedColormap
# from lyft data download
ebikes_path = "lyft_bike_scooters.csv"
cars_path = "passenger_rides.csv"
# from strava data download
strava_path = "activities.csv"
def strava_date_conversion(date):
return datetime.datetime.strptime(date,'%b %d, %Y, %I:%M:%S %p').strftime("%Y-%m-%d %H:%M:%S UTC")
with open(cars_path) as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
next(reader) #skip first line
car_dates = [(row[1],'A: Lyft Car') for row in reader]
with open(ebikes_path) as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
next(reader) #skip first line
ebike_dates = [(row[3],'C: Lyft eBike') for row in reader]
with open(strava_path) as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
next(reader) #skip first line
strava_dates = [(strava_date_conversion(row[1]),'B: My Bike (Strava)') for row in reader]
df = pd.DataFrame(ebike_dates + car_dates + strava_dates, columns=['date', 'mode'])
df.date = pd.to_datetime(df.date)
pd.crosstab(df.date.dt.to_period('Y'),df["mode"]).plot.bar(
colormap=ListedColormap(['#8FBFE0', '#7C77B9', '#1D8A99']),
stacked=True,
xlabel="Year",
ylabel='Rides'
)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment