2016-11-21 180 views
3

我正在用matplotlib绘制纬度经度坐标中的变量。问题是这个图像不能包含坐标轴或边框。我已经能够移除轴,但是我的图像周围的白色填充必须完全移除(请参阅以下代码中的示例图像:http://imgur.com/a/W0vy9)。Matplotlib散点图 - 删除白色填充

我试图从谷歌搜索的几种方法,包括这些StackOverflow的方法:

Remove padding from matplotlib plotting

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

Matplotlib plots: removing axis, legends and white spaces

,但没有去除空白工作过。如果你有任何建议(即使是沟渠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