HeL*_*889 1 python plot matplotlib removing-whitespace geopandas
我正在寻找一种使用 matplotlib 进行无缝地图图像绘制的解决方案。当前的代码运行良好且稳定,但是,它在左侧和底部留下了空白。我想删除这个空白,但不知道如何。
我的示例代码:
import geopandas
from seaborn import despine
from pandas import read_csv
import matplotlib.pyplot as plt
# read data and shapefile
geo_path = 'shapefiles/ne_10m_admin_0_countries.shp'
df = read_csv('UNpopEstimates2100.csv')
world = geopandas.read_file(geo_path)
# specifiy what is to be plotted
cm = 'Greys'
world['2015'] = df['2015']
# set plot environment
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
world.plot(ax=ax, column='2015', cmap=cm, scheme='quantiles')
plt.savefig('sample.png', bbox_inches='tight', tight_layout=True, pad_inches=0, frameon=None)
Run Code Online (Sandbox Code Playgroud)
我遵循了Matplotlib's Tight Layout Guide、machinelearningplus.com、Removingwhite padding from graph on Reddit以及其他几个 stackoverflow 帖子中的教程,即
和
matplotlib绘图仅用数据点填充图形,没有边框,标签,轴,
我缺少什么?
编辑 - 提供具有非现实数据的可重现版本,但问题保持不变 - 如何消除绘图周围的空白?
我是 Geopandas 的新手,所以我不确定如何重新创建地理数据框,但是,它有内置的数据集。
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world['2015'] = np.random.uniform(low=1., high=100., size=(177,))
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
world.plot(ax=ax, column='2015', scheme='quantiles')
plt.savefig('sample.png')
Run Code Online (Sandbox Code Playgroud)
首先,使用不同的 geopandas 版本时存在差异。人们可能应该确保至少使用 geopandas 0.4 以使地图具有正确的宽高比。
下一步需要删除轴内的填充。这可以使用命令来完成ax.margins(0)。
现在,这将导致一个方向上出现一些空白(在本例中为顶部和底部)。一种选择是将图形缩小到轴的范围。
import numpy as np
import matplotlib; print(matplotlib.__version__)
import matplotlib.pyplot as plt
import geopandas; print(geopandas.__version__)
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world['2015'] = np.random.uniform(low=1., high=100., size=(177,))
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
world.plot(ax=ax, column='2015', scheme='quantiles')
ax.margins(0)
ax.apply_aspect()
bbox = ax.get_window_extent().inverse_transformed(fig.transFigure)
w,h = fig.get_size_inches()
fig.set_size_inches(w*bbox.width, h*bbox.height)
plt.savefig('sample.png')
plt.show()
Run Code Online (Sandbox Code Playgroud)
这样做的好处是图形的物理尺寸确实适合轴;因此无论是显示在屏幕上还是保存为图像,结果都是相同的。
相反,如果目标只是保存没有空格的图形,您可以使用参数bbox_inches并savefig提供轴的实际范围(以英寸为单位)。
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
world.plot(ax=ax, column='2015', scheme='quantiles')
ax.margins(0)
ax.apply_aspect()
bbox = ax.get_window_extent().inverse_transformed(fig.dpi_scale_trans)
plt.savefig('sample.png', bbox_inches=bbox)
Run Code Online (Sandbox Code Playgroud)
最后,可以使用 自动化上述操作bbox_inches='tight'。但是,为了使该'tight'选项正常工作,需要确保轴周围没有刻度线和标签,否则会增加间距。
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
world.plot(ax=ax, column='2015', scheme='quantiles')
ax.margins(0)
ax.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
plt.savefig('sample.png', bbox_inches="tight", pad_inches=0)
Run Code Online (Sandbox Code Playgroud)
在上述所有三种情况下,结果数字将是
| 归档时间: |
|
| 查看次数: |
5932 次 |
| 最近记录: |