Search code examples
pythonmatplotlibjupyter-notebookgeopandascontextily

Zoom in & Zoom out basemap contextily


I'm doing a data science project with the Global Terrorism Dataset in python. I was wondering if it's allowed to enlarge or decrease the size of the basemap, because the images in the following files go out of bound in the Jupyter presentation. I've already tried the figsize method but doesn't work with the basemap.

Plot of Spain that goes out of bound Plot of United States that is really small

Here there is the plotting code for the United States (it's pretty similar for the Spain):

topUsa = unionTopFive[unionTopFive["country_name"] == "United States"]

mortiUsa = geoTr[(geoTr['country_name'] == 'United States') & (geoTr['nkill'] > 0)]
feritiUsa = geoTr[(geoTr['country_name'] == 'United States') & (geoTr['nwound'] > 0)]

mortiUsa['nkill'] *= 1.1 # scalo la visualizzazione
feritiUsa['nwound'] *= 1.1


axUsa = mortiUsa.plot(column='nkill', alpha=0.5, marker='o', color="black", label="Morti", figsize=(13, 13), markersize="nkill")
feritiUsa.plot(ax=axUsa, marker='o', color='red', alpha=0.3, label="Feriti",figsize=(13, 13), markersize="nwound")
ctx.add_basemap(
    axUsa,
    crs=geoTr.crs.to_string(),
    zoom=5,
    source=ctx.providers.CartoDB.Positron
    #source=ctx.providers.OpenStreetMap.Mapnik
)

axUsa.legend(frameon=True, loc="upper left", markerscale=0.2, facecolor="white", framealpha=1, fontsize=11)
axUsa.set_axis_off()

#axUsa.get_figure().savefig("usaImg.pdf",format="pdf")

#   Putroppo automatizzando il processo le coordinate delle label non erano troppo modificabili e si accumulavano una sopra l'altra.
#   Quindi, ho mantenuto lo stile iterabile per riutillizare il lavoro svolto.
'''
for l in range(0, length):
    axIta.annotate(topIta.iloc[l,:]["annotateOnly"],
                    xy=(topIta.iloc[l,:]["longitude"], topIta.iloc[l,:]["latitude"]), 
                 xytext=(topIta.iloc[l,:]["longitude"]-random.randint(2, 4), topIta.iloc[l,:]["latitude"]-random.randint(1, 2)), xycoords='data', textcoords='data',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
             bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))
'''
axUsa.annotate(topUsa.iloc[0,:]["annotateOnly"],
                xy=(topUsa.iloc[0,:]["longitude"], topUsa.iloc[0,:]["latitude"]), 
                xytext=(topUsa.iloc[0,:]["longitude"]-11, topUsa.iloc[0,:]["latitude"]+5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[2,:]["annotateOnly"],
                xy=(topUsa.iloc[2,:]["longitude"], topUsa.iloc[2,:]["latitude"]), 
                xytext=(topUsa.iloc[2,:]["longitude"]-11, topUsa.iloc[2,:]["latitude"]+0.5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[1,:]["annotateOnly"],
                xy=(topUsa.iloc[1,:]["longitude"], topUsa.iloc[1,:]["latitude"]), 
                xytext=(topUsa.iloc[1,:]["longitude"]+5, topUsa.iloc[1,:]["latitude"]-4.4), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[3,:]["annotateOnly"],
                xy=(topUsa.iloc[3,:]["longitude"], topUsa.iloc[3,:]["latitude"]), 
                xytext=(topUsa.iloc[3,:]["longitude"]-5, topUsa.iloc[3,:]["latitude"]+5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[4,:]["annotateOnly"],
                xy=(topUsa.iloc[4,:]["longitude"], topUsa.iloc[4,:]["latitude"]), 
                xytext=(topUsa.iloc[4,:]["longitude"]+1, topUsa.iloc[4,:]["latitude"]-5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))
plt.show()

Solution

  • Have you tried creating the figure prior to plotting and then setting extents to wanted coordinates ?

    fig, axUSA = plt.subplots(figsize=(10, 10))
    axUSA.set_xlim([-130, -60])
    axUSA.set_ylim([20, 50])
    

    Just pass the ax=axUSA to the plot commands then.