In [1]:
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns;
from numpy import nan
from math import sqrt, sin, cos, atan2, log
plt.style.use('ggplot')
from IPython.core.display import HTML
css = open('style-table.css').read()+ open('style-notebook.css').read()
HTML('<style>{}</style>'.format(css))


Out[1]:

In [2]:
inner_data = pd.read_csv('inner_data.csv')


/Users/luis/anaconda/lib/python3.4/site-packages/IPython/core/interactiveshell.py:2902: DtypeWarning: Columns (18,19) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)

In [167]:
#full_data = full_data[full_data['ENT']!=0]

In [4]:
secciones = ['seccion_1', 'seccion_2', 'seccion_3', 'seccion_4', 'seccion_5', 'seccion_6', 'seccion_7', 'seccion_8', 'seccion_9']
resumen = ['ENT']
resumen.extend(secciones)
resumen.extend(['cal_final'])
indicadores = ['seccion_1', 'seccion_2', 'seccion_3', 'seccion_4', 'seccion_5', 'seccion_6', 'seccion_7', 'seccion_8', 'seccion_9', 'cal_final']
max_vals = np.array([11, 13, 13, 11, 14, 17, 2, 11, 8])

In [169]:
calificaciones = full_data[secciones].sum(axis=1)
full_data['cal_final'] = calificaciones


/Users/luis/anaconda/lib/python3.4/site-packages/ipykernel/__main__.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  from ipykernel import kernelapp as app

In [149]:
full_norm = full_data[resumen]

In [170]:
#full_norm[secciones] = full_norm[secciones]/max_vals
full_agg=full_data[resumen].groupby(['ENT']).agg('mean')
full_agg_median = full_data[resumen].groupby(['ENT']).agg('median')

In [171]:
#full_data = full_data[['ENT', 'MUN', 'LOC', 'AGEB', 'MZA', 'ID_INM', 'seccion_1', 'seccion_2', 'seccion_3', 'seccion_4', 'seccion_5', 'seccion_6', 'seccion_7', 'seccion_8', 'seccion_9', 'X', 'Y', 'cal_final']]
full_data[full_data['ENT']==1][indicadores].hist()


Out[171]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x12615a710>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x136ff0470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12b631cc0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x12fb74da0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x13e178e48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x13d58e7f0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x116ddfb38>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12c0201d0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x13395d390>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x12bdbebe0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12b9b86a0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11710cf98>]], dtype=object)

In [37]:
full_data.to_csv('full_data.csv')

In [5]:
sinaloa = inner_data[inner_data['ENT']==25]

In [159]:
full_data#int(sqrt(len(sinaloa['cal_final'])))


Out[159]:
ENT MUN LOC AGEB MZA ID_INM seccion_1 seccion_2 seccion_3 seccion_4 seccion_5 seccion_6 seccion_7 seccion_8 seccion_9 X Y cal_final
0 2 4 1 2876 23 1914 0.727273 0.615385 0.846154 1.0 0.839286 0.282353 0 0.800000 0.5 NaN NaN 0.6735
1 2 4 1 2880 103 1915 0.727273 0.653846 0.846154 0.5 0.821429 0.161765 0 0.436364 1.0 NaN NaN 0.6005
2 2 4 1 3677 5 1916 0.727273 0.692308 0.846154 0.5 0.839286 0.161765 0 0.436364 0.5 NaN NaN 0.5680
3 2 4 1 3677 9 1917 0.727273 0.615385 0.846154 1.0 0.839286 0.155882 0 0.909091 0.0 NaN NaN 0.6240
4 2 4 1 3677 23 1918 0.727273 0.615385 0.846154 0.5 0.764286 0.314706 0 0.763636 1.0 NaN NaN 0.6495
5 2 4 1 3681 17 1919 0.727273 0.884615 0.846154 0.0 0.817857 0.161765 0 0.545455 1.0 NaN NaN 0.5870
6 2 4 1 3681 25 1920 0.727273 0.923077 0.846154 0.0 0.571429 0.285294 0 0.745455 1.0 NaN NaN 0.6005
7 2 4 1 3817 9 1921 0.727273 0.461538 0.846154 0.0 0.839286 0.223529 0 0.654545 1.0 NaN NaN 0.5575
8 2 4 1 1191 39 1922 1.000000 0.884615 0.846154 1.0 0.821429 0.082353 0 0.109091 0.5 NaN NaN 0.6260
9 2 4 1 1191 39 1923 1.000000 0.884615 0.846154 0.5 0.750000 0.238235 0 0.545455 1.0 NaN NaN 0.6755
10 2 4 1 1191 39 1924 1.000000 0.923077 0.923077 0.0 0.910714 0.223529 0 0.436364 0.5 NaN NaN 0.6035
11 2 4 1 2467 18 1925 1.000000 0.923077 0.846154 0.0 0.817857 0.205882 0 0.763636 1.0 NaN NaN 0.6535
12 2 4 1 2467 18 2798 1.000000 0.923077 0.846154 0.5 0.660714 0.144118 0 0.654545 1.0 NaN NaN 0.6640
13 2 4 1 1806 57 1926 1.000000 0.692308 0.846154 1.0 0.678571 0.220588 1 0.763636 1.0 NaN NaN 0.7365
14 2 4 1 1806 57 1927 1.000000 0.923077 0.846154 1.0 0.657143 0.205882 0 0.327273 1.0 NaN NaN 0.6930
15 2 4 1 1806 50 1928 0.727273 0.923077 0.846154 0.0 0.839286 0.176471 0 0.872727 0.5 NaN NaN 0.5935
16 2 4 1 1806 65 1929 1.000000 0.846154 0.846154 0.5 0.639286 0.270588 0 0.436364 1.0 NaN NaN 0.6485
17 2 4 1 6525 19 1763 1.000000 0.923077 0.846154 1.0 0.839286 0.252941 0 0.872727 1.0 NaN NaN 0.7865
18 2 4 1 6525 19 2781 0.727273 0.961538 0.846154 0.5 0.839286 0.252941 0 0.872727 1.0 NaN NaN 0.7065
19 2 4 1 3821 59 1765 1.000000 0.769231 0.846154 0.5 0.839286 0.314706 0 0.872727 1.0 NaN NaN 0.7220
20 2 4 1 4266 1 1766 1.000000 0.692308 0.923077 0.0 0.817857 0.314706 0 0.872727 1.0 NaN NaN 0.6640
21 2 4 1 4266 1 2340 0.727273 0.769231 0.846154 0.0 0.892857 0.267647 0 0.909091 1.0 NaN NaN 0.6405
22 2 4 1 4463 15 1767 1.000000 0.923077 0.846154 0.5 0.839286 0.300000 0 0.763636 0.5 NaN NaN 0.6875
23 2 4 1 4463 15 2554 1.000000 0.923077 0.846154 0.5 0.839286 0.267647 0 0.763636 0.5 NaN NaN 0.6820
24 2 4 1 2927 29 1768 1.000000 0.923077 0.923077 0.5 0.839286 0.282353 0 0.036364 0.5 NaN NaN 0.6145
25 2 4 1 2927 29 1754 0.727273 1.000000 0.846154 0.5 0.728571 0.285294 0 0.800000 0.5 NaN NaN 0.6535
26 2 4 1 5601 9 1769 0.727273 0.923077 0.846154 0.0 0.839286 0.238235 0 0.872727 0.5 NaN NaN 0.6040
27 2 4 1 0210 25 1770 1.000000 1.000000 0.846154 0.0 0.678571 0.173529 0 0.872727 0.0 NaN NaN 0.5705
28 2 4 1 0475 52 1771 1.000000 0.923077 0.923077 0.5 0.839286 0.220588 0 0.654545 0.0 NaN NaN 0.6320
29 2 4 1 3412 2 1772 1.000000 0.923077 0.846154 0.5 0.839286 0.252941 0 0.654545 0.5 1084889.4391 2344929.5143 0.6675
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
232206 15 75 1 0154 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.83036e+06 859408 0.0000
232207 15 33 1 0686 60 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80954e+06 840703 0.0000
232208 15 33 1 014A 34 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80875e+06 847403 0.0000
232209 15 81 25 0240 46 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.81646e+06 857950 0.0000
232210 15 65 1 0035 6 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.83881e+06 859376 0.0000
232211 15 65 1 0035 27 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.83917e+06 859412 0.0000
232212 15 65 1 0105 27 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.83951e+06 859569 0.0000
232213 15 16 1 0223 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.83889e+06 861829 0.0000
232214 15 92 1 0206 6 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.82772e+06 858362 0.0000
232215 15 33 1 2517 16 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.81184e+06 848304 0.0000
232216 15 109 69 1345 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.79809e+06 848078 0.0000
232217 15 20 1 0029 11 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80528e+06 852794 0.0000
232218 15 2 1 0228 13 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.82295e+06 852294 0.0000
232219 15 81 1 0467 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.81567e+06 859903 0.0000
232220 15 33 1 2004 24 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.81121e+06 844897 0.0000
232221 15 13 1 0202 25 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.7881e+06 842372 0.0000
232222 15 13 1 0202 25 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.78809e+06 842396 0.0000
232223 15 104 1 1955 29 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.79557e+06 838837 0.0000
232224 15 81 19 1126 17 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80971e+06 850233 0.0000
232225 15 109 25 0898 27 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80499e+06 856601 0.0000
232226 15 104 1 0020 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.7927e+06 845336 0.0000
232227 15 104 1 0478 6 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.7894e+06 840192 0.0000
232228 15 104 1 0478 34 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.78974e+06 840064 0.0000
232229 15 13 1 0255 17 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.78905e+06 840160 0.0000
232230 15 104 1 0497 19 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.79043e+06 839753 0.0000
232231 15 104 1 0548 5 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.79009e+06 839812 0.0000
232232 15 104 1 0618 7 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.79014e+06 839608 0.0000
232233 15 33 1 4087 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80824e+06 847290 0.0000
232234 15 81 19 0132 15 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.81009e+06 855815 0.0000
232235 15 20 1 025A 29 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.80461e+06 849729 0.0000

