def top_entry_bar_plot(self, save=False): """ Bar chart of turnstile entrance data for top stations. :param bool save: if True the figure will be saved """ fig = plt.figure('Top Station Box Plot', figsize=(8, 5), facecolor='white', edgecolor='black') rows, cols = (1, 1) ax0 = plt.subplot2grid((rows, cols), (0, 0)) (self.top_stations.sort_values(by='entry').loc[:, 'Monday':'Sunday'].plot( kind='bar', alpha=0.5, edgecolor='black', ax=ax0)) ax0.set_title(f'{self.date_start} - {self.date_end}', fontsize=size['title']) ax0.set_xlabel('Subway Station', fontsize=size['label']) ax0.set_xticklabels(ax0.xaxis.get_majorticklabels(), rotation=80) ax0.set_ylabel('Turnstile Entries ($billions$)', fontsize=size['label']) ax0.yaxis.set_major_formatter(ax_formatter['billions']) plt.tight_layout() plt.suptitle('Turnstile Data', fontsize=size['super_title'], x=0.54, y=1.05) save_fig('age_voted', save)
def train_plot(self, save=False): """ Plot subway stations by train. :param bool save: if True the figure will be saved """ sns.lmplot(x='longitude', y='latitude', data=(self.trains.sort_values(by='train')), hue='train', fit_reg=False, legend=False, markers='d', scatter_kws={'alpha': 0.3}, size=10) legend = plt.legend(bbox_to_anchor=(1.10, 0.5), fancybox=True, fontsize=size['legend'], loc='center right', shadow=True, title='Train') plt.setp(legend.get_title(), fontsize=size['label']) plt.xlabel('Longitude', fontsize=size['label']) plt.ylabel('Latitude', fontsize=size['label']) plt.tight_layout() plt.suptitle('New York Subway Train Stations', fontsize=size['super_title'], y=1.03) save_fig('stations_trains', save)
def targets_entry_plot(self, save=False): """ Bar chart of turnstile entrance data. :param bool save: if True the figure will be saved """ fig = plt.figure('Station Box Plot', figsize=(8, 10), facecolor='white', edgecolor='black') rows, cols = (2, 1) ax0 = plt.subplot2grid((rows, cols), (0, 0)) ax1 = plt.subplot2grid((rows, cols), (1, 0)) # Pie Plot pie = (self.target_data.groupby('station')['entry'].sum().sort_values( ascending=False)) (pie.plot(kind='pie', colors=sns.color_palette('gnuplot', n_colors=10), explode=[0.05] * pie.shape[0], labels=None, legend=None, shadow=True, startangle=180, ax=ax0)) ax0.set_aspect('equal') ax0.set_ylabel('') ax0.legend(bbox_to_anchor=(1.3, 0.5), labels=pie.index, loc='center') # Bar Plot (self.target_data.groupby('station')[[ 'entry', 'exit' ]].sum().sort_values(by='entry').plot(kind='bar', alpha=0.5, edgecolor='black', ax=ax1)) ax1.set_title(f'{self.date_start} - {self.date_end}', fontsize=size['title']) ax1.legend(['Entry', 'Exit']) ax1.set_xlabel('Subway Station', fontsize=size['label']) ax1.set_xticklabels(ax0.xaxis.get_majorticklabels(), rotation=80) ax1.set_ylabel('Turnstile Use ($billions$)', fontsize=size['label']) ax1.yaxis.set_major_formatter(ax_formatter['billions']) plt.tight_layout() plt.suptitle('Turnstile Data', fontsize=size['super_title'], x=0.53, y=1.05) save_fig('age_voted', save)
def daily_use_plot(self, station_name, save=False): """ Plot average daily turnstile use. :param str station_name: station name :param bool save: if True the figure will be saved """ if self.target_data is not station_name: self.average_daily_use(station_name) fig = plt.figure('Station By Day', figsize=(12, 12), facecolor='white', edgecolor='black') rows, cols = (3, 1) ax0 = plt.subplot2grid((rows, cols), (0, 0)) ax1 = plt.subplot2grid((rows, cols), (1, 0), sharex=ax0, sharey=ax0) ax2 = plt.subplot2grid((rows, cols), (2, 0), sharex=ax0, sharey=ax0) bar_plot = (self.daily_use.loc[( slice(0, 7), slice( pd.to_datetime('09:00:00').time(), pd.to_datetime('17:00:00').time())), :].reset_index().rename( columns={ 'date_time': 'weekday', 'level_1': 'time' })) bar_plot['day_name'] = ( bar_plot.weekday.map(lambda x: f'{self.days[x]}')) bar_plot['entry_legend'] = ( bar_plot.weekday.map(lambda x: f'{self.days[x]} Entry')) bar_plot['exit_legend'] = ( bar_plot.weekday.map(lambda x: f'{self.days[x]} Exit')) # entry data sns.barplot(x='time', y='entry', hue='day_name', data=bar_plot, alpha=0.7, edgecolor='black', palette='gnuplot', ax=ax0) ax0.set_title('Turnstile Entrances', fontsize=size['title']) # exit data sns.barplot(x='time', y='exit', hue='day_name', data=bar_plot, alpha=0.5, edgecolor='black', ax=ax1) ax1.set_title('Turnstile Exits', fontsize=size['title']) # combined plot sns.barplot(x='time', y='entry', hue='entry_legend', data=bar_plot, alpha=0.7, edgecolor='black', palette='gnuplot', ax=ax2) sns.barplot(x='time', y='exit', hue='exit_legend', data=bar_plot, alpha=0.5, edgecolor='black', ax=ax2) ax2.set_title('Turnstile Entrance and Exits', fontsize=size['title']) for ax in (ax0, ax1, ax2): ax.legend(bbox_to_anchor=(1.2, 1), fontsize=size['legend'], loc='upper right') ax.set_xlabel('Time of Day', fontsize=size['label']) ax.set_ylabel('Turnstile Use ($millions$)', fontsize=size['label']) ax.yaxis.set_major_formatter(ax_formatter['millions']) plt.tight_layout() plt.suptitle( f'Station Average Turnstile Use: ' f'{station_name.title()}\n' f'{self.date_start} - {self.date_end}\n', fontsize=size['super_title'], x=0.52, y=1.07) save_fig('daily_use_plot', save)
def age_vote_plot(self, save=False): """ US Voter Age plots :param bool save: if True the figure will be saved """ fig = plt.figure('Voter Age', figsize=(12, 10), facecolor='white', edgecolor='black') rows, cols = (2, 2) ax0 = plt.subplot2grid((rows, cols), (0, 0)) ax1 = plt.subplot2grid((rows, cols), (0, 1), sharex=ax0, sharey=ax0) ax2 = plt.subplot2grid((rows, cols), (1, 0), sharex=ax0) ax3 = plt.subplot2grid((rows, cols), (1, 1), sharex=ax0, sharey=ax2) both_sexes = self.data.query('sex == "both sexes"') female = self.data.query('sex == "female"') male = self.data.query('sex == "male"') # Voters All US Plot (both_sexes.voted_yes.plot(kind='area', alpha=0.5, label='Voters', ax=ax0)) target_group = both_sexes.voted_yes.copy() target_group.loc[target_group < target_group.quantile(0.7)] = 0 (target_group.plot(kind='area', alpha=0.2, color='C2', label='Target Group', ax=ax0)) ax0.set_title('Voters vs Age', fontsize=size['title']) ax0.legend(loc='upper left', fontsize=size['legend']) ax0.set_ylabel('People ($thousands$)', fontsize=size['label']) ax0.yaxis.set_major_formatter(ax_formatter['thousands']) # Voters Gender Plot for n, gender in enumerate((female, male)): (gender.voted_yes.plot(kind='area', alpha=0.5, color=f'C{n}', ax=ax1)) ax1.set_title('Voters vs Age by Gender', fontsize=size['title']) # Voters All US Percentage Plot (both_sexes.voted_yes.div(both_sexes.us_population).plot( kind='area', alpha=0.5, label='Voter Percentage', ax=ax2)) target_group = both_sexes.voted_yes_pct.copy() target_group.loc[target_group < 0.5] = 0 (target_group.plot(kind='area', alpha=0.2, color='C2', label='Target Group', ax=ax2)) ax2.set_title('Percent Voters vs Age', fontsize=size['title']) ax2.legend(loc='upper left', fontsize=size['legend']) ax2.set_ylabel('Voting Percentage of Age', fontsize=size['label']) ax2.yaxis.set_major_formatter(ax_formatter['percent_convert']) # Voters Gender Percentage Plot for n, gender in enumerate((female, male)): (gender.voted_yes.div(gender.us_population).plot(kind='area', alpha=0.5, color=f'C{n}', ax=ax3)) ax3.set_title('Percent Voters vs Age by Gender', fontsize=size['title']) for ax in (ax0, ax1): ax.set_xlabel('') for ax in (ax2, ax3): ax.set_xlabel('Age ($years$)', fontsize=size['label']) for ax in (ax1, ax3): ax.legend(['Female', 'Male'], fontsize=size['legend']) plt.tight_layout() plt.suptitle('2016 US Voter Age Distributions', fontsize=size['super_title'], y=1.03) save_fig('age_voted', save)
def ethnicity_plot(self, save=False): """ New York state ethnicity voter plots. :param bool save: if True the figure will be saved """ fig = plt.figure('Age Vote', figsize=(10, 10), facecolor='white', edgecolor='black') rows, cols = (2, 1) ax0 = plt.subplot2grid((rows, cols), (0, 0)) ax1 = plt.subplot2grid((rows, cols), (1, 0)) # Voter Turnout (self.data.loc[ 'new york', ['race', 'state_population', 'registered_yes', 'voted_yes']]. set_index('race').sort_values(by='voted_yes').plot(kind='bar', alpha=0.5, edgecolor='black', ax=ax0)) ax0.set_title('2016 New York Voters', fontsize=size['title']) ax0.legend(['Total Population', 'Registered Voters', 'Voted in 2016'], fontsize=size['legend']) ax0.set_ylabel('People ($thousands$)', fontsize=size['label']) ax0.yaxis.set_major_formatter(ax_formatter['thousands']) # Voter Turnout Percentage voter_pct = (self.data.loc['new york', [ 'race', 'registered_total_pct', 'registered_total_error', 'voted_total_pct', 'voted_total_error' ]].set_index('race').applymap(lambda x: float(x)).sort_values( by='voted_total_pct')) data_cols = ['registered_total_pct', 'voted_total_pct'] voter_err = voter_pct.drop(data_cols, axis=1) voter_err.columns = data_cols error_kw = {'capsize': 3, 'capthick': 1, 'elinewidth': 1} voter_pct.plot(kind='bar', y=data_cols, alpha=0.5, edgecolor='black', error_kw=error_kw, yerr=voter_err, ax=ax1) ax1.set_title('2016 New York Voter Percentages', fontsize=size['title']) ax1.legend(['Registered Voters', 'Voted in 2016'], fontsize=size['legend']) ax1.set_ylabel('Percentage', fontsize=size['label']) ax1.yaxis.set_major_formatter(ax_formatter['percent']) for ax in (ax0, ax1): ax.set_xlabel('Group', fontsize=size['label']) ax.set_xticklabels(ax.xaxis.get_majorticklabels(), fontsize=12, rotation=75) plt.tight_layout() plt.suptitle('2016 New York State Voter Distributions', fontsize=size['super_title'], y=1.03) save_fig('age_voted', save)