How to use NumPy slices in 2d for PyPlot's GridSpec

I am trying to create a plot with multiple subplots in PyPlot. For that I am using matplotlib’s GridSpec .
Some of the usage is given by NumPy slices as seen in their example from the docs:

fig8 = plt.figure(constrained_layout=False)
gs1 = fig8.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
f8_ax1 = fig8.add_subplot(gs1[:-1, :])
f8_ax2 = fig8.add_subplot(gs1[-1, :-1])
f8_ax3 = fig8.add_subplot(gs1[-1, -1])

It was shown here that slicing can be done by

f8_ax1 = fig8.add_subplot(get(gs1, pycall(pybuiltin("slice"), PyObject, 0,3)))

The above line is equivalent to gs1[0, 0:3].

But my question is, how can I do it in 2d? I mean how can I slice for example gs1[0:2, 0:3]?

Use a combination of get and pybuiltin("slice")

using PyPlot
using PyCall

fig = figure()
gs = fig.add_gridspec(2,2)

element(i,j) = get(gs, (i,j))
slice(i,j) = pycall(pybuiltin("slice"), PyObject, i,j)

ax = fig.add_subplot(element(0,0))
ax.plot(sin.(-pi:0.1:pi))

ax = fig.add_subplot(element(0,1))
ax.plot(cos.(-pi:0.1:pi))

ax = fig.add_subplot(element(1,slice(0,2)))
ax.plot(-pi:0.1:pi |> collect)

2 Likes