matplotlib - geopandas中地图周围的边距问题
问题描述
我似乎无法解决 GeoPandas 地图和添加注释之间的一些边距/位置问题。注释是由函数添加的addChartSignature
,addTitle
它们会导致糟糕的布局。
我已经设法通过 hack 解决了一些垂直边距问题,请参阅评论### to deal with vertical margin issue
,但是我似乎无法处理左侧的边距:我希望地图、签名和标题与左侧对齐非常小的边距(如右侧)。
我添加了变量righthspace
来正确移动注释,但它没有帮助,而且感觉也不对。
下面是总结问题的代码示例
from __future__ import division
import matplotlib.pyplot as plt
import geopandas
from mpl_toolkits.axes_grid1 import make_axes_locatable
def addChartSignature(ax, vspace=0, righthspace=0):
ax.annotate('',
xy=(0.97, 0.05 + vspace),
xycoords='figure fraction',
xytext=(0.03 + righthspace,0.05 + vspace),
textcoords='figure fraction',
arrowprops=dict(arrowstyle="-",
linewidth=0.7,
facecolor='grey',
alpha=.7,
edgecolor='grey'),
horizontalalignment = 'center',
verticalalignment='bottom')
ax.annotate(u" ©myCompany",
xy=(0.5, 0.5),
xycoords='figure fraction',
xytext= (0.03 + righthspace,0.01+vspace),
textcoords='figure fraction',
ha="left",
va="bottom",
color = 'grey',
alpha = .7,
fontsize = 11)
ax.annotate(u"Source: Internal",
xy=(0.5, 0.5),
xycoords='figure fraction',
xytext=(0.97,0.01+vspace),
textcoords='figure fraction',
ha="right",
va="bottom",
color = 'grey',
alpha = .7,
fontsize = 11)
def addTitle(ax, vspace=0, righthspace=0):
ax.annotate("My Chart Title",
xy=(0.5, 0.5),
xycoords='figure fraction',
xytext=(0.01+righthspace, 0.985+vspace),
textcoords='figure fraction',
ha="left",
va="top",
color = 'black',
alpha = .75,
fontsize = 19,
weight = 'bold')
path = geopandas.datasets.get_path('naturalearth_lowres')
world = geopandas.read_file(path)
world = world[(world.pop_est>0) & (world.name!="Antarctica")]
plt.style.use('fivethirtyeight')
fig = plt.figure(figsize=(8, 6))
ax = fig.add_axes([0., 0., 1, 1])
ax = world.plot(color="lightgrey", ax=ax)
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size="3%", pad=-1.3)
world.dropna().plot(
column='pop_est',
ax=ax,
legend=True,
cax=cax,
cmap='RdYlGn',
)
ax.grid(color='#F8F8F8')
ax.set_xticklabels([])
ax.set_yticklabels([])
### to deal with vertical margin issue
ax.set_aspect(aspect=4./3)
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)
addChartSignature(ax, righthspace = 0.05)
addTitle(ax, righthspace = 0.05)
解决方案
推荐阅读
- javascript - 无法使用 jQuery .submit() 获取 $_POST('input')
- html - 一种在移动屏幕上将水平列垂直放置的电子邮件友好方式(没有s 并且没有最小宽度)
- cordova - 无法使用科尔多瓦视频编辑器插件显示生成的缩略图
- node.js - Puppeteer 忽略代理设置
- django - Django根据另一个模型中最后一个数据的字段值过滤对象
- ruby-on-rails - 活动存储 - 添加文件描述/文本 - Ruby on Rails 5.2
- for-loop - v-for 循环中的动态类连接
- c# - asp.net MVC 不显眼的浏览器错误
- javascript - axios post请求参数到后端未定义
- dialogflow-es - 状态码为 401 的响应正文