如何使用我的模型将颜色数据绘制到matplotlib底图上?为什么数据没有出现?

时间:2017-06-27 05:11:48

标签: python matplotlib scikit-learn matplotlib-basemap

我正在尝试从我在Basemap上绘制的模型中获取数据。我可以看到地图,但不是点。有什么问题?我正在使用python 2.7的最新版scikit-learn和basemap。我有以下代码:

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()

1 个答案:

答案 0 :(得分:0)

此示例已从official example修改。在散点图中使用色彩映射的关键部分是:

  1. x.datay.data是转换后的坐标。

  2. c = np.random.randint(1, 500, size=len(lats))是映射到色图中与每个点对应的颜色的值。

  3. 您可能不需要的部分内容是:

    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)
    
         

    此部分用于生成数据样本。您可能希望将其替换为真实数据。

    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))
    
         

    这是bogatron's answer的直接应用,以使colorbar的大小与绘图匹配。这是你保留与否的选择。

    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