I'm trying to animate a heat map of some information regarding geographic locations in Pittsburgh using matplotlib and basemap in Python 3. Right now I'm having issues getting basemap to use ARCGis imagery as the background. The following code only produces a blue square
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
m =Basemap(llcrnrlon=40.361369,llcrnrlat=-80.0955278,
urcrnrlon=40.501368,urcrnrlat=-79.865723,epsg=2272)
m.arcgisimage(service='ESRI_StreetMap_World_2D', xpixels=7000,dpi=96,verbose=True)
I've pulled down and run several examples from the internet about how to use arcgis images with basemap and they have run so I'm pretty sure its not a connection issue. I've tried several different projections and EPSG's including the world and US EPSGs, but no luck. Any help would be appreciated.
You have the longitudes and latitudes mixed up (see here):
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(
llcrnrlat=40.361369, llcrnrlon=-80.0955278,
urcrnrlat=40.501368, urcrnrlon=-79.865723,
epsg = 2272
)
m.arcgisimage(service='ESRI_StreetMap_World_2D', xpixels=7000, verbose=True)
plt.show()
produces this image:
Related
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots(2,2,figsize=(15,10))
x=np.linspace(-3,3)
ax[0,0].plot(x,foo-function)
now I need a way to save each of the 4 plots into one file like this:
plt1=topleft_plot.saveNOTfigBUTplot('quadfunction.pdf')
how?
Using the answer here: https://stackoverflow.com/a/4328608/16299117
We can do the following to save a SINGLE subplot from the overall figure:
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots(2,2,figsize=(15,10))
x=np.linspace(-3,3)
ax[0,0].plot(x,x**2) # This is just to make an actual plot.
# I am not using jupyter notebook, so I use this to show it instead of %inline
plt.show()
# Getting only the axes specified by ax[0,0]
extent = ax[0,0].get_window_extent().transformed(fig.dpi_scale_trans.inverted())
# Saving it to a pdf file.
fig.savefig('ax2_figure.pdf', bbox_inches=extent.expanded(1.1, 1.2))
EDIT: I believe I may have misunderstood what you want. If you want to save EACH plot individually, say as 4 different pages in a pdf, you can do the following adapted from this answer: https://stackoverflow.com/a/29435953/16299117
This will save each subplot from the figure as a different page in a single pdf.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
fig,ax=plt.subplots(2,2,figsize=(15,10))
x=np.linspace(-3,3)
ax[0,0].plot(x,x**2) # This is just to make an actual plot.
with PdfPages('foo.pdf') as pdf:
for x in range(ax.shape[0]):
for y in range(ax.shape[1]):
extent = ax[x, y].get_window_extent().transformed(fig.dpi_scale_trans.inverted())
pdf.savefig(bbox_inches=extent.expanded(1.1, 1.2))
I'm having a bit of trouble plotting polygon/multipolygon data with Geoviews. I have made a geodataframe that combines my two datasets together nicely. I am able to plot the data easily using the plot function:
See below:
import geopandas as gpd
import numpy as np
import pandas as pd
import holoviews as hv
import geoviews as gv
import geoviews.feature as gf
import cartopy
import cartopy.feature as cf
from geoviews import opts
from cartopy import crs as ccrs
gv.extension('bokeh')
Drop_na.plot(column = 'Registrations',figsize=(10,10), legend=True)
This gives me the below chart:
However, when I try to plot the same using GeoViews with the code below:
gv.Polygons(Drop_na, vdims=['Registrations']).opts(
tools=['hover'], width=550, height=700, color_index='Registrations',
colorbar=True, toolbar='above', xaxis=None, yaxis=None, padding=0.1)
I get the following error: "While projecting a Polygons element from a PlateCarree coordinate reference system (crs) to a PlateCarree projection none of the projected paths were contained within the bounds specified by the projection. Ensure you have specified the correct coordinate system for your data."
I've read around the documentation and have tried different ways of messing around with the projection method but i just can't get it generate the same chart or any chart for that matter.
Does anyone know what i'm doing wrong?
Kind regards,
Amen
Sample result I want to color code the circle plots in my scatter and associate them with a legend.
I have made various attempts at a solution referencing matplotlib.org and this website. All to no avail.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
hysector = pd.read_csv('LF98TRUU_Lvl3_sector.csv',index_col=0)
hysector.plot.scatter(x='OAD',y='OAS',s=hysector['Wgt']*35,label='Sector');
I'm simply trying to plot a map and add the borders of the countries.
Here is the code snipped:
map=Basemap(projection="lcc",resolution="l",width=1E6,height=1E6,lon_0=9.9167,lat_0=51.5167,fix_aspect=False)
map.drawcounties(zorder=1,color="black")
map.shadedrelief()
map.drawcoastlines(color="black",linewidth=2)
map.drawrivers(linewidth=0.5,color="blue")
map.drawmapboundary()
Everything is working besides the borders....There is also no
Error...it simply does nothing.
What am I doing wrong?
Additionaly the resolution of the map is a bit blurred. Is there any way to boost the resolution?
Thanks for your answers!!!
To draw to borders of the countries you need drawcountries. (Mind the r)
To draw to borders of the counties you need drawcounties.
Note however that the german Bundesländer are no "counties" in the sense of the basemap, so it will not draw them.
To get a higher resolution try resolution="i" in the Basemap initialization.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
map=Basemap(projection="lcc",resolution="i",width=1E6,height=1E6,
lon_0=9.9167,lat_0=51.5167,fix_aspect=False)
map.drawcountries(zorder=1,color="black", linewidth=1)
map.shadedrelief()
map.drawcoastlines(color="black",linewidth=1.2)
map.drawrivers(linewidth=0.5,color="blue")
map.drawmapboundary()
plt.show()
Plotting a very, very simple map of only europe in matplotlib / basemap takes so much time (around 10 seconds!). This is just unreal!?
Setting of resolution is only "l" (low).
Here is the very simple code:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='stere',lon_0=5,lat_0=90.0,rsphere=6371200.,\
llcrnrlon=-25.0,urcrnrlon=72.0,llcrnrlat=26.0,urcrnrlat=65.0,resolution='l')
m.drawcoastlines(linewidth=0.2)
m.drawcountries(linewidth=0.2)
plt.savefig('/var/www/map.png')
I need to plot hundreds of these maps every 2 hours. This would be impossible? :( Only idea is: Create an empty Basemap and try to draw boundaries with a shapefile.
Regards,
John