2016-11-21 13 views
3

위도 경도로 변수를 플롯하기 위해 matplotlib를 사용하고 있습니다. 문제는이 이미지에 축이나 테두리가 포함될 수 없다는 것입니다. 축을 제거 할 수 있었지만 이미지 주위의 흰색 패딩을 완전히 제거해야합니다 (아래 코드의 예제 이미지 참조 : http://imgur.com/a/W0vy9).Matplotlib 분산 형 플롯 - 흰색 패딩 제거

Remove padding from matplotlib plotting

How to remove padding/border in a matplotlib subplot (SOLVED)

Matplotlib plots: removing axis, legends and white spaces

을하지만 아무것도 공백을 제거하는 일을하지 않았다 :

나는이 StackOverflow의 방법론을 포함하여 Google 검색에서 여러 가지 방법을 시도했다. matplotlib을 버리고 다른 플로팅 라이브러리를 시도하는 경우에도 조언이 있으면 감사하겠습니다.

import numpy as np 
import matplotlib 
from mpl_toolkits.basemap import Basemap 
from scipy import stats 

lat = np.random.randint(-60.5, high=60.5, size=257087) 
lon = np.random.randint(-179.95, high=180, size=257087) 
maxnsz = np.random.randint(12, 60, size=257087) 

percRange = np.arange(100,40,-1) 
percStr=percRange.astype(str) 
val_percentile=np.percentile(maxnsz, percRange, interpolation='nearest') 

#Rank all values 
all_percentiles=stats.rankdata(maxnsz)/len(maxnsz) 
#Figure setup 
fig = matplotlib.pyplot.figure(frameon=False, dpi=600) 
#Basemap code can go here 

x=lon 
y=lat 

cmap = matplotlib.cm.get_cmap('cool') 


h=np.where(all_percentiles >= 0.999) 
hl=np.where((all_percentiles < 0.999) & (all_percentiles > 0.90)) 
mh=np.where((all_percentiles > 0.75) & (all_percentiles < 0.90)) 
ml=np.where((all_percentiles >= 0.4) & (all_percentiles < 0.75)) 
l=np.where(all_percentiles < 0.4) 

all_percentiles[h]=0 
all_percentiles[hl]=0.25 
all_percentiles[mh]=0.5 
all_percentiles[ml]=0.75 
all_percentiles[l]=1 

rgba_low=cmap(1) 
rgba_ml=cmap(0.75) 
rgba_mh=cmap(0.51) 
rgba_hl=cmap(0.25) 
rgba_high=cmap(0) 

matplotlib.pyplot.axis('off') 

matplotlib.pyplot.scatter(x[ml],y[ml], c=rgba_ml, s=3, marker=',',edgecolor='none', alpha=0.4) 
matplotlib.pyplot.scatter(x[mh],y[mh], c=rgba_mh, s=3,  marker='o', edgecolor='none', alpha=0.5) 
matplotlib.pyplot.scatter(x[hl],y[hl], c=rgba_hl, s=4, marker='*',edgecolor='none', alpha=0.6) 
matplotlib.pyplot.scatter(x[h],y[h], c=rgba_high, s=5, marker='^', edgecolor='none',alpha=0.75) 

fig.savefig('/home/usr/code/python/testfig.jpg', bbox_inches=0, nbins=0, transparent="True", pad_inches=0.0) 
fig.canvas.draw() 
+0

당신은 [이 어떻게, 최소 완료하고 검증 가능한 예제를 만들] 읽었다 (http://stackoverflow.com/help/mcve)? – ImportanceOfBeingErnest

+0

아니요.하지만 다른 것을 제출하기 전에 확실히 상담하겠습니다. @ImportanceOfBeingErnest 팁과 답변을 주셔서 감사합니다. 나는 많은 것을 배우고 있습니다! – mofs

답변

2

문제는 Matplotlib plots: removing axis, legends and white spaces에 주어진 모든 솔루션이 실제로 imshow 작업을 의미하는 것입니다 :

여기에 내가 그 프로그램이 동작을 사용하고 코드의 기본적인 형태입니다.

그래서, 다음은 분명히

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax=fig.add_axes([0,0,1,1]) 
ax.set_axis_off() 

im = ax.imshow([[2,3,4,1], [2,4,4,2]], origin="lower", extent=[1,4,2,8]) 
ax.plot([1,2,3,4], [2,3,4,8], lw=5) 

ax.set_aspect('auto') 
plt.show() 

을 작동하고

enter image description here

을 생산하지만 여기, 당신은 scatter을 사용하고 있습니다. 산포도를 추가

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax=fig.add_axes([0,0,1,1]) 
ax.set_axis_off() 


im = ax.imshow([[2,3,4,1], [2,4,4,2]], origin="lower", extent=[1,4,2,8]) 
ax.plot([1,2,3,4], [2,3,4,8], lw=5) 

ax.scatter([2,3,4,1], [2,3,4,8], c="r", s=2500) 

ax.set_aspect('auto') 
plt.show() 

enter image description here

Scatter 생산하는 축에 한계가 모든 분산 포인트는되도록 설정하는 것을 의미 기본적으로 모든 점을 볼 수 있도록하는 시도를하기 matplotlib 특수성을 가지고 전체적으로 볼 수 있습니다.

이를 극복하기 위해, 우리는 특히 축 한계를 설정해야합니다 : 우리가 원하는 동작을 얻을 것이다

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax=fig.add_axes([0,0,1,1]) 
ax.set_axis_off() 

im = ax.imshow([[2,3,4,1], [2,4,4,2]], origin="lower", extent=[1,4,2,8]) 
ax.plot([1,2,3,4], [2,3,4,8], lw=5) 

ax.scatter([2,3,4,1], [2,3,4,8], c="r", s=2500) 

ax.set_xlim([1,4]) 
ax.set_ylim([2,8]) 

ax.set_aspect('auto') 
plt.show() 

하도록.

enter image description here

+1

@mofs 여기서도 문제의 최소한의 예를 16 행으로 작성할 수 있음을 알 수 있습니다. – ImportanceOfBeingErnest