import pandas as pd import os %matplotlib inline import matplotlib.pyplot as plt import numpy as np cur_dir = os.path.dirname(os.path.realpath('__file__')) df = pd.read_csv(cur_dir + '/data/cross_raw_data/' + 'bc_full_crossing_data.csv') df.head() df['Port Name'][1] df_douglas = df[df['Port Name'] == 'AZ: Douglas '][:] df_douglas.head() df_douglas['Total Crossers'] = df_douglas['Train Passengers'] + df_douglas['Bus Passengers'] + df_douglas['Personal Vehicle Passengers'] + df_douglas['Pedestrians'] df_douglas_annual_sum = df_douglas.groupby(df_douglas['Year']).sum() df_douglas_annual_sum.head() df_douglas_annual_sum['Bus Passengers'].plot(kind='area', grid=False) df_douglas_annual_sum['Personal Vehicle Passengers'].plot(kind='area', grid=False) df_douglas_annual_sum['Pedestrians'].plot(kind='area', grid=False) # Create a figure with a single subplot f, ax = plt.subplots(1, figsize=(10,5)) # Set bar width at 1 bar_width = 1 # positions of the left bar-boundaries bar_l = [i for i in range(len(df_douglas_annual_sum['Bus Passengers']))] # positions of the x-axis ticks (center of the bars as bar labels) tick_pos = [i+(bar_width/2) for i in bar_l] # Create the total score for each participant totals = [i+j+k for i,j,k in zip(df_douglas_annual_sum['Bus Passengers'], df_douglas_annual_sum['Personal Vehicle Passengers'], df_douglas_annual_sum['Pedestrians'])] # Create the percentage of the total score the pre_score value for each participant was pre_rel = [i / j * 100 for i,j in zip(df_douglas_annual_sum['Bus Passengers'], totals)] # Create the percentage of the total score the mid_score value for each participant was mid_rel = [i / j * 100 for i,j in zip(df_douglas_annual_sum['Personal Vehicle Passengers'], totals)] # Create the percentage of the total score the post_score value for each participant was post_rel = [i / j * 100 for i,j in zip(df_douglas_annual_sum['Pedestrians'], totals)] # Create a bar chart in position bar_1 ax.bar(bar_l, # using pre_rel data pre_rel, # labeled label='Bus Passengers', # with alpha alpha=0.9, # with color color='#019600', # with bar width width=bar_width, # with border color edgecolor='white' ) # Create a bar chart in position bar_1 ax.bar(bar_l, # using mid_rel data mid_rel, # with pre_rel bottom=pre_rel, # labeled label='Personal Vehicle Passengers', # with alpha alpha=0.9, # with color color='#3C5F5A', # with bar width width=bar_width, # with border color edgecolor='white' ) # Create a bar chart in position bar_1 ax.bar(bar_l, # using post_rel data post_rel, # with pre_rel and mid_rel on bottom bottom=[i+j for i,j in zip(pre_rel, mid_rel)], # labeled label='Pedestrians', # with alpha alpha=0.9, # with color color='#219AD8', # with bar width width=bar_width, # with border color edgecolor='white' ) # Set the ticks to be first names plt.xticks(tick_pos, df_douglas_annual_sum.index) ax.set_ylabel("Percent Of Total") ax.set_xlabel("Year") # Let the borders of the graphic plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width]) plt.ylim(-10, 110) # rotate axis labels plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') # shot plot plt.show() # Create a figure with a single subplot f, ax = plt.subplots(1, figsize=(10,5)) # Create a scatterplot of, # x axis: Latency, and plt.bar(df_douglas_annual_sum.index, # y axis: Download, with df_douglas_annual_sum['Personal Vehicle Passengers'], # the color assigned as blue color='b') plt.bar(df_douglas_annual_sum.index, # y axis: Download, with df_douglas_annual_sum['Pedestrians'], # the color assigned as blue color='r') plt.bar(df_douglas_annual_sum.index, # y axis: Download, with df_douglas_annual_sum['Bus Passengers'], # the color assigned as blue color='g') # Chart title plt.title('Types Of Border Crossers') # y label plt.ylabel('Total People') # x label plt.xlabel('Year') df.columns df_crossing = df.groupby('Port Name').sum() df_crossing.sort('Trains', ascending=False).head() df_crossing['container_total'] = df_crossing['Loaded Truck Containers'] + df_crossing['Empty Truck Containers'] df_crossing = df_crossing.sort(columns='container_total') # input data, specifically the second and # third rows, skipping the first column x1 = df_crossing['Loaded Truck Containers'] x2 = df_crossing['Empty Truck Containers'] # Create the bar labels bar_labels = df_crossing.index # Create a figure fig = plt.figure(figsize=(10,8)) # Set the y position y_pos = np.arange(len(x1)) y_pos = [x for x in y_pos] plt.yticks(y_pos, bar_labels, fontsize=10) # Create a horizontal bar in the position y_pos plt.barh(y_pos, # using x1 data x1, # that is centered align='center', # with alpha 0.4 alpha=0.4, # and color green color='#263F13') # Create a horizontal bar in the position y_pos plt.barh(y_pos, # using NEGATIVE x2 data -x2, # that is centered align='center', # with alpha 0.4 alpha=0.4, # and color green color='#77A61D') # annotation and labels plt.xlabel('Empty Containers: Light Green. Full Containers: Dark Green') t = plt.title('Comparison of Empty And Full Truck Containers Per Border Crossing') plt.ylim([-1,len(x1)+0.1]) plt.grid() plt.show()