我有一个创建类似2D直方图网格的函数。因此,我可以选择是否将这个新情节放在预先存在的数字上,我会执行以下操作:
def make_hist2d(x, y, current_fig=False, layout=(1,1,1),*args):
if current_fig:
fig = _plt.gcf()
ax = fig.add_subplot(*layout) # layout=(nrows, ncols, nplot)
else:
fig, ax = _plt.subplots()
H, x, y = np.histogram2d(...)
# manipulate the histogram, e.g. column normalize.
XX, YY = _np.meshgrid(xedges, yedges)
Image = ax.pcolormesh(XX, YY, Hplot.T, norm=norm, **pcmesh_kwargs)
ax.autoscale(tight=True)
grid_kargs = {'orientation': 'vertical'}
cax, kw = _mpl.colorbar.make_axes_gridspec(ax, **grid_kargs)
cbar = fig.colorbar(Image, cax=cax)
cbar.set_label(cbar_title)
return fig, ax, cbar
def hist2d_grid(data_dict, key_pairs, layout, *args): # ``*args`` are things like xlog, ylog, xlabel, etc.
# that are common to all subplots in the figure.
fig, ax = _plt.subplots()
nplots = range(len(key_pairs) + 1) # key_pairs = ((k1a, k1b), (k2a, k2b), ..., (kna, knb))
ax_list = []
for pair, i in zip(key_pairs, nplots):
fig, ax, cbar = make_hist2d(data[k1a], data[k1b]
ax_list.append(ax)
return fig, ax_list
然后我打电话给:
hgrid = hist2d_grid(...)
但是,如果我想向grid
添加一个新图形,我不知道获得子图布局的好方法。例如,是否有类似的东西:
layout = fig.get_layout()
那会给我一些像(nrows, ncols, n_subplots)
?
我可以用以下的方式做到这一点:
n_plot = len(ax_list) / 2 # Each subplot generates a plot and a color bar.
n_rows = np.floor(np.sqrt(n_ax))
n_cols = np.ceil(np.sqrt(n_ax))
但是我必须处理像(2,4)
子图数组这样的特殊情况,我会得到n_rows = 2
和n_cols = 3
,这意味着我会将(2,3,8)
传递给ax.add_subplot()
<div id="waterfall">
<div class="fall" id="water1">Defaulter’s Initial Margin</div>
<div class="fall" id="water2">Defaulter’s Guaranty Fund Contribution</div>
<div class="fall" id="water3">Clearing House Contribution $20 Million</div>
<div class="fall" id="water4">Guaranty Fund<br>Total Value XX Million</div>
<div class="fall" id="water5">Unfunded contributions - assessment
<div class="text-box" id="text-box-2">text text text text text text text
text text text text text text text text text text text text</div>
</div>
<div class="fall" id="water6">Service Continuity Phase
<div class="text-box" id="text-box-1">text text text text text text
text text text text text text text text text text text text text</div>
</div>
<div class="fall" id="water7">Service Closure</div>
</div><!-- end #waterfall -->
,显然不起作用,因为8&gt; 3 * 2
答案 0 :(得分:2)
由;WITH Parents
AS
(
Select SP1.SupplierId , SP1.ParentId from SupplierLinks SP1 where SP1.SupplierId = @SupplierId
Union All
Select SP2.SupplierId , SP2.ParentId
From SupplierLinks SP2 Inner Join Parents C on C.ParentId = SP2.SupplierId
)
Decalare @ParentId int
Select @ParentId = P.SupplierId
From Parents as P
Where parentid = 0 or ParentId is null
;WITH Children
AS
(
Select SP1 .SupplierId , SP1 .ParentId from SupplierLinks SP1 where SupplierId = @ParentId
Union All
Select SP2.SupplierId , SP2.ParentId
From SupplierLinks SP2 Inner Join Children C on C.SupplierId = SP2.parentid
)
返回的ax
是一个numpy轴数,然后fig, ax = plt.subplots(4,2)
将为您提供所需的布局信息,例如。
ax.shape
您还可以通过循环图形对象的子项
来获取各个轴的位置 nrows, ncols = ax.shape
n_subplots = nrows*ncols
并且可能从最终元素[[f.colNum, f.rowNum] for f in fig.get_children()[1:]]
获得大小
如果需要,您还可以使用gridspec更明确地了解子图的位置。使用gridspec,您可以设置gridspec对象并传递给子图,
fig.get_children()[-1]
要获得可以使用的布局,
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])