Skip to content

Commit

Permalink
Emit axes for unbound colorbars
Browse files Browse the repository at this point in the history
Fixes nschloe#606

Not sure if this is the best way to do it, since it seems a bit kludgy,
but it seems to work.
  • Loading branch information
JasonGross committed Apr 26, 2024
1 parent 11dad0e commit 462f54d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 15 deletions.
32 changes: 19 additions & 13 deletions src/tikzplotlib/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ def __init__(self, data, obj): # noqa: C901
"""Returns the PGFPlots code for an axis environment."""
self.content = []

# Are we dealing with an axis that hosts a colorbar? Skip then, those are
# treated implicitily by the associated axis.
self.is_colorbar = _is_colorbar_heuristic(obj)
if self.is_colorbar:
return

# instantiation
self.nsubplots = 1
self.subplot_index = 0
self.is_subplot = False

if isinstance(obj, mpl.axes.Subplot):
self.axis_options = []

# Are we dealing with an axis that hosts a colorbar? Skip then, those are
# treated implicitily by the associated axis.
self.is_colorbar = _is_colorbar_heuristic(obj)

if isinstance(obj, mpl.axes.Subplot) and not self.is_colorbar:
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 and self.is_visible):
# unassociated colorbars should have hidden axes; colorbars associated to axes will be printed by the axis
if not (obj.axison and self.is_visible) or self.is_colorbar:
self.axis_options.append("hide x axis")
self.axis_options.append("hide y axis")

Expand Down Expand Up @@ -147,10 +148,9 @@ def __init__(self, data, obj): # noqa: C901
if col != "white":
self.axis_options.append(f"axis background/.style={{fill={col}}}")

# find color bar
colorbar = _find_associated_colorbar(obj)
if colorbar:
self._colorbar(colorbar, data)
self.colorbar = _find_associated_colorbar(obj)
if self.colorbar:
self._colorbar(self.colorbar, data)

if self.is_subplot:
self.content.append("\n\\nextgroupplot")
Expand Down Expand Up @@ -798,7 +798,7 @@ def _handle_listed_color_map(cmap, data):
if cmap.N is None or cmap.N == len(cmap.colors):
colors = [
f"rgb({k}{unit})=({rgb[0]:{ff}},{rgb[1]:{ff}},{rgb[2]:{ff}})"
for k, rgb in enumerate(cmap.colors)
for k, rgb in enumerate(map(mpl.colors.to_rgb, cmap.colors))
]
else:
reps = int(float(cmap.N) / len(cmap.colors) - 0.5) + 1
Expand Down Expand Up @@ -856,6 +856,12 @@ def _find_associated_colorbar(obj):
next axis environment, and see if it is de facto a color bar; if yes, return the
color bar object.
"""
try:
cbar = obj._colorbar
if cbar is not None:
return cbar
except AttributeError:
pass
for child in obj.get_children():
try:
cbar = child.colorbar
Expand Down
10 changes: 8 additions & 2 deletions src/tikzplotlib/_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ def _recurse(data, obj):
"""Iterates over all children of the current object, gathers the contents
contributing to the resulting PGFPlots file, and returns those.
"""
# bound_colorbars holds colorbars that are associated to axes
# we don't add axes from colorbars if they come after the axis to which they were associated
bound_colorbars = []
content = _ContentManager()
for child in obj.get_children():
# Some patches are Spines, too; skip those entirely.
Expand All @@ -338,17 +341,20 @@ def _recurse(data, obj):
if isinstance(child, mpl.axes.Axes):
ax = _axes.Axes(data, child)

if ax.is_colorbar:
if ax.is_colorbar and any(ax.colorbar is cb for cb in bound_colorbars):
continue

if not ax.is_colorbar and ax.colorbar is not None:
bound_colorbars.append(ax.colorbar)

# add extra axis options
if data["extra axis options [base]"]:
ax.axis_options.extend(data["extra axis options [base]"])

data["current mpl axes obj"] = child
data["current axes"] = ax

if ax.is_visible:
if ax.is_visible and not ax.is_colorbar:
# Run through the child objects, gather the content.
data, children_content = _recurse(data, child)
else:
Expand Down
64 changes: 64 additions & 0 deletions tests/test_colorbars_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,70 @@

\definecolor{darkgray176}{RGB}{176,176,176}

\begin{axis}[
colorbar horizontal,
colormap={mymap}{[1pt]
rgb(0pt)=(0,1,1);
rgb(1pt)=(1,0,1)
},
hide x axis,
hide y axis,
point meta max=10,
point meta min=-5,
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xlabel={Some Units},
xmin=-5, xmax=10,
xtick style={color=black},
ymin=0, ymax=1
]
\end{axis}

\begin{axis}[
colorbar horizontal,
colormap={mymap}{[1pt]
rgb(0pt)=(1,0,0);
rgb(1pt)=(0,0.5,0);
rgb(2pt)=(0,0,1);
rgb(3pt)=(0,0.75,0.75)
},
hide x axis,
hide y axis,
point meta max=8,
point meta min=1,
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xlabel={Discrete intervals, some other units},
xmin=1, xmax=8,
xtick style={color=black},
ymin=0, ymax=1
]
\end{axis}

\begin{axis}[
colorbar horizontal,
colormap={mymap}{[1pt]
rgb(0pt)=(0,0.4,1);
rgb(1pt)=(0,0.8,1);
rgb(2pt)=(1,0.8,0);
rgb(3pt)=(1,0.4,0)
},
hide x axis,
hide y axis,
point meta max=1,
point meta min=-1,
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xlabel={Custom extension lengths, some other units},
xmin=-1, xmax=1,
xtick style={color=black},
ymin=0, ymax=1
]
\end{axis}

\begin{groupplot}[group style={group size=1 by 4}]
\nextgroupplot[
colorbar horizontal,
Expand Down

0 comments on commit 462f54d

Please sign in to comment.