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?

5
  • have you run "%matplotlib inline" with your import statements in the jupyter notebook? Commented Sep 23, 2017 at 16:21
  • Yes, I have. The graphs show up in my jupyter notebook, but separately. Commented Sep 23, 2017 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 minimal reproducible example available. Commented Sep 23, 2017 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? Commented Sep 23, 2017 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. Commented Sep 23, 2017 at 20:30

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.