ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-Matplotlib散点图-删除白色填充

2019-11-12 02:56:16  阅读:511  来源: 互联网

标签:scatter-plot matplotlib data-visualization python


我正在使用matplotlib在纬度经度坐标中绘制变量.问题在于该图像不能包含轴或边界.我已经能够移除轴,但是必须完全移除图像周围的白色填充(请参见下面的代码示例图像:http://imgur.com/a/W0vy9).

我已经尝试了Google搜索提供的几种方法,包括以下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()

解决方法:

问题在于,在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

但是在这里,您正在使用分散.添加散点图

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

散点的特殊之处在于,默认情况下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

标签:scatter-plot,matplotlib,data-visualization,python
来源: https://codeday.me/bug/20191112/2024108.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有