如何在两个图形之间绘制单个颜色条?
我使用Basemap
和scatter
绘制了图形。我想画一个颜色条。 我做了以下操作来绘制一个并不奇怪的图形,但这不是我想要的结果。
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()
我修改如下...
#-----------------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")
...
结果,我得到了一个非常奇怪的数字。 我想在四个图形的中间绘制一个颜色条,并同时在散点图中绘制一个颜色条。
我应该怎么办?谢谢。
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()
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")
...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论