SereneWizard
SereneWizard

Reputation: 166

mpl_toolkits basemap scatter plot error

I successfully plotted a polygon using mpl_toolkits.basemap.Basemap() function. After that, I tried to add some (x,y) points as scatterplot onto the basemap. It gives a Deprecationwarning message, and doesn't plot the scatter points (even though the earlier shapefile is still plotted). The following is the code chunk (Please understand that the necessary libraries have been loaded):

fname = "../DATA/GIS/IL_State_ln"
m = Basemap(llcrnrlon=-92.,llcrnrlat=36.8,urcrnrlon=-86.5,urcrnrlat=43.,
             resolution='i', projection='tmerc', lat_0 = 36.5, lon_0 = -91.8)
m.readshapefile(fname, 'mygeom')

x = [-90., -91.2, -88.]
y = [38., 37.7, 42.]
m.plot(x,y)

plt.show()

The deprecationwarning message is:

/home/serenewiz/miniconda3/envs/onering/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py:3260: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
  b = ax.ishold()
/home/serenewiz/miniconda3/envs/onering/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py:3269: MatplotlibDeprecationWarning: axes.hold is deprecated.
    See the API Changes document (http://matplotlib.org/api/api_changes.html)
    for more details.
  ax.hold(b)

Just for your information, the versions are: Python(3.5), matplotlib (2.0.0) and basemap(1.0.7).

I referred to the following link, before posting here, but still I could not solve the problem: https://github.com/matplotlib/matplotlib/issues/7808

Upvotes: 4

Views: 2165

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

There are two problems here:

  1. There is a depreciation warning
  2. The points won't show up.

Both problems are completely unrelated.

1. Depreciation warning

A depreciation warning is not an error. It simply tells us that some command or function should not be used (any more). In this case, the depreciated command is a command from matplotlib that basemap uses internally. So it's not actually under our control, but resides in the basemap code. This command (ax.hold) has been depreciated as of matplotlib version 2.x. So there is nothing we can do about it, until there is a new version of basemap out, which would not use it anymore. As seen from this issue there seems to be someone working on it at least.
But as this is not an error, the only drawback as of now is that annoying warning message. The functionality of basemap is not at all deteriorated.

2. Show points on plot

To draw points on a basemap plot the coordinates need to be transformed using the Basemap instance. I.e. drawing the points lon = 80, lat=34 on a basemap plot m = Basemap(...), they need to be transformed using x,y = m([lon],[lat]) before being plotted with m.plot(x,y, marker="o"). Note that in order to draw points, you need to supply the marker argument to plot.

Here is a complete example.

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

m = Basemap(llcrnrlon=-92.,llcrnrlat=36.8,urcrnrlon=-86.5,urcrnrlat=43.,
             resolution='i', projection='tmerc', lat_0 = 36.5, lon_0 = -91.8)

m.drawcoastlines()
m.drawcounties(zorder=0, color='gray')

x,y = m([-90., -91.2, -88.],[38., 37.7, 42.])

m.plot(x,y, marker="o")

plt.show()

enter image description here

Upvotes: 4

Related Questions