大佬教程收集整理的这篇文章主要介绍了将 subplot2grid 与“AxesSubplot”对象一起使用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 subplot2grID 来定义图网格,如下所示。 效果很好,这是一个很好的功能。
plot_axes_1 = plt.subplot2grID((6,4),(0,0),rowspan=2,colspan=3) ##1
plot_axes_2 = plt.subplot2grID((6,(2,colspan=3,sharex=scatter_axes_1) ##2
x_hist_axes_2 = plt.subplot2grID((6,(4,sharex=scatter_axes_2) ##3
y_hist_axes_1 = plt.subplot2grID((6,3),sharey=scatter_axes_1) ##4
y_hist_axes_2 = plt.subplot2grID((6,sharey=scatter_axes_2,sharex= y_hist_axes_1) ##5
现在我想将图像中的 5 个绘图视为一个单元,并绘制它的 6 个副本,排列在 3 行 2 列上。
fig,ax= plt.subplots(3,2)
for l in range(3):
for m in range(2):
ax[l,m].subplot2grID((6,colspan=3) ##1
ax[l,sharex=scatter_axes_1) ##2
ax[l,sharex=scatter_axes_2) ##3
ax[l,sharey=scatter_axes_1) ##4
ax[l,sharex= y_hist_axes_1) ##5
但是我不能像这样使用 subplot2grID,我收到错误
'Axessubplot' object has no attribute 'subplot2grID'
还有其他函数可以与 Axessubplot 一起使用吗?
我对你要做什么感到有些困惑。然而,处理不同宽度和高度的另一种方法可能是使用宽度比?
编辑:使用子图来保留轴的逻辑组。
import matplotlib.pyplot as plt
fig = plt.figure(consTrained_layout=True,figsize=(8,12))
sfigs = fig.subfigures(3,2)
for nn,sf in enumerate(sfigs.flat):
sf.suptitle(nn)
axs = sf.subplots(3,2,gridspec_kw={'width_ratios': [2,1],'height_ratios': [2,1]})
sf.delaxes(axs[2,1])
plt.show()
我认为这是 matplotlib 的 sematic figure composition function,即 subplot_mosaic
函数的工作。这在 matplotlib > 3.3 中可用。您需要为 5 个面板定义基本布局,然后根据所需的行/列数生成完整布局。据我所知,通过 subplot2grid
或 Gridspec
或任何其他方法创建这将是非常复杂和困难的(尽管并非不可能!)。
import matplotlib.pyplot as plt
import numpy as np
def layout(panel,rows=3,cols=2,empty_senTinal=999):
"""Takes in a single layout and arranges it in multiple
rows and columns"""
npanels = rows * cols
panel[panel >= empty_senTinal] = empty_senTinal
minipanels = len(np.unique(panel))
panels = np.array([i * (minipanels) + panel for i in range(npanels)])
panel_rows = [np.hstack(panels[i : i + cols]) for i in range(0,npanels,cols)]
panel_cols = np.vstack(panel_rows)
panel_cols[panel_cols > empty_senTinal] = empty_senTinal
return panel_cols
A) 生成单个面板:
single_panel = np.array([
[1,1,999],[1,[3,3,4,[5,5,999,[999] * 9,])
fig,ax = plt.subplot_mosaic(single_panel,figsize=(10,10),empty_senTinel=999)
for k,v in ax.items():
v.set_xticklabels([])
v.set_yticklabels([])
v.text(0.5,0.5,k,ha="center",va="center",fontsize=25)
plt.show()
(B)“平铺”上面的单面板
@H_441_5@my_layout = layout(panel=single_panel,cols=2) fig,ax = plt.subplot_mosaic(my_layout,fontsize=25) plt.show()
一些注意事项:
以上是大佬教程为你收集整理的将 subplot2grid 与“AxesSubplot”对象一起使用全部内容,希望文章能够帮你解决将 subplot2grid 与“AxesSubplot”对象一起使用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。