Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide invisible axes while still displaying colorbars #607

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}