In [62]:
import sys
import os
import matplotlib.pyplot as plt
%pylab inline
In [63]:
sys.path.append(os.path.abspath('..'))
from pysal.explore import inequality
import pysal.lib
In [64]:
pysal.lib.examples.available()
Out[64]:
In [65]:
pysal.lib.examples.explain('mexico')
Out[65]:
In [66]:
import geopandas
pth = pysal.lib.examples.get_path("mexicojoin.shp")
gdf = geopandas.read_file(pth)
from pysal.lib.weights import Queen, Rook, KNN
In [67]:
%matplotlib inline
import matplotlib.pyplot as plt
In [68]:
ax = gdf.plot()
ax.set_axis_off()
In [69]:
gdf.head()
Out[69]:
In [99]:
ax = gdf.plot(column='PCGDP1940',k=5,scheme='Quantiles',legend=True)
ax.set_axis_off()
#ax.set_title("PC GDP 1940")
plt.savefig('1940.png')
In [71]:
gini_1940 = inequality.gini.Gini(gdf['PCGDP1940'])
In [72]:
gini_1940.g
Out[72]:
In [73]:
decades = range(1940, 2010, 10)
decades
Out[73]:
In [74]:
ginis = [ inequality.gini.Gini(gdf["PCGDP%s"%decade]).g for decade in decades]
In [75]:
ginis
Out[75]:
In [76]:
inequality.gini.Gini_Spatial
Out[76]:
In [77]:
regimes = gdf['HANSON98']
In [78]:
w = pysal.lib.weights.block_weights(regimes)
In [98]:
ax = gdf.plot(column='HANSON98', categorical=True)
#ax.set_title('Regions')
ax.set_axis_off()
plt.savefig('regions.png')
In [80]:
import numpy as np
np.random.seed(12345)
gs = inequality.gini.Gini_Spatial(gdf['PCGDP1940'],w)
In [81]:
gs.p_sim
Out[81]:
In [82]:
gs_all = [ inequality.gini.Gini_Spatial(gdf["PCGDP%s"%decade], w) for decade in decades]
In [83]:
p_values = [gs.p_sim for gs in gs_all]
In [84]:
p_values
Out[84]:
In [85]:
wgs = [gs.wcg_share for gs in gs_all]
In [86]:
wgs
Out[86]:
In [87]:
bgs = [ 1 - wg for wg in wgs]
In [88]:
bgs
Out[88]:
In [89]:
%pylab inline
In [90]:
years = np.array(decades)
In [91]:
years
Out[91]:
In [97]:
fig, ax1 = plt.subplots()
t = years
s1 = ginis
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('Year')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('Gini', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
s2 = bgs
ax2.plot(t, s2, 'r-.')
ax2.set_ylabel('Spatial Inequality Share', color='r')
ax2.tick_params('y', colors='r')
fig.tight_layout()
plt.savefig('share.png')
In [ ]:
In [ ]:
In [ ]: