ImNeos
ImNeos

Reputation: 567

Country borders are shifted in relation to fillings - Basemap python

I am trying to do a colored map on python using BaseMap with a Shapefile but I have small shifts between borders and countries. Anyone would happen to know how I can fix this ? I would like to color only inside the borders. I am using ne_10m_admin_0_countries packages for the shapefile.

shapefile_path = mypath


m = Basemap(projection='merc', llcrnrlat=35, urcrnrlat=70,
            llcrnrlon=-25, urcrnrlon=45, resolution='i')
m.readshapefile(shapefile_path, 'countries', drawbounds=False)

m.shadedrelief()
#m.drawcountries(color = 'grey')


country = {
    "Belgium": 100,
    "Czechia": 120,
    "Denmark": 100,
    "Finland": 142,
    "France": 5000,
    "Germany": 6500,
    "Greece": 0,
    "Iceland": 72,
    "Italy": 1499,
    "Lithuania": 150,
    "Luxembourg": 221,
    "Netherlands": 250,
    "Portugal": 100,
    "Slovakia": 0,
}


def get_color(value):
    if 0 <= value <= 250:
        return "#F8FFB9"
    elif 250 <= value <= 500:
        return "#F9CD40"  # gold
    elif 500 <= value <= 1000:
        return "#F6CE46"  # orange
    elif 1000 <= value <= 1500:
        return "#FE982A"  # orange-red
    elif 1500 <= value <= 2000:
        return "#FD351E"  # red
    elif 2000 <= value:
        return "#C10001"


for info, shape in zip(m.countries_info, m.countries):
    if info['ADMIN'] in country.keys():
        x, y = zip(*shape)
        m.plot(x, y, marker=None, color='lightgrey', linewidth = 0.1)
        #plt.fill(x, y, color=cmap(norm(country[info['ADMIN']])))
        plt.fill(x, y, color=get_color(country[info['ADMIN']]))
        #plt.fill(x, y, color=interpolate_color(country[info['ADMIN']], min_value, max_value, min_color, max_color))


    else:
        x, y = zip(*shape)
        m.plot(x, y, marker=None, color='grey', linewidth = 0.1)
        #plt.fill(x, y, color=cmap(norm(country[info['ADMIN']])))
        plt.fill(x, y, color='#EFFCFE')

colors = [get_color(0), get_color(255), get_color(505), get_color(1200), get_color(1700), get_color(2500), '#EFFCFE']
labels = ["0-250", "251-500", "501-1000", "1001-1500", "1501-2000", "2000+", "pas de données"]
patches = [mpatches.Patch(color=colors[i], label=labels[i]) for i in range(len(colors))]


font_properties = FontProperties()
font_properties.set_size('xx-small')  

plt.legend(handles=patches, title="Légendes", loc='center left', bbox_to_anchor=(1, 0.5), prop=font_properties,
            title_fontsize='small', edgecolor='lightgrey')

plt.savefig('map_image_1.png', dpi=1000, bbox_inches='tight')  

plt.show()

I get this :

enter image description here

Upvotes: 0

Views: 19

Answers (0)

Related Questions