[英]How to convert AxesSubplot to Axes object to make subplots_adjust works only on unconverted AxesSubplot
I have created two lists of AxesSubplot
, and I only want to change the bounding box of two set of AxesSubplot
using subplots_adjust()
seperately.我创建了两个
AxesSubplot
列表,我只想AxesSubplot
使用AxesSubplot
subplots_adjust()
更改两组AxesSubplot
的边界框。 I found subplots_adjust()
does not work on Axes
object, which means if I convert one list of AxesSubplot
objects to Axes
, then subplots_adjust()
will work fine for the rest of AxesSubplot
objects.我发现
subplots_adjust()
对Axes
对象不起作用,这意味着如果我将一个AxesSubplot
对象列表转换为Axes
,那么AxesSubplot
subplots_adjust()
将适用于其余的AxesSubplot
对象。 Pls see code below.请参阅下面的代码。
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)
I know I can get bounds
of AxesSubplot
and create Axes
from that, but I am not sure if there are other ways to do it.我知道我可以获得
AxesSubplot
bounds
并AxesSubplot
创建Axes
,但我不确定是否还有其他方法可以做到这一点。
If there exist other ways to achieve such flexibility, pls also suggest:)如果有其他方法可以实现这种灵活性,请也建议:)
This is possible using two different instances of GridSpec
.这可以使用
GridSpec
两个不同实例来GridSpec
。
Each GridSpec
can have its own bounding left, right, bottom, top
defined.每个
GridSpec
都可以定义自己的left, right, bottom, top
边界。
In this example, we create two GridSpec
's with different bounds, then create all the subplots for each GridSpec
using the .subplots
function.在这个例子中,我们创建了两个具有不同边界的
GridSpec
,然后使用.subplots
函数为每个GridSpec
创建所有子图。
Note that I coloured the faces of the subplots on the second using the subplots_kw
option, just to make it clear which is which.请注意,我使用
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.