簡體   English   中英

如何將 AxesSubplot 轉換為 Axes 對象以使 subplots_adjust 僅適用於未轉換的 AxesSubplot

[英]How to convert AxesSubplot to Axes object to make subplots_adjust works only on unconverted AxesSubplot

我創建了兩個AxesSubplot列表,我只想AxesSubplot使用AxesSubplot subplots_adjust()更改兩組AxesSubplot的邊界框。 我發現subplots_adjust()Axes對象不起作用,這意味着如果我將一個AxesSubplot對象列表轉換為Axes ,那么AxesSubplot subplots_adjust()將適用於其余的AxesSubplot對象。 請參閱下面的代碼。

fig, axes1 = plt.subplots(2, 2, figsize=(7.2, 7.2))
fig.subplots_adjust(0.2, 0.2 , 0.5, 0.5) # adjust axes1
axes2 = fig.subplots(3, 3)
# only want to adjust axes2, but axes1 affected. 
# If elements in axes1 change from `AxesSubplot` to `Axes`, 
# then it will be fine, fig.subplots_adjust() only works on `AxesSubplot`.
fig.subplots_adjust(0.6, 0.6 , 0.8, 0.8) 

我知道我可以獲得AxesSubplot boundsAxesSubplot創建Axes ,但我不確定是否還有其他方法可以做到這一點。

如果有其他方法可以實現這種靈活性,請也建議:)

這可以使用GridSpec兩個不同實例來GridSpec

每個GridSpec都可以定義自己的left, right, bottom, top邊界。

在這個例子中,我們創建了兩個具有不同邊界的GridSpec ,然后使用.subplots函數為每個GridSpec創建所有子圖。

請注意,我使用subplots_kw選項為第二個子圖的面着色,只是為了明確哪個是哪個。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(7.2, 7.2))

gs1 = fig.add_gridspec(nrows=2, ncols=2, left=0.1, right=0.6, bottom=0.55, top=0.95)
gs2 = fig.add_gridspec(nrows=3, ncols=3, left=0.4, right=0.9, bottom=0.05, top=0.45)

gs1.subplots()
gs2.subplots(subplot_kw={'facecolor': 'red'})

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM