matplotlib pcolormesh plot with multiple axes on one figure using gridspec

23 Views Asked by At

Wondering if someone can help explain why the first code block runs fine, while the second code block throws this error:

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) Cell In[12], line 9
>       6 ax3 = fig.add_subplot(gs1[2])
>       7 ax4 = fig.add_subplot(gs1[3])
> ----> 9 im1 = ax1.pcolormesh(d0, t0, r0, vmin=0, vmax=vm)
>      10 plt.colorbar(im1, cax=ax2)
>      12 im2 = ax3.pcolormesh(d1, t1, r1, vmin=0, vmax=vm)
> 
> File c:\Users\Sophie
> Varabioff\miniconda3\envs\single-ping-env\Lib\site-packages\matplotlib\__init__.py:1442,
> in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)    1439
> @functools.wraps(func)    1440 def inner(ax, *args, data=None,
> **kwargs):    1441     if data is None:
> -> 1442         return func(ax, *map(sanitize_sequence, args), **kwargs)    1444     bound = new_sig.bind(ax, *args, **kwargs)    1445     auto_label = (bound.arguments.get(label_namer)    1446       
> or bound.kwargs.get(label_namer))
> 
> File c:\Users\Sophie
> Varabioff\miniconda3\envs\single-ping-env\Lib\site-packages\matplotlib\axes\_axes.py:6220,
> in Axes.pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading,
> antialiased, *args, **kwargs)    6217 shading = shading.lower()   
> 6218 kwargs.setdefault('edgecolors', 'none')
> -> 6220 X, Y, C, shading = self._pcolorargs('pcolormesh', *args,    6221                                     shading=shading,
> kwargs=kwargs)    6222 coords = np.stack([X, Y], axis=-1)    6223 #
> convert to one dimensional array, except for 3D RGB(A) arrays ...
> -> 5727     nrows, ncols = C.shape[:2]    5728 else:    5729     raise _api.nargs_error(funcname, takes="1 or 3", given=len(args))
> 
> ValueError: not enough values to unpack (expected 2, got 1)

For context, I am hoping to do something similar to the second code block so that I can add a line object to ax1 and ax3 and be able to update the line using mouse clicks. This is code block 1:

in_path = "xxx"

d220, d291, d220r, d291r = generate_echomap_data(in_path)

d0, d1, t0, t1, r0, r1 = generate_rectangular_data(d220, d291, d220r, d291r) 

vm = int(np.maximum(np.max(d0), np.max(d1))) + 1
fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, figsize=(2*(ECHOMAP_PIXELS/my_dpi), ECHOMAP_PIXELS/my_dpi), dpi=my_dpi)

im = ax0.pcolormesh(t0, r0, d0, vmin=0, vmax=vm)
fig.colorbar(im, ax=ax0)
ax0.set_title(f'Echomap Plot of 50m (Long Range) Scan {scan_number} Data')
plt.grid()

im1 = ax1.pcolormesh(t1, r1, d1, vmin=0, vmax=vm)
fig.colorbar(im1, ax=ax1)
ax1.set_title(f'Echomap Plot of 50m (Long Range) Scan {scan_number} Data')
plt.grid()

plt.tight_layout()

this is code block 2:

in_path = "xxx"

d220, d291, d220r, d291r = generate_echomap_data(in_path)

d0, d1, t0, t1, r0, r1 = generate_rectangular_data(d220, d291, d220r, d291r) 

vm = int(np.maximum(np.max(d0), np.max(d1))) + 1

fig = plt.figure()

gs1 = fig.add_gridspec(1, 4, width_ratios=[10,1,10,1], height_ratios=[1])
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
ax3 = fig.add_subplot(gs1[2])
ax4 = fig.add_subplot(gs1[3])

im1 = ax1.pcolormesh(d0, t0, r0, vmin=0, vmax=vm)
plt.colorbar(im1, cax=ax2)

im2 = ax3.pcolormesh(d1, t1, r1, vmin=0, vmax=vm)
plt.colorbar(im2, cax=ax4)

gs1.tight_layout(fig, rect=[None, None, 0.45, None])

plt.show()
0

There are 0 best solutions below