python - 如何使 matplotlib-basemap 轴的长度与另一个子图轴相等?
问题描述
对此问题的任何帮助表示赞赏。我在使用底图时遇到子图问题。使用 gridspec,我在 matplotlib 中创建了一个包含三个子图的图形。我想绘制底图的纬度和经度轴长度以及其他 2 个子图的轴相同。但是在我当前的代码中,虽然 2 个子图的纬度轴长度相同,但经度轴的长度不同。如何使底图和左下象限子图的经度轴长度相等?有没有办法做到这一点?
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from mpl_toolkits.basemap import Basemap
fig = plt.figure(figsize=(12,12))
gs = GridSpec(2, 2, width_ratios=[3, 1], height_ratios=[3, 1], hspace=0.1, wspace=0.1)
ax1 = fig.add_subplot(gs[0])
map1 = Basemap(projection = 'merc', resolution = 'l',
lat_0 = 39.65, lon_0 = 27.5, area_thresh = 0.1,
llcrnrlon = 24.75, llcrnrlat = 35.25, urcrnrlon = 31.0, urcrnrlat = 41.5, ax=ax1)
map1.fillcontinents(color = 'darkgrey', lake_color = 'lightskyblue')
map1.drawmapboundary(fill_color = 'lightskyblue')
map1.drawcoastlines(linewidth = 1.0, color = 'black')
map1.drawcountries(linewidth = 0.5, color = 'black')
ax2 = fig.add_subplot(gs[1])
ax2.set_ylim(35.25, 41.5)
ax3 = fig.add_subplot(gs[2])
ax3.set_xlim(24.75, 31)
for i, ax in enumerate(fig.axes):
ax.tick_params(labelbottom=False, labelleft=False)
ax.set_xlabel("")
ax.set_ylabel("")
ax1.set_xlabel("Longitude (°)", labelpad=10)
ax1.set_ylabel("Latitude (°)", labelpad=10)
ax2.set_xlabel("Depth (km)", rotation=180, labelpad=10)
ax2.set_ylabel("Latitude (°)", labelpad=10)
ax3.set_xlabel("Longitude (°)", labelpad=10)
ax3.set_ylabel("Depth (km)", labelpad=10)
解决方案
使用 GridSpec 和设置figsize=(12,12)
会导致子图未正确对齐。
我发现使用figsize=(6.3,8.12)
产生了一个情节对齐的情节。
fig = plt.figure(figsize = (6.3, 8.12))
gs = GridSpec(2, 2, width_ratios=[3, 1], height_ratios=[3, 1], hspace=0.2, wspace=0.2)
ax1 = fig.add_subplot(gs[0])
map1 = Basemap(projection = 'merc', resolution = 'l',
lat_0 = 39.65, lon_0 = 27.5, area_thresh = 0.1,
llcrnrlon = 24.75, llcrnrlat = 35.25, urcrnrlon = 31.0, urcrnrlat = 41.5, ax=ax1)
map1.fillcontinents(color = 'darkgrey', lake_color = 'lightskyblue')
map1.drawmapboundary(fill_color = 'lightskyblue')
map1.drawcoastlines(linewidth = 1.0, color = 'black')
map1.drawcountries(linewidth = 0.5, color = 'black')
ax2 = fig.add_subplot(gs[1])
ax2.set_ylim(35.25, 41.5)
ax3 = fig.add_subplot(gs[2])
ax3.set_xlim(24.75, 31)
for i, ax in enumerate(fig.axes):
ax.tick_params(labelbottom=False, labelleft=False)
ax.set_xlabel("")
ax.set_ylabel("")
ax1.set_xlabel("Longitude (°)", labelpad=10)
ax1.set_ylabel("Latitude (°)", labelpad=10)
ax2.set_xlabel("Depth (km)", rotation=180, labelpad=10)
ax2.set_ylabel("Latitude (°)", labelpad=10)
ax3.set_xlabel("Longitude (°)", labelpad=10)
ax3.set_ylabel("Depth (km)", labelpad=10)
推荐阅读
- spring - 为什么spring ResourceUtils getFile无法读取jar存档中的文件?
- php - 对象序列化的 PHP 8 会话问题
- c - VIDIOC_S_FMT 给出错误但类型正确
- express - 在每个请求之前更改 set-cookie 标头
- azerothcore - Azerothcore - 错误 1045 (28000): 用户 'acore'@'localhost' 的访问被拒绝(使用密码:是)
- python-3.x - 如何从 Python 中的“dict”对象列表中基于键提取值
- html - 发送表单向导保存并转到上一步
- postgresql - 在 postgresql 中自动调整列统计目标
- json - Firebase中集合之间的关系
- reactjs - ChartJS react-chart-js2 选项不起作用