Browse Source

updating onecity analysis scripts. univariate and scatter plots. we have our first pair of three plots: one for everyone, one for pre-adolescents, one for seniors.

master
Charles Reid 10 years ago
parent
commit
24583a9274
  1. 137
      analysis/OneCity_Scatter.py
  2. 106
      analysis/OneCity_Univariate.py

137
analysis/OneCity_Scatter.py

@ -40,9 +40,14 @@ if do_univariate_kde: @@ -40,9 +40,14 @@ if do_univariate_kde:
# Mongo lookup
cbsa = mt.CBSACode(city)[0]
pre_search = metaprops.find_one({'geoid':cbsa})
mongo_search = props.find({'metroid':cbsa})
mongo_search = props.find({'$and': [
{'metroid':cbsa},
{'geoid':{'$nin':[cbsa]}}
]
})
if 'B17001' not in pre_search['tables']:
print "Error: could not find table B17001 for city",city,"in db."
@ -52,9 +57,15 @@ if do_univariate_kde: @@ -52,9 +57,15 @@ if do_univariate_kde:
print "Error: could not find city",city,"in db."
continue
df = pd.DataFrame([])
z = list(mongo_search)
df = df.append(z)
srch = list(mongo_search)
for i,r in enumerate(srch):
if cbsa in r['geoid']:
del srch[i]
break
df = df.append(srch)
df = Table17001(df)
@ -64,88 +75,76 @@ if do_univariate_kde: @@ -64,88 +75,76 @@ if do_univariate_kde:
# Scatter plots:
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
# ------------------------
# Subplot 1
ax1.set_title(cities[0])
sns.regplot(df['B17001003'],df['B17001031'], color=c3, ax=ax1, fit_reg=False)
xlim = 1000
ylim = 10000
#xcode = 'B17001003'
#xlabel = metaprops.find_one({'code':xcode})['name']
#ycode = 'B17001031'
#ylabel = metaprops.find_one({'code':ycode})['name']
ax1.set_xlim([-50,xlim])
ax1.set_ylim([-50,ylim])
xcode = 'TotalPop'
xlabel=xcode
ycode = 'M_P_PctM'
ylabel=ycode
sns.regplot(df[xcode],df[ycode], color=c2, ax=ax1, fit_reg=False)
xcode = 'TotalPop'
xlabel=xcode
ycode = 'F_P_PctF'
ylabel=ycode
sns.regplot(df[xcode],df[ycode], color=c1, ax=ax1, fit_reg=False)
'''
#######################
# Plot:
# Pre-adolescent poverty levels
# (overall and by gender)
xlim = (min(df[xcode]),
max(df[xcode]))
ylim = (min(df[ycode]),
max(df[ycode]))
xlim = 0.50
ylim = 5
ax1.set_xlim(xlim)
ax1.set_ylim(ylim)
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0] + " " + cities[1] + "Pre-Adolesc")
sns.kdeplot( df['PAP_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df2['PAP_Pct'], shade=True, color=c2, ax=ax1)
sns.kdeplot( df['PAM_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot( df['PAF_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig("onecity_univariate_"+city+"_2.jpg")
ax1.set_xlabel(xlabel)
ax1.set_ylabel('Pct of M/F Pop in Pov')
# ------------------------
# Subplot 2
xcode = 'TotalPop'
xlabel=xcode
ycode = 'SrM_P_PctM'
ylabel=ycode
sns.regplot(df[xcode],df[ycode], color=c2, ax=ax2, fit_reg=False)
#sns.regplot(df[xcode],df[ycode] - ((df['TotalPop']*df[ycode])/df['TotalPop']), color=c1, ax=ax2, fit_reg=False)
xcode = 'TotalPop'
xlabel=xcode
ycode = 'SrF_P_PctF'
ylabel=ycode
sns.regplot(df[xcode],df[ycode], color=c1, ax=ax2, fit_reg=False)
#sns.regplot(df[xcode],abs(df[ycode] - df[ycode].mean()), color=c1, ax=ax2, fit_reg=False)
xlim = (min(df[xcode]),
max(df[xcode]))
ylim = (min(df[ycode]),
max(df[ycode]))
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
ax2.set_xlabel(xlabel)
#ax2.set_ylabel('Pct of M/F Pre-Adol. Pop in Pov')
#######################
# Plot:
# Juvenile poverty levels
# (overall and by gender)
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0] + " " + cities[1] + "Juveniles")
sns.kdeplot( df['JuvP_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df2['JuvP_Pct'], shade=True, color=c2, ax=ax1)
sns.kdeplot( df['JuvM_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot( df['JuvF_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig("onecity_univariate_"+city+"_3.jpg")
#######################
# Plot:
# Senior poverty levels
# (overall and by gender)
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0] + " " + cities[1] + "Seniors")
sns.kdeplot( df['SrP_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df2['SrP_Pct'], shade=True, color=c2, ax=ax1)
sns.kdeplot( df['SrM_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot( df['SrF_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig(cities[0]+"_g2_3.jpg")
'''
plt.draw()
plt.show()

106
analysis/OneCity_Univariate.py

