Plotting data points onto matplotlib Basemap in Jupyter Notebook

强颜欢笑 提交于 2020-01-15 10:22:55

问题


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?


回答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:

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



来源:https://stackoverflow.com/questions/46381620/plotting-data-points-onto-matplotlib-basemap-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!