nschloe/tikzplotlib

Incorrect rendering of (relatively) complex grid of imshows and colorbars using `AxesGrid`

Sbozzolo opened this issue · 1 comments

I am using AxesGrid to plot a three images with distinct colorbars following the example provided by the documentation of matplotlib.
tikzplotlib fails to replicate the grid structure (see below). I am not sure what is the expected tikz code (maybe using groupplots?).

(Sorry if the code below is not the most minimal example possible.)

#!/usr/bin/env python3

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import numpy as np

from tikzplotlib import save as tikz_save

num_pixels = 512
image_size = 20

coords = np.linspace(-image_size/2, image_size/2, num_pixels)
dx = (coords[1]-coords[0])/2.
extent = [coords[0] - dx, coords[-1] + dx, coords[0] - dx, coords[-1] + dx]

int1 = np.random.standard_normal((num_pixels, num_pixels))
int2 = np.random.standard_normal((num_pixels, num_pixels))
int3 = np.random.standard_normal((num_pixels, num_pixels))

vmin, vmax = -1, 1

fig = plt.figure(figsize=(20,10))

grid = AxesGrid(fig, 111,  # similar to subplot(121)
                nrows_ncols=(1, 3),
                axes_pad=0.50,
                share_all=True,
                label_mode="L",
                cbar_location="top",
                cbar_mode="each",
                cbar_pad=0.35,
                cbar_size="8%",
                direction="column"
                )


ax_in, ax_out, ax_err = grid
cax_in, cax_out, cax_err = grid.cbar_axes

im_in = ax_in.imshow(int1.T, extent=extent, vmin=vmin, vmax=vmax)
cbar_in = cax_in.colorbar(im_in)
im_out = ax_out.imshow(int2.T, extent=extent, vmin=vmin, vmax=vmax)
cbar_out = cax_out.colorbar(im_out)
im_err = ax_err.imshow(int3, extent=extent, cmap=plt.get_cmap("cividis"))
cax_err.colorbar(im_err)
cbar_err = cax_err.colorbar(im_err, cmap=plt.get_cmap("cividis"))

ax_in.set_xlabel("alpha [M]")
ax_in.set_ylabel("beta [M]")
ax_out.set_xlabel("alpha [M]")
ax_out.set_ylabel("beta [M]")
ax_err.set_xlabel("alpha [M]")
ax_err.set_ylabel("beta [M]")
plt.savefig("wrong.pdf")
tikz_save("image.tikz", standalone=True)

See below the result (top is expected, bottom is obtained).

2022-05-09:10:01:03

I attach a passable tikz code for the plot I wanted to produce.
okay_plot.txt