Skip to content

Instantly share code, notes, and snippets.

@kylebarlow
Created May 28, 2013 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylebarlow/5665584 to your computer and use it in GitHub Desktop.
Save kylebarlow/5665584 to your computer and use it in GitHub Desktop.
Script to plot gender roadtrip data that I generated. Used for my post on Beaker Report: http://www.beakerreport.com/2013/05/28/genderdriving/
#!/usr/bin/python
'''
Script to plot gender roadtrip data using matplotlib
'''
__author__ = "Kyle Barlow"
__email__ = "kb@kylebarlow.com"
__license__ = "GPL v3"
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.font_manager as fm
default_colors=['blue','pink','red','green']
class GenderData:
def __init__(self):
self.woman={
'man' : 7,
'woman' : 3,
'self' : 3
}
self.man={
'man' : 4,
'woman' : 18,
'self' : 15
}
self.men_total=0
self.men_driving=0
self.men_passengers=0
self.men_solo=0
self.women_total=0
self.women_driving=0
self.women_passengers=0
self.women_solo=0
for key in self.woman:
val=self.woman[key]
self.women_driving+=val
self.women_total+=val
if key=='man':
self.men_total+=val
self.men_passengers+=val
elif key=='woman':
self.women_total+=val
self.women_passengers+=val
elif key=='self':
self.women_solo+=val
for key in self.man:
val=self.man[key]
self.men_driving+=val
self.men_total+=val
if key=='man':
self.men_total+=val
self.men_passengers+=val
elif key=='woman':
self.women_total+=val
self.women_passengers+=val
elif key=='self':
self.men_solo+=val
def print_summary(self):
print '%d total women (%d drivers, %d passengers)'%(self.women_total,self.women_driving,self.women_passengers)
print '%d total men (%d drivers, %d passengers)'%(self.men_total,self.men_driving,self.men_passengers)
def plot(self):
myBlue=(.04,.39,.64)
fig = plt.figure(figsize=(12,10),dpi=75)
gs = gridspec.GridSpec(4, 1)
gs.update()
# Make graph 0
ax0=plt.subplot(gs[0])
t='Count of mixed gender driving pairs\nobserved on Highway 101 on 5/24/2013'
y_labels=('Women driving men', 'Men driving women')
data=(self.woman['man'],self.man['woman'])
make_bargraph(ax0,data,title_label=t,xlabel='Count',ylabels=y_labels,
colors=['pink',myBlue],)
# Make graph 1
ax1=plt.subplot(gs[1])
t='Total drivers'
y_labels=('Female', 'Male')
data=(self.women_driving,self.men_driving)
make_bargraph(ax1,data,title_label=t,xlabel='Count',ylabels=y_labels,
colors=['pink',myBlue],)
# Make graph 2
ax2=plt.subplot(gs[2])
t='Total on the road\n(drivers or passengers)'
y_labels=('Female', 'Male')
data=(self.women_total,self.men_total)
make_bargraph(ax2,data,title_label=t,xlabel='Count',ylabels=y_labels,
colors=['pink',myBlue],)
# Make graph 3
ax3=plt.subplot(gs[3])
t='Solo drivers'
y_labels=('Female', 'Male')
data=(self.women_solo,self.men_solo)
make_bargraph(ax3,data,title_label=t,xlabel='Count',ylabels=y_labels,
colors=['pink',myBlue],)
#ax.legend( (rects1[0], rects2[0]), ('Women driving men', 'Men driving women'), loc='lower right' )
fig.patch.set_facecolor('white')
gs.tight_layout(fig)
plt.show()
def make_bargraph(ax,data,title_label='Title',xlabel='X axis',ylabels=[],
colors=[],):
width=1.0
spacer=width/4
bar_y_locations=[x*width+spacer*(x+1) for x in xrange(0,len(data))]
rects=[]
for i,datum in enumerate(data):
if len(colors)<i:
color=default_colors[color]
else:
color=colors[i]
rect = ax.barh(bar_y_locations[i],
(datum),
width, color=color)
rects.append(rect)
plt.ylim([0,bar_y_locations[-1]+width+spacer])
plt.xlim([0,max(data)*1.05])
title_font='Oswald-Regular.ttf'
small_font='OpenSans-Regular.ttf'
title_prop = fm.FontProperties(fname=title_font, size=32)
label_prop = fm.FontProperties(fname=small_font, size=24)
tick_prop = fm.FontProperties(fname=small_font, size=18)
ax.set_title(title_label,fontproperties=title_prop)
ax.set_xlabel(xlabel,fontproperties=label_prop)
ytick_locations=[x+width/2 for x in bar_y_locations]#[width/2+spacer,3*(width/2)+2*spacer]
ax.set_yticks(ytick_locations)
if len(ylabels)==len(data):
ax.set_yticklabels(ylabels,
fontproperties=tick_prop)
for i in xrange(0,len(data)):
y_loc=ytick_locations[i]-0.05
count=data[i]
plt.annotate(str(count),xy=(count+0.2,y_loc),xycoords='data',fontproperties=tick_prop)
for label in ax.get_xticklabels():
label.set_fontproperties(tick_prop)
def main():
data=GenderData()
data.print_summary()
data.plot()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment