0

I am trying to get my data output from my model plotted on Basemap. I can see the map, but not the points. What is the issue? I am using the latest version of scikit-learn and basemap for python 2.7. I have the following code:

dataframe = pd.read_csv('powerplants.csv') # CSV file that contains latitude column, longitude column, and id for the powerplant
colormap = np.arange(500)
labels = modeloutput.labels_ # assume this is output of my model

fig = plt.figure()
ax = fig.add_subplot(111, axisbg='w', frame_on=True)
fig.set_size_inches(18.5, 10.5)
map_points = [...] # assume this is a list populated with coordinates from the csv
# Plot the clusters on the map
# m is a basemap object
m.scatter(
         [geom.x for geom in map_points],
         [geom.y for geom in map_points],
         20, marker='o', lw=.25,
         c = colormap(labels.astype(float)),
         alpha =0.9, antialiased=True,
         zorder=3)
m.fillcontinents(color='#555555')
plt.show()
Rolando
  • 58,640
  • 98
  • 266
  • 407
  • Don't really know what you data looks like. What does `colormap(labels.astype(float))` mean? A callable ndarray? Have you got any error message? Is that possible you can provide a more complete and verifiable example? – Y. Luo Jun 27 '17 at 05:46
  • I am not sure, it is output of a kmeans model. The data is just a csv file with latitude longitudes. Without astype(float) it doesn't do anything either. No error messages displayed, but nothing plotting either except for the background basemap itself. – Rolando Jun 27 '17 at 16:25
  • My suspect is the problem comes from `colormap(labels.astype(float))`, where `colormap` is a `ndarray` and might not be callable. I would say seeing your data or have a relevant sample is helpful in solving your problem. Just realize I've answered another question from you yesterday. Sorry that these couldn't solve your problem. I tried my best and made an example below which might be close to your example. Hopefully that helps a little. – Y. Luo Jun 27 '17 at 18:58

1 Answers1

0

This example is modified from the official example. Key parts for using colormap in scatter plot are:

  1. x.data and y.data are transformed coordinates.

  2. c = np.random.randint(1, 500, size=len(lats)) are values mapping to a color in the colormap corresponding to each point.

Some parts which might be not necessary for you are:

import urllib, os
from netCDF4 import Dataset
import numpy as np

filename, _ = urllib.urlretrieve('http://coastwatch.pfeg.noaa.gov/erddap/tabledap/apdrcArgoAll.nc?longitude,latitude,time&longitude>=0&longitude<=360&latitude>=-90&latitude<=90&time>=2010-01-01&time<=2010-01-08&distinct()')
dset = Dataset(filename)
lats = dset.variables['latitude'][:]
lons = dset.variables['longitude'][:]
dset.close()
os.remove(filename)
c = np.random.randint(1, 500, size=len(lats))

x, y = m(lons,lats)

This part is used for generating an data sample. You might want to replace it with your real data.

from mpl_toolkits.axes_grid1 import make_axes_locatable

divider = make_axes_locatable(ax)
fig.colorbar(pc, cax=divider.append_axes("right", size="5%", pad=0.05))

This is a direct application of bogatron's answer to make the size of colorbar matches the plot. It's your choice of keeping it or not.

import urllib, os

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
import numpy as np

# data downloaded from the form at
# http://coastwatch.pfeg.noaa.gov/erddap/tabledap/apdrcArgoAll.html
filename, _ = urllib.urlretrieve('http://coastwatch.pfeg.noaa.gov/erddap/tabledap/apdrcArgoAll.nc?longitude,latitude,time&longitude>=0&longitude<=360&latitude>=-90&latitude<=90&time>=2010-01-01&time<=2010-01-08&distinct()')
dset = Dataset(filename)
lats = dset.variables['latitude'][:]
lons = dset.variables['longitude'][:]
dset.close()
os.remove(filename)
c = np.random.randint(1, 500, size=len(lats))

# draw map with markers for float locations
fig = plt.figure()
ax = fig.add_subplot(111, axisbg='w', frame_on=True)
fig.set_size_inches(18.5, 10.5)
m = Basemap(lon_0=180, ax=ax)
x, y = m(lons,lats)
pc = m.scatter(x.data, y.data, 20, marker='o', c=c, lw=.25, alpha =0.9, antialiased=True, zorder=3, cmap='summer')
m.fillcontinents(color='#555555')
divider = make_axes_locatable(ax)
fig.colorbar(pc, cax=divider.append_axes("right", size="5%", pad=0.05))
plt.show()

enter image description here

Y. Luo
  • 5,622
  • 1
  • 18
  • 25