@ -40,9 +40,14 @@ if do_univariate_kde: @@ -40,9 +40,14 @@ if do_univariate_kde:
# Mongo lookup
cbsa = mt.CBSACode(city)[0]
pre_search = metaprops.find_one({'geoid':cbsa})
mongo_search = props.find({'metroid':cbsa})
mongo_search = props.find({'$and': [
{'metroid':cbsa},
{'geoid':{'$nin':[cbsa]}}
]
})
if 'B17001' not in pre_search['tables']:
print "Error: could not find table B17001 for city",city,"in db."
@ -52,9 +57,14 @@ if do_univariate_kde: @@ -52,9 +57,14 @@ if do_univariate_kde:
print "Error: could not find city",city,"in db."
continue
df = pd.DataFrame([])
z = list(mongo_search)
df = df.append(z)
srch = list(mongo_search)
for i,r in enumerate(srch):
if cbsa in r['geoid']:
del srch[i]
break
df = df.append(srch)
df = Table17001(df)
@ -63,23 +73,19 @@ if do_univariate_kde: @@ -63,23 +73,19 @@ if do_univariate_kde:
# Percent of people in poverty
# overall, and by gender
xlim = 0.50
ylim = 5
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0])
#ax1.set_title(cities[0])
sns.kdeplot(df['P_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df['M_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot(df['F_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_xlim([0,1])
ax2.set_xlim([0,1])
#
#ax1.set_ylim([0,ylim])
#ax2.set_ylim([0,ylim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig("onecity_univariate_"+city+"_1.jpg")
#f.savefig("onecity_univariate_"+city+"_1.jpg")
@ -93,43 +99,43 @@ if do_univariate_kde: @@ -93,43 +99,43 @@ if do_univariate_kde:
ylim = 5
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0] + " " + cities[1] + "Pre-Adolesc")
#ax1.set_title(cities[0] + " " + cities[1] + "Pre-Adolesc")
sns.kdeplot( df['PAP_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df2['PAP_Pct'], shade=True, color=c2, ax=ax1)
#sns.kdeplot(df2['PAP_Pct'], shade=True, color=c2, ax=ax1)
sns.kdeplot( df['PAM_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot( df['PAF_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_xlim([0,1])
ax2.set_xlim([0,1])
#
#ax1.set_ylim([0,ylim])
#ax2.set_ylim([0,ylim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig("onecity_univariate_"+city+"_2.jpg")
#f.savefig("onecity_univariate_"+city+"_2.jpg")
#######################
# Plot:
# Juvenile poverty levels
# (overall and by gender)
### #######################
### # Plot:
### # Juvenile poverty levels
### # (overall and by gender)
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0] + " " + cities[1] + "Juveniles")
sns.kdeplot( df['JuvP_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df2['JuvP_Pct'], shade=True, color=c2, ax=ax1)
sns.kdeplot( df['JuvM_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot( df['JuvF_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig("onecity_univariate_"+city+"_3.jpg")
### f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
### #ax1.set_title(cities[0] + " " + cities[1] + "Juveniles")
### sns.kdeplot( df['JuvP_Pct'], shade=True, color=c3, ax=ax1)
### #sns.kdeplot(df2['JuvP_Pct'], shade=True, color=c2, ax=ax1)
### sns.kdeplot( df['JuvM_P_PctM'], shade=True, color=c2, ax=ax2)
### sns.kdeplot( df['JuvF_P_PctF'], shade=True, color=c1, ax=ax2);
###
### ax1.set_xlim([0,1])
### ax2.set_xlim([0,1])
### #
### #ax1.set_ylim([0,ylim])
### #ax2.set_ylim([0,ylim])
###
### #f.savefig("onecity_univariate_"+city+"_3.jpg")
@ -139,18 +145,20 @@ if do_univariate_kde: @@ -139,18 +145,20 @@ if do_univariate_kde:
# (overall and by gender)
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 8))
ax1.set_title(cities[0] + " " + cities[1] + "Seniors")
#ax1.set_title(cities[0] + " " + cities[1] + "Seniors")
sns.kdeplot( df['SrP_Pct'], shade=True, color=c3, ax=ax1)
sns.kdeplot(df2['SrP_Pct'], shade=True, color=c2, ax=ax1)
#sns.kdeplot(df2['SrP_Pct'], shade=True, color=c2, ax=ax1)
sns.kdeplot( df['SrM_P_PctM'], shade=True, color=c2, ax=ax2)
sns.kdeplot( df['SrF_P_PctF'], shade=True, color=c1, ax=ax2);
ax1.set_xlim([0,1])
ax2.set_xlim([0,1])
#
#ax1.set_ylim([0,ylim])
#ax2.set_ylim([0,ylim])
ax1.set_xlim([0,xlim])
ax2.set_xlim([0,xlim])
ax1.set_ylim([0,ylim])
ax2.set_ylim([0,ylim])
f.savefig(cities[0]+"_g2_3.jpg")
#f.savefig(cities[0]+"_g2_3.jpg")
plt.show()
plt.draw()

Loading…
Cancel
Save