1

Link to full code

I am able to successfully create a Basemap of the US within a Jupyter Notebook complete with shapefile, coloring, and borders in Cell 4.

I am trying to plot many data points onto this Basemap with the following line in Cell 8:

m.plot(x, y, marker='o', markersize=data, color='#444444', alpha=0.8, latlon=True)

The data gets plotted, but I lose all of my Basemap's formatting and shaping. Effectively, I want Cell 8 overlayed on Cell 4. I suspect I am not plotting these shapes on the same plane.

Additionally, plt.show() gives me nothing. What am I missing?

Alex
  • 185
  • 1
  • 1
  • 14
  • have you run "%matplotlib inline" with your import statements in the jupyter notebook? – flyingmeatball Sep 23 '17 at 16:21
  • Yes, I have. The graphs show up in my jupyter notebook, but separately. – Alex Sep 23 '17 at 16:23
  • So, I suspect everything works fine if you put the whole code in one cell, right?! For testing it would be good to have a [mcve] available. – ImportanceOfBeingErnest Sep 23 '17 at 16:35
  • @ImportanceOfBeingErnest True, I could have boiled the example down. Both this answer (putting it all in 1 cell) and your below answer (passing axis arguments to basemap) are great solutions. Thank you. I wonder why 1 cell works but splitting cells doesnt. Memory? – Alex Sep 23 '17 at 20:04
  • No the reason is that there is no current figure present when plotting in the new cell, such that a new figure is created. You may prevent that from happening as detailed in a new part of my answer below. – ImportanceOfBeingErnest Sep 23 '17 at 20:30

1 Answers1

1

A. using a figure instance

The idea can be to explicitely specify the axes to plot to at Basemap creation.

Cell 1:

fig, ax = plt.subplots()
m = Basemap(... , ax=ax)

Cell 2: # do other stuff

Cell 3:

# plot to map:
m.plot(...)

Cell 4:

# state figure object to show figure (when inline backend is in use) 
fig


Screenshot of example:

enter image description here

B. let pyplot not close figures

The other option would be to let pyplot not close the figures. This is the second option from this answer: How to overlay plots from different cells?

%config InlineBackend.close_figures=False

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712