我正在尝试创建一个具有6个子图的图形,所有子图都具有相同的颜色条。我能够将所有内容绘制在一起,但是python自动为每个绘图添加了一个颜色栏。因此,当我尝试创建并添加跨图形长度的另一个颜色条时,它仍然具有所有6个原始颜色条以及新创建的颜色条。如何创建跨所有6个图/整幅图形的长度的颜色条,并仍删除/删除每个子图的单个颜色条?
这是我的代码和示例输出(这两个中间图目前应该是空的):
fig, axs = plot.subplots(ncols=2, nrows=3, axwidth=5, proj='pcarree')
ax1, ax2, ax3, ax4, ax5, ax6 = axs
axs.format(suptitle=('MAM S06 Change in (m/s)'),
coast = True,latlim = (20,50), lonlim = (235,293), innerborders = True)
S06_mam_seas_1.plot.contourf(ax=ax1, levels = levels)
S06_mam_seas_2.plot.contourf(ax=ax2, levels = levels)
S06_mam_seas_3.plot.contourf(ax=ax3, levels = levels)
S06_mam_seas_4.plot.contourf(ax=ax4, levels = levels)
S06_mam_seas_5.plot.contourf(ax=ax5, levels = levels)
S06_mam_seas_6.plot.contourf(ax=ax6, levels = levels)
ax1.set_title(str(Diff1) + ' - ' + str(BASE))
ax2.set_title(str(Diff2) + ' - ' + str(BASE))
ax3.set_title(str(Diff3) + ' - ' + str(BASE))
ax4.set_title(str(Diff4) + ' - ' + str(BASE))
ax5.set_title(str(Diff5) + ' - ' + str(BASE))
ax6.set_title(str(Diff6) + ' - ' + str(BASE))
我的图像输出
谢谢!
答案 0 :(得分:0)
对我来说,这看起来像xarray的faceting功能,是不错的选择。 首先,您必须沿着新维度(即时间段)将数据数组连接起来。
import numpy as np
import pandas as pd
import xarray as xr
# Create a dummy representation of your data
S06_mam_seas_1 = xr.DataArray(
name="s06_change", dims=("lat", "lon"), data=np.random.rand(100, 100)
)
S06_mam_seas_2 = xr.DataArray(
name="s06_change", dims=("lat", "lon"), data=np.random.rand(100, 100)
)
S06_mam_seas_3 = xr.DataArray(
name="s06_change", dims=("lat", "lon"), data=np.random.rand(100, 100)
)
S06_mam_seas_4 = xr.DataArray(
name="s06_change", dims=("lat", "lon"), data=np.random.rand(100, 100)
)
S06_mam_seas_5 = xr.DataArray(
name="s06_change", dims=("lat", "lon"), data=np.random.rand(100, 100)
)
S06_mam_seas_6 = xr.DataArray(
name="s06_change", dims=("lat", "lon"), data=np.random.rand(100, 100)
)
# Concatenate the dataarrays along a new dimension
start_years = [1881, 1911, 1941, 2021, 2041, 2071]
end_years = [1910, 1940, 1970, 2050, 2070, 2100]
period = [f"{start} – {end}" for start, end in zip(start_years, end_years)]
S06_mam_seas = xr.concat(
[
S06_mam_seas_1,
S06_mam_seas_2,
S06_mam_seas_3,
S06_mam_seas_4,
S06_mam_seas_5,
S06_mam_seas_6,
],
dim=pd.Index(period, name="time_period"),
)
# Set some metadata
S06_mam_seas.attrs["long_name"] = "MAM S06 Change"
S06_mam_seas.attrs["units"] = "m/s"
然后,绘图变得非常简单:
levels = np.arange(0, 1, 0.1)
g = S06_mam_seas.plot.contourf(col="time_period", col_wrap=2, levels=levels)
g.set_titles(template="({value}) - (1971 – 2000)")
我对绘制地图没有经验,因此您需要对设置投影等的代码进行一些调整。但是在xarray documentation中,有一个在地图上进行刻面的示例。