Python Matplotlib散点图-删除白色填充
我正在使用matplotlib以经纬度坐标绘制变量。问题是此图像不能包含轴或边框。我已经能够删除axis,但必须完全删除图像周围的白色填充(请参见下面代码中的示例图像:) 我尝试过谷歌搜索的几种方法,包括以下StackOverflow方法: 但在消除空白方面没有任何效果。如果您有任何建议(即使是放弃matplotlib并尝试另一个绘图库),我将不胜感激 下面是我正在使用的代码的基本形式,它显示了这种行为:Python Matplotlib散点图-删除白色填充,python,matplotlib,data-visualization,scatter-plot,Python,Matplotlib,Data Visualization,Scatter Plot,我正在使用matplotlib以经纬度坐标绘制变量。问题是此图像不能包含轴或边框。我已经能够删除axis,但必须完全删除图像周围的白色填充(请参见下面代码中的示例图像:) 我尝试过谷歌搜索的几种方法,包括以下StackOverflow方法: 但在消除空白方面没有任何效果。如果您有任何建议(即使是放弃matplotlib并尝试另一个绘图库),我将不胜感激 下面是我正在使用的代码的基本形式,它显示了这种行为: import numpy as np import matplotlib from
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()
将numpy导入为np
导入matplotlib
从mpl_toolkits.basemap导入basemap
从scipy导入统计信息
lat=np.random.randint(-60.5,高=60.5,大小=257087)
lon=np.random.randint(-179.95,高=180,大小=257087)
maxnsz=np.random.randint(12,60,size=257087)
percRange=np.arange(100,40,-1)
percStr=percsrange.astype(str)
val_百分位数=np.百分位数(maxnsz,percRange,插值='nearest')
#对所有价值进行排名
所有百分位数=统计数据rankdata(maxnsz)/len(maxnsz)
#图形设置
fig=matplotlib.pyplot.figure(frameon=False,dpi=600)
#底图代码可以放在这里
x=lon
y=纬度
cmap=matplotlib.cm.get\u cmap('cool')
h=np.其中(所有百分位数>=0.999)
hl=np,其中((所有百分位<0.999)和(所有百分位>0.90))
mh=np.式中((所有百分位数>0.75)和(所有百分位数<0.90))
ml=np,其中((所有百分位>=0.4)和(所有百分位<0.75))
l=np,其中(所有百分位<0.4)
所有百分位数[h]=0
所有百分位数[hl]=0.25
所有百分位数[mh]=0.5
所有百分位数[ml]=0.75
所有百分位数[l]=1
rgba_低=cmap(1)
rgba_ml=cmap(0.75)
rgba_mh=cmap(0.51)
rgba_hl=cmap(0.25)
rgba_高=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_高,s=5,marker='^',edgecolor='none',alpha=0.75)
fig.savefig('/home/usr/code/python/testfig.jpg',bbox_英寸=0,nbins=0,transparent=“True”,pad_英寸=0.0)
图canvas.draw()
问题在于,在中给出的所有解决方案实际上都是用于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()
生产
但在这里,您使用的是分散。添加散点图
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()
产生
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()
这样我们就能得到想要的行为
你读了吗?没有,但在提交任何其他内容之前,我肯定会参考一下@非常感谢你的提示和答案,我学到了很多@在这里,您还可以看到,您的问题的最小示例可以用16行代码编写。