OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Zoom in & Zoom out basemap contextily

  • Thread starter Thread starter Samuele
  • Start date Start date
S

Samuele

Guest
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):

Code:
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()
<p>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 <code>figsize</code> method but doesn't work with the basemap.</p>
<p><a href="https://i.sstatic.net/H3heI63O.png" rel="nofollow noreferrer">Plot of Spain that goes out of bound</a>
<a href="https://i.sstatic.net/YjczkYAx.png" rel="nofollow noreferrer">Plot of United States that is really small</a></p>
<p>Here there is the plotting code for the United States (it's pretty similar for the Spain):</p>
<pre><code>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()
</code></pre>
 

Latest posts

C
Replies
0
Views
1
CameronBaba
C
A
Replies
0
Views
1
Adembilal Bilal
A
F
Replies
0
Views
1
Farhan Muhammad
F
Top