Skip to content

Commit

Permalink
Hide invisible axes while still displaying colorbars
Browse files Browse the repository at this point in the history
This at least allows a workaround for nschloe#606
  • Loading branch information
JasonGross committed Apr 26, 2024
1 parent 450712b commit 34ff73c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/tikzplotlib/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
8 changes: 6 additions & 2 deletions src/tikzplotlib/_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_colorbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down
25 changes: 25 additions & 0 deletions tests/test_colorbars_reference.tex
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 34ff73c

Please sign in to comment.