2017-03-10 4 views
2

다음은베이스 맵을 사용하여 Geogtiff 파일의 데이터를 플롯하는 스크립트입니다. 데이터는 범주 적이며이 도메인에는 13 개의 범주가 있습니다. 문제는 일부 범주가 하나의 색으로 묶여 있으므로 일부 해상도가 손실된다는 것입니다.
불행히도이 문제를 해결하는 방법을 모르겠습니다. 나는 plt.cm.get_cmp이 이산 데이터 세트를 위해 더 낫다는 것을 읽었지만 불행히도 그것을 얻지는 못했다. 여기에 어떤 도움을 너무 많이컬러 맵이 데이터를 올바르게 분류하지 않습니다.

np.unique(data_new) 

array([ 0, 1, 4, 5, 7, 10, 11, 12, 13, 14, 15, 16, 17], dtype=uint8) 

감사 : 여기

gtif = 'some_dir' 

ds = gdal.Open(gtif) 
data = ds.ReadAsArray() 
gt = ds.GetGeoTransform() 
proj = ds.GetProjection() 
xres = gt[1] 
yres = gt[5] 

xmin = gt[0] + xres 
xmax = gt[0] + (xres * ds.RasterXSize) - xres 
ymin = gt[3] + (yres * ds.RasterYSize) + yres 
ymax = gt[3] - yres 
xy_source = np.mgrid[xmin:xmax+xres:xres, ymax+yres:ymin:yres] 
ds = None 

fig2 = plt.figure(figsize=[12, 11]) 
ax2 = fig2.add_subplot(111) 
ax2.set_title("Land use plot") 
bm2 = Basemap(ax=ax2,projection='cyl',llcrnrlat=ymin,urcrnrlat=ymax,llcrnrlon=xmin,urcrnrlon=xmax,resolution='l') 
bm2.drawcoastlines(linewidth=0.2) 
bm2.drawcountries(linewidth=0.2) 

data_new=np.copy(data) 
data_new[data_new==255] = 0 

nbins = np.unique(data_new).size 
cb =plt.cm.get_cmap('jet', nbins+1) 
img2 =bm2.imshow(np.flipud(data_new), cmap=cb) 
ax2.set_xlim(3, 6) 
ax2.set_ylim(50,53) 
plt.show() 

labels = [str(i) for i in np.unique(data_new)] 
cb2=bm2.colorbar(img2, "right", size="5%", pad='3%', label='NOAH Land Use Category') 
cb2.set_ticklabels(labels) 
cb2.set_ticks(np.unique(data_new)) 

는 도메인 (번호 클래스) 내에서 발견되는 종류이다. 불일치를 보여주는 출력 이미지도 첨부했습니다. (작동하지 않음)

답변

1

먼저이 색상 맵 문제는 기본 맵의 사용과 관계가 없습니다. 따라서 다음은 Matplotlib 플롯에 적용 할 수 있습니다.

여기서 문제는 n 값에서 색상 맵을 생성하면 해당 값이 색상 맵 범위에 균등하게 분배된다는 것입니다. 따라서 이미지의 일부 값은 색상 표 내에서 동일한 색상 범위로 떨어집니다.

이렇게하려면 다음과 같이 초기 범주 수의 색상 맵을 생성 할 수 있습니다.


enter image description here

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.colors 

# generate some data 
data = np.array([ 0, 1, 4, 5, 7, 10]*8) 
np.random.shuffle(data) 
data = data.reshape((8,6)) 

# generate colormap and norm 
unique = np.unique(data) 
vals = np.arange(int(unique.max()+1))/float(unique.max()) 
cols = plt.cm.jet(vals) 
cmap = matplotlib.colors.ListedColormap(cols, int(unique.max())+1) 
norm=matplotlib.colors.Normalize(vmin=-0.5, vmax=unique.max()+0.5) 


fig, ax = plt.subplots(figsize=(5,5)) 
im = ax.imshow(data, cmap=cmap, norm=norm) 
for i in range(data.shape[0]): 
    for j in range(data.shape[1]): 
     ax.text(j,i,data[i,j], color="w", ha="center", va="center") 

cb = fig.colorbar(im, ax=ax, norm=norm) 
cb.set_ticks(unique) 

plt.show() 
다음과 같이 이미지에없는 색상을 제외하도록 확장 할 수 있습니다

enter image description here

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.colors 

# generate some data 
data = np.array([ 0, 1, 4, 5, 7, 10]*8) 
np.random.shuffle(data) 
data = data.reshape((8,6)) 

unique, newdata = np.unique(data, return_inverse=1) 
newdata = newdata.reshape(data.shape) 

# generate colormap and norm 
new_unique = np.unique(newdata) 
vals = np.arange(int(new_unique.max()+1))/float(new_unique.max()) 
cols = plt.cm.jet(vals) 
cmap = matplotlib.colors.ListedColormap(cols, int(new_unique.max())+1) 
norm=matplotlib.colors.Normalize(vmin=-0.5, vmax=new_unique.max()+0.5) 

fig, ax = plt.subplots(figsize=(5,5)) 
im = ax.imshow(newdata, cmap=cmap, norm=norm) 
for i in range(newdata.shape[0]): 
    for j in range(newdata.shape[1]): 
     ax.text(j,i,data[i,j], color="w", ha="center", va="center") 

cb = fig.colorbar(im, ax=ax, norm=norm) 
cb.ax.set_yticklabels(unique) 

plt.show() 
+0

덕분에 빠른 답변에 대한 너무 많은! 이것은 작동하지만 유일한 문제는 중첩 된 for 루프가 내가 가지고있는 매우 큰 데이터 세트 (수천 개의 점)에 대해 실제로 느려질 것이라는 것입니다. 매우 큰 데이터 세트에 대해이를 최적화하는 방법이 있습니까? – user2013373

+0

루프를 사용하지 않고보다 효율적인 버전에 대한 대답을 편집했습니다. – ImportanceOfBeingErnest

+0

감사! 루프를 피하기 위해 노력했던 것과 다소 비슷합니다. 멋진 한 주를 보내십시오! – user2013373

관련 문제