示例#1
0
        def city_mapper(crimes_base, address_list, show_city_block=True):

                """Create a custom map of Chicago city with its districts. The color of each district will be determined
                by the relative ammount of crimes by square mile of that district. Police stations will be plotted as a
                violet points.

                :param crimes_base a CrimesDataFrame instance
                :param address_list a list of instances of addressClass
                """

                # Validate the crimes_base input
                mu._crime_dataframe_validator(crimes_base)

                # Validate the address_list input
                mu._address_object_list_validator(address_list)

                # Get the crime density by district
                crimes_by_sq_tentative = crimes_base.crime_density_by_district()

                # Validate that the output is correct, which should be a Dictionary with the valid districts as keys and densities represented by numbers as values
                crimes_by_sq = mu._city_mapper_validator(crimes_by_sq_tentative)

                # Create a colormap to show safer areas with blue and dangerous areas with red.
                cmap = plt.get_cmap('RdYlBu_r', int(max(crimes_by_sq.values())))

                # Create equally spaced labels for a color bar.
                labels = mu._steps_labels_for_colorbar_creator(crimes_by_sq)

                # Start plotting the figure
                plt.close('all')
                fig = plt.figure()
                ax = fig.add_subplot(111)

                # Create a basemap instance of the city with district
                city_map = mu._basemap_city()

                # Iterate through each district and paint it with the proper color.
                for key in crimes_by_sq.keys():
                    patches = []
                    for info, shape in zip(city_map.district_m_info, city_map.district_m):
                        if info['DIST_NUM'] == str(key):
                            patches.append(Polygon(np.array(shape), True))
                    pc = PatchCollection(patches, alpha=1, edgecolor='k', linewidths=1, zorder=2)

                    if int(crimes_by_sq[key]) == 0: # Meaning very little to no crimes in that district, we paint it in green
                        pc.set_facecolor('#00008B')
                    else:  # Otherwise we paint it in the proper color of the colormap
                        pc.set_facecolor(cmap(int(crimes_by_sq[key])))
                    ax.add_collection(pc)



                # Plot points representing each address with the proper street address annotation.
                for address in address_list:
                    address_lat, address_lon, street_address = address.lat, address.lon, address.street_address
                    x_address, y_address = city_map(address_lon, address_lat)
                    city_map.plot(x_address, y_address, marker='D',color='k')
                    plt.annotate(street_address, xy=(x_address, y_address), xycoords='data', xytext=(city_map(address_lon + 0.0015, address_lat + 0.0015)), textcoords='data', color='k', size='small', rotation=-35)


                # Add a color bar by using the _custom_colorbar function
                if int(max(crimes_by_sq.values())) > 1:
                    cbar = mu._custom_colorbar(cmap, len(labels), labels, shrink=1)
                    cbar.ax.tick_params(labelsize=16)
                    cbar.set_label('Number of crimes by Square mile in each District', size=16)
                    fig.canvas.set_window_title('Number of crimes by Square mile')
                else:
                    fig.canvas.set_window_title('No significant crimes for the selection. Please choose more crimes or years')
                    print ('No significant crimes for the selection. Please choose more crimes or years')

                # Set figuresize, save and show.
                plt.gcf().set_size_inches(15, 15)
                plt.show(block=show_city_block)