Skip to content

Commit a3928a3

Browse files
committed
Hide invisible axes while still displaying colorbars
This at least allows a workaround for nschloe#606
1 parent 450712b commit a3928a3

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/tikzplotlib/_axes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ def __init__(self, data, obj): # noqa: C901
3232
self._subplot(obj, data)
3333

3434
self.axis_options = []
35+
self.is_visible = obj.get_visible()
3536

3637
# check if axes need to be displayed at all
37-
if not obj.axison:
38+
if not (obj.axison and self.is_visible):
3839
self.axis_options.append("hide x axis")
3940
self.axis_options.append("hide y axis")
4041

src/tikzplotlib/_save.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,12 @@ def _recurse(data, obj):
348348
data["current mpl axes obj"] = child
349349
data["current axes"] = ax
350350

351-
# Run through the child objects, gather the content.
352-
data, children_content = _recurse(data, child)
351+
if ax.is_visible:
352+
# Run through the child objects, gather the content.
353+
data, children_content = _recurse(data, child)
354+
else:
355+
# we may still display the colorbar
356+
children_content = []
353357

354358
# populate content and add axis environment if desired
355359
if data["add axis environment"]:

tests/test_colorbars.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def plot():
66
# Make a figure and axes with dimensions as desired.
7-
fig, ax = plt.subplots(3)
7+
fig, ax = plt.subplots(4)
88

99
# Set the colormap and norm to correspond to the data for which the colorbar will be
1010
# used.
@@ -69,6 +69,16 @@ def plot():
6969
)
7070
cb3.set_label("Custom extension lengths, some other units")
7171

72+
# Set the colormap and norm to correspond to the data for which the colorbar will be
73+
# used. This time attach the colorbar to axes.
74+
cmap = mpl.cm.cool
75+
norm = mpl.colors.Normalize(vmin=-5, vmax=10)
76+
77+
img = ax[3].imshow([[0, 1]], cmap=cmap)
78+
ax[3].set_visible(False)
79+
cax = fig.add_axes([0.1, 1, 0.8, 0.1])
80+
cb4 = fig.colorbar(img, cax=cax, orientation="horizontal", label="Some Units")
81+
7282
return fig
7383

7484

tests/test_colorbars_reference.tex

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
\begin{tikzpicture}
22

3+
\definecolor{darkgray176}{RGB}{176,176,176}
4+
5+
\begin{groupplot}[group style={group size=1 by 4}]
6+
\nextgroupplot[
7+
colorbar horizontal,
8+
colormap={mymap}{[1pt]
9+
rgb(0pt)=(0,1,1);
10+
rgb(1pt)=(1,0,1)
11+
},
12+
hide x axis,
13+
hide y axis,
14+
point meta max=1,
15+
point meta min=0,
16+
tick align=outside,
17+
tick pos=left,
18+
x grid style={darkgray176},
19+
xmin=-0.5, xmax=1.5,
20+
xtick style={color=black},
21+
y dir=reverse,
22+
y grid style={darkgray176},
23+
ymin=-0.5, ymax=0.5,
24+
ytick style={color=black}
25+
]
26+
\end{groupplot}
27+
328
\end{tikzpicture}

0 commit comments

Comments
 (0)