簡體   English   中英

如何將模型中帶有顏色的數據繪制到matplotlib底圖上? 為什么沒有顯示數據?

[英]How do I plot data with color from my model onto matplotlib Basemap? Why is data not showing up?

我試圖從我的模型的底圖上繪制數據輸出。 我可以看到地圖,但看不到要點。 有什么問題 我正在使用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. x.datay.data是轉換后的坐標。

  2. c = np.random.randint(1, 500, size=len(lats))是映射到顏色圖中與每個點相對應的顏色的值。

您可能不需要的一些部分是:

 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答案的直接應用,可以使顏色條的大小與圖相匹配。 您選擇保留還是不保留。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM