简体   繁体   English

如何将 AxesSubplot 转换为 Axes 对象以使 subplots_adjust 仅适用于未转换的 AxesSubplot

[英]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 boundsAxesSubplot创建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.

相关问题 Pandas数据帧错误:matplotlib.axes._subplots.AxesSubplot - Pandas dataframe error: matplotlib.axes._subplots.AxesSubplot Seaborn的“ pairplot”函数以错误结束:““ AxesSubplot”对象没有属性“ _make_twin_axes”” - Seaborn 'pairplot' function ends with an error “ 'AxesSubplot' object has no attribute '_make_twin_axes' ” 如何将现有的2D AxesSubplot对象转换/升级为Axes3DSubplot对象? - How can an existing 2D AxesSubplot object be converted/upgraded to an Axes3DSubplot object? TypeError:尝试创建2个子图时,'AxesSubplot'对象不可迭代 - TypeError: 'AxesSubplot' object is not iterable when trying to create 2 subplots 合并子图错误-'AxesSubplot'对象没有属性'get_gridspec' - Merging subplots error - 'AxesSubplot' object has no attribute 'get_gridspec' "matplotlib:AttributeError:“AxesSubplot”对象没有属性“add_axes”" - matplotlib: AttributeError: 'AxesSubplot' object has no attribute 'add_axes' 如何删除AxesSubplot对象上的多余图? - How to delete extra plots on a AxesSubplot object? 如何设置AxesSubPlot的宽度? - How to set the width of an AxesSubPlot? 如何在Python中显示AxesSubplot? - How to show an AxesSubplot in Python? 如何从 matplotllib 中的 AxesSubplot object 获取 BarContainer Object? - How to get a BarContainer Object from an AxesSubplot object in matplotllib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM