2016-11-21 9 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