首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使matplotlib-basemap轴的长度与另一个子图轴相等?

如何使matplotlib-basemap轴的长度与另一个子图轴相等?
EN

Stack Overflow用户
提问于 2020-01-31 02:22:33
回答 1查看 203关注 0票数 1

在这个问题上的任何帮助都是非常感谢的。我在使用底图时遇到了一个子图问题。使用gridspec,我在matplotlib中创建了一个包含三个子图的图。我想把底图的纬度和经度轴的长度和其他两个子图的轴线画得一样。但是在我现在的代码中,两个子图的纬度轴的长度是相同的,而经度轴的长度是不同的。如何使底图和左下象限子图的经轴长度相等?有没有办法做到这一点?

下面是我的代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-31 02:54:02

使用GridSpec并设置figsize=(12,12)会导致子图不能正确对齐。

我发现使用figsize=(6.3,8.12)生成的图与图对齐。

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59992235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档