如何在两个图形之间绘制单个颜色条?

发布于 2025-01-09 09:20:04 字数 1820 浏览 0 评论 0原文

我使用Basemapscatter绘制了图形。我想画一个颜色条。 我做了以下操作来绘制一个并不奇怪的图形,但这不是我想要的结果。

def def_basemap(ax):
    m = Basemap(projection="merc", resolution="l", ax=ax,
                lat_0=37.35, lon_0=126.58,
                urcrnrlat=44, llcrnrlat=32, llcrnrlon=121.5, urcrnrlon=132.5)
    m.drawcoastlines()
    m.drawcountries()
    m.drawmapboundary()
    return m


fig, ax = plt.subplots(2,2,figsize=(10,10))
axes_ = ax.ravel().tolist()
base_ = [def_basemap(axes_[i]) for i in range(4)] # create basemap

# lon & lat, values
lon  = np.linspace(121.5, 132.5, 10)
lat  = np.linspace(32, 44, 10)
val1 = np.random.random((10,10))

lons, lats = np.meshgrid(lon, lat)
X, Y = zip(*[base_[i](lons, lats) for i in range(3)])

#-----------------Contourf-----------------#
for i in range(3):
  cbar = axes_[i].contourf(X[i], Y[i], val1)
  cb   = fig.colorbar(cbar, ax=axes_[i])

#-----------------Scatter------------------#
X2, Y2 = base_[-1](lon, lat)
val2 = np.random.random(10)
val2_sorted = sorted(val2)

cols = val2_sorted
size = np.linspace(100, 500, 10) * 2
sbar = axes_[-1].scatter(X2, Y2, s=size, c=cols)
cb2  = fig.colorbar(sbar, ax=axes_[-1])

plt.tight_layout()

图01

我修改如下...

#-----------------Contourf-----------------#
...
cbar_ = []
for i in range(3):
  cbar = axes_[i].contourf(X[i], Y[i], val1)
  cbar_.append(cbar)

cb = fig.colorbar(cbar_[0], ax=axes_[0:2], orientation="horizontal")
...

Figure02

结果,我得到了一个非常奇怪的数字。 我想在四个图形的中间绘制一个颜色条,并同时在散点图中绘制一个颜色条。

我应该怎么办?谢谢。

I drew figures using Basemap and scatter. And I wanted to draw a colorbar.
I did the following to draw a figure that wasn't strange, but it wasn't the result I wanted.

def def_basemap(ax):
    m = Basemap(projection="merc", resolution="l", ax=ax,
                lat_0=37.35, lon_0=126.58,
                urcrnrlat=44, llcrnrlat=32, llcrnrlon=121.5, urcrnrlon=132.5)
    m.drawcoastlines()
    m.drawcountries()
    m.drawmapboundary()
    return m


fig, ax = plt.subplots(2,2,figsize=(10,10))
axes_ = ax.ravel().tolist()
base_ = [def_basemap(axes_[i]) for i in range(4)] # create basemap

# lon & lat, values
lon  = np.linspace(121.5, 132.5, 10)
lat  = np.linspace(32, 44, 10)
val1 = np.random.random((10,10))

lons, lats = np.meshgrid(lon, lat)
X, Y = zip(*[base_[i](lons, lats) for i in range(3)])

#-----------------Contourf-----------------#
for i in range(3):
  cbar = axes_[i].contourf(X[i], Y[i], val1)
  cb   = fig.colorbar(cbar, ax=axes_[i])

#-----------------Scatter------------------#
X2, Y2 = base_[-1](lon, lat)
val2 = np.random.random(10)
val2_sorted = sorted(val2)

cols = val2_sorted
size = np.linspace(100, 500, 10) * 2
sbar = axes_[-1].scatter(X2, Y2, s=size, c=cols)
cb2  = fig.colorbar(sbar, ax=axes_[-1])

plt.tight_layout()

Figure01

I revised it as follows...

#-----------------Contourf-----------------#
...
cbar_ = []
for i in range(3):
  cbar = axes_[i].contourf(X[i], Y[i], val1)
  cbar_.append(cbar)

cb = fig.colorbar(cbar_[0], ax=axes_[0:2], orientation="horizontal")
...

Figure02

As a result, I got a very weird figure.
I want to draw one colorbar in the middle of the four figures, and one colorbar in the scatter figure at the same time.

What should I do? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文