![](/img/trans.png)
[英]Pandas dataframe error: matplotlib.axes._subplots.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
bounds
並AxesSubplot
創建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.