[英]How can I remove space between bars while plotting bar plot using pandas and matplotlib in Python?
[英]How can I remove borders/frames of subplots while plotting xarray dataset using imshow of matplotlib in Python?
我從NASA 網站下載了一個名為 gistemp1200_GHCNv4_ERSSTv5.nc.gz 的 netCDF 文件
我使用包含溫度異常數據的 xarray 打開數據。
import xarray as xr
file = "../data/gistemp1200_GHCNv4_ERSSTv5.nc"
xr_df = xr.open_dataset(file)
xr_df_annual = xr_df.resample(time = "Y").mean()
anomaly = xr_df_annual["tempanomaly"]
它包含相對於特定時間段的年溫度異常值。
我想以子圖的形式繪制四年的年溫度值。 我使用以下代碼:
fig, axs = plt.subplots(2, 2, figsize = (10, 10))
fig.suptitle("Temperature anomaly in the end of last four decades\n relative to 1951-1980")
def get_plot(year, i, j, k):
ax = fig.add_subplot(2, 2, k,
projection = ccrs.PlateCarree())
ax.add_feature(NaturalEarthFeature('cultural', 'admin_0_countries', '10m'),
facecolor='none', edgecolor='black')
ax.set_extent([-150, 150, -55, 85])
xr_df_sub = anomaly.loc[f"{year}-12-31"]
ax.set_title(f"{year}")
ax.set_axis_off()
xr.plot.imshow(xr_df_sub,
ax = ax,
add_labels = False,
vmin = -4, vmax = 4,
cmap = "coolwarm",
add_colorbar = False,
interpolation = "bicubic")
axs[0, 0] = get_plot(1990, 0, 0, 1)
axs[0, 1] = get_plot(2000, 0, 1, 2)
axs[1, 0] = get_plot(2010, 0, 1, 3)
axs[1, 1] = get_plot(2020, 0, 1, 4)
# add colorbar
cax = fig.add_axes([0.92, 0.15, 0.01, 0.7]) #[left, bottom, width, height]
sm = plt.cm.ScalarMappable(cmap='coolwarm',
norm=plt.Normalize(vmin= -4, vmax= 4))
# fake up the array of the scalar mappable.
sm._A = []
lgd=fig.colorbar(sm, cax=cax, extend = "both"
).set_label("°C", rotation=0,y=1.1, labelpad= -35)
plt.show()
我為每個子圖創建了一個名為get_plot(year, i, j, k)
的函數。 i
, j
指的是行號和列號, k
指的是子圖的索引號。 繪制給定year
的溫度異常的函數。
這是所需的情節,但是,我為每個要刪除的子情節獲得了外部黑色框架。 我使用了以下代碼:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
但是,它不會改變任何東西。 我也試過
fig.patch.set_visible(False)
它也沒有改變任何東西。
如何刪除每個子圖的 x 軸和 y 軸上 0 到 1 的外部邊界? 另外,我想獲得有關使子圖彼此靠近的建議?
根據 Dahn 的建議,我發現我使用了plt.subplot()
並再次使用了fig.add_subplot()
兩次。
我解決了如下問題:
fig = plt.figure(figsize = (10, 10))
fig.suptitle("過去四個十年末的溫度異常\n相對於 1951-1980", y = 0.8)
plt.subplots_adjust(底部 = 0.3,頂部 = 0.7,wspace=0.1,hspace=0.25)
def get_plot(year, i, j, k):
ax = fig.add_subplot(2, 2, k,
projection = ccrs.PlateCarree())
ax.add_feature(NaturalEarthFeature('cultural', 'admin_0_countries', '10m'),
facecolor='none', edgecolor='black')
ax.set_extent([-150, 150, -55, 85])
xr_df_sub = anomaly.loc[f"{year}-12-31"]
ax.set_title(f"{year}")
ax.axis("off")
ax = xr.plot.imshow(xr_df_sub,
add_labels = True,
vmin = -4, vmax = 4,
cmap = "coolwarm",
add_colorbar = False,
interpolation = "bicubic",
)
return ax
ax[0, 0] = get_plot(1990, 0, 0, 1)
ax[0, 1] = get_plot(2000, 0, 1, 2)
ax[1, 0] = get_plot(2010, 0, 1, 3)
ax[1, 1] = get_plot(2020, 0, 1, 4)
# add colorbar
cax = fig.add_axes([0.92, 0.3, 0.02, 0.5]) #[left, bottom, width, height]
sm = plt.cm.ScalarMappable(cmap='coolwarm',
norm=plt.Normalize(vmin= -4, vmax= 4))
# fake up the array of the scalar mappable.
sm._A = []
lgd=fig.colorbar(sm, cax=cax, extend = "both"
).set_label("°C", rotation=0,y=1.1, labelpad= -35)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.