232236 rows × 18 columns


In [153]:
#sinaloa[secciones].boxplot(figsize=(40,100))#hist(bins=int(sqrt(len(sinaloa['cal_final']))))
sinaloa[indicadores].hist(figsize=(20,15))
sinaloa.hist()


Out[153]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x12bda77f0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11267a2e8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x134745ef0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11dcc7ba8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x12f308d68>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x131269048>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12c5df2b0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12bd75198>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x12b92c7b8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x116816eb8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x130943f98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11e780198>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x116db9dd8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1168ed518>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x116902080>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12f8e1780>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x11de3e198>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11de2fa90>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x11339c1d0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x116e6b198>]], dtype=object)

In [173]:
#full_data[full_data['ENT']==25][indicadores].plot(kind='box', figsize=(20,10), ylim=(0,1.01), showmeans=True, bootstrap=1000)
for i in range(1,33):
    ax = full_data[full_data['ENT']==i][indicadores].plot(kind='box', figsize=(20,10), ylim=(0,1.01), showmeans=True, bootstrap=1000)
    fig = ax.get_figure()
    fig.savefig('estado_'+str(i)+'.png', bbox_inches='tight')


/Users/luis/anaconda/lib/python3.4/site-packages/matplotlib/pyplot.py:424: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)

In [163]:
coef = 1000
estados = ['Aguascalientes', 'Baja California', 'Baja California Sur']
#COLORS
colors = ['peru', 'hotpink', 'crimson', 'darkslategray', 'lightsalmon', 'gray', 'firebrick', 'lightgreen', 'olive', 'y']
#colors = [[color[0]]*9, [color[1]]*9, [color[2]]*9 ]
#for i in range(3):
#    colors[i].extend('y')
#GRID
fig = plt.figure(figsize=(10,30))
ax = fig.add_subplot(1,1,1)
ax.set_axis_bgcolor('white')

major_yticks = np.arange(0, 33, 5)
ax.set_yticks(major_yticks)                                           
major_xticks = np.arange(0, 11, 5)
ax.set_xticks(major_xticks)


minor_yticks = np.arange(0, 33, 1)
ax.set_yticks(minor_yticks, minor=True)                                           
minor_xticks = np.arange(0, 11, 1)
ax.set_xticks(minor_xticks, minor=True)


#ax.grid(which='major', c = 'white', linestyle='-')
ax.grid(which='minor', c = 'green', linestyle=':')


for i in full_agg.index.values:
    plt.scatter(range(1,11),[i]*10, s=coef, c = colors, alpha=0.1, edgecolor='k', lw=1)
    plt.scatter(range(1,11), [i]*10, s=full_agg.ix[i].map(lambda x: coef*x), c = colors)
plt.savefig('bubble_plot.png', bbox_inches='tight')
plt.show()


/Users/luis/anaconda/lib/python3.4/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):