Plotting data in Jupyter Notebook with country map as base

Viewed 664

I have a shapefile of India, and on that map I want to plot the Indian cities as points. I am using Jupyter Notebook with Python 3.7, running on Ubuntu 18.04.

ind_states = gpd.read_file('/media/debanjandey/Developer zone/Data ML works/Igismap_1/Indian_States.shp')
ind_states.head()

Output:

Indian states

Map obtained:

India map using shape file

Now to place the cities, I want to use ind_states as ax attribute of plot(), on which these points will be plotted.

For this, I am supposed to save the previous map in a variable, say ind_map and set ax=ind_map while plotting. This works well if the previous code and this code is written in the same notebook cell.

ind_places = gpd.read_file('/media/debanjandey/Developer zone/Data ML works/places/places.shp') #points
ind_places[ind_places['type'] == 'city'].plot(ax= ind_map, markersize=4, c='antiquewhite')

Output:

Indian cities

But if I write the last 2 lines of code in a separate cell (to separate output of head() from the plot), then this is the output:

<matplotlib.axes._subplots.AxesSubplot at 0x7f6bdeb24750>
<Figure size 432x288 with 0 Axes>

and no graph is visible.

However, if I specify the value of ax specifically, i.e.,

ind_places[ind_places['type'] == 'city'].plot(ax= ind_states.plot(figsize=(30,15), color='#4A1989'), 
                              markersize=4, c='antiquewhite')

Then the map is visible as expected.

What is the reason behind this behaviour? I did a similar thing while practicing in Kaggle GeoPandas course and the output map was visible in each case. Then why is it not working in my setup (i.e. in 2nd case where no plot is visible)? What are the ways to fix this?

0 Answers
Related