From 34ff73c441e49ab85b4803b6892af57fc1982c49 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Thu, 25 Apr 2024 20:15:44 -0700 Subject: [PATCH] Hide invisible axes while still displaying colorbars This at least allows a workaround for #606 --- src/tikzplotlib/_axes.py | 3 ++- src/tikzplotlib/_save.py | 8 ++++++-- tests/test_colorbars.py | 12 +++++++++++- tests/test_colorbars_reference.tex | 25 +++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/tikzplotlib/_axes.py b/src/tikzplotlib/_axes.py index fa84db2f..b4c6d005 100644 --- a/src/tikzplotlib/_axes.py +++ b/src/tikzplotlib/_axes.py @@ -32,9 +32,10 @@ def __init__(self, data, obj): # noqa: C901 self._subplot(obj, data) self.axis_options = [] + self.is_visible = obj.get_visible() # check if axes need to be displayed at all - if not obj.axison: + if not (obj.axison and self.is_visible): self.axis_options.append("hide x axis") self.axis_options.append("hide y axis") diff --git a/src/tikzplotlib/_save.py b/src/tikzplotlib/_save.py index d89cadfa..96ceabd0 100644 --- a/src/tikzplotlib/_save.py +++ b/src/tikzplotlib/_save.py @@ -348,8 +348,12 @@ def _recurse(data, obj): data["current mpl axes obj"] = child data["current axes"] = ax - # Run through the child objects, gather the content. - data, children_content = _recurse(data, child) + if ax.is_visible: + # Run through the child objects, gather the content. + data, children_content = _recurse(data, child) + else: + # we may still display the colorbar + children_content = [] # populate content and add axis environment if desired if data["add axis environment"]: diff --git a/tests/test_colorbars.py b/tests/test_colorbars.py index 3d384f1f..8fb7e730 100644 --- a/tests/test_colorbars.py +++ b/tests/test_colorbars.py @@ -4,7 +4,7 @@ def plot(): # Make a figure and axes with dimensions as desired. - fig, ax = plt.subplots(3) + fig, ax = plt.subplots(4) # Set the colormap and norm to correspond to the data for which the colorbar will be # used. @@ -69,6 +69,16 @@ def plot(): ) cb3.set_label("Custom extension lengths, some other units") + # Set the colormap and norm to correspond to the data for which the colorbar will be + # used. This time attach the colorbar to axes. + cmap = mpl.cm.cool + norm = mpl.colors.Normalize(vmin=-5, vmax=10) + + img = ax[3].imshow([[0, 1]], cmap=cmap) + ax[3].set_visible(False) + cax = fig.add_axes([0.1, 1, 0.8, 0.1]) + cb4 = fig.colorbar(img, cax=cax, orientation="horizontal", label="Some Units") + return fig diff --git a/tests/test_colorbars_reference.tex b/tests/test_colorbars_reference.tex index dec2184e..6c25b552 100644 --- a/tests/test_colorbars_reference.tex +++ b/tests/test_colorbars_reference.tex @@ -1,3 +1,28 @@ \begin{tikzpicture} +\definecolor{darkgray176}{RGB}{176,176,176} + +\begin{groupplot}[group style={group size=1 by 4}] +\nextgroupplot[ +colorbar horizontal, +colormap={mymap}{[1pt] + rgb(0pt)=(0,1,1); + rgb(1pt)=(1,0,1) +}, +hide x axis, +hide y axis, +point meta max=1, +point meta min=0, +tick align=outside, +tick pos=left, +x grid style={darkgray176}, +xmin=-0.5, xmax=1.5, +xtick style={color=black}, +y dir=reverse, +y grid style={darkgray176}, +ymin=-0.5, ymax=0.5, +ytick style={color=black} +] +\end{groupplot} + \end{tikzpicture}