Skip to content

Commit 462f54d

Browse files
committed
Emit axes for unbound colorbars
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.
1 parent 11dad0e commit 462f54d

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

src/tikzplotlib/_axes.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,26 @@ def __init__(self, data, obj): # noqa: C901
1010
"""Returns the PGFPlots code for an axis environment."""
1111
self.content = []
1212

13-
# Are we dealing with an axis that hosts a colorbar? Skip then, those are
14-
# treated implicitily by the associated axis.
15-
self.is_colorbar = _is_colorbar_heuristic(obj)
16-
if self.is_colorbar:
17-
return
18-
1913
# instantiation
2014
self.nsubplots = 1
2115
self.subplot_index = 0
2216
self.is_subplot = False
2317

24-
if isinstance(obj, mpl.axes.Subplot):
18+
self.axis_options = []
19+
20+
# Are we dealing with an axis that hosts a colorbar? Skip then, those are
21+
# treated implicitily by the associated axis.
22+
self.is_colorbar = _is_colorbar_heuristic(obj)
23+
24+
if isinstance(obj, mpl.axes.Subplot) and not self.is_colorbar:
2525
self._subplot(obj, data)
2626

2727
self.axis_options = []
2828
self.is_visible = obj.get_visible()
2929

3030
# check if axes need to be displayed at all
31-
if not (obj.axison and self.is_visible):
31+
# unassociated colorbars should have hidden axes; colorbars associated to axes will be printed by the axis
32+
if not (obj.axison and self.is_visible) or self.is_colorbar:
3233
self.axis_options.append("hide x axis")
3334
self.axis_options.append("hide y axis")
3435

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

150-
# find color bar
151-
colorbar = _find_associated_colorbar(obj)
152-
if colorbar:
153-
self._colorbar(colorbar, data)
151+
self.colorbar = _find_associated_colorbar(obj)
152+
if self.colorbar:
153+
self._colorbar(self.colorbar, data)
154154

155155
if self.is_subplot:
156156
self.content.append("\n\\nextgroupplot")
@@ -798,7 +798,7 @@ def _handle_listed_color_map(cmap, data):
798798
if cmap.N is None or cmap.N == len(cmap.colors):
799799
colors = [
800800
f"rgb({k}{unit})=({rgb[0]:{ff}},{rgb[1]:{ff}},{rgb[2]:{ff}})"
801-
for k, rgb in enumerate(cmap.colors)
801+
for k, rgb in enumerate(map(mpl.colors.to_rgb, cmap.colors))
802802
]
803803
else:
804804
reps = int(float(cmap.N) / len(cmap.colors) - 0.5) + 1
@@ -856,6 +856,12 @@ def _find_associated_colorbar(obj):
856856
next axis environment, and see if it is de facto a color bar; if yes, return the
857857
color bar object.
858858
"""
859+
try:
860+
cbar = obj._colorbar
861+
if cbar is not None:
862+
return cbar
863+
except AttributeError:
864+
pass
859865
for child in obj.get_children():
860866
try:
861867
cbar = child.colorbar

src/tikzplotlib/_save.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ def _recurse(data, obj):
328328
"""Iterates over all children of the current object, gathers the contents
329329
contributing to the resulting PGFPlots file, and returns those.
330330
"""
331+
# bound_colorbars holds colorbars that are associated to axes
332+
# we don't add axes from colorbars if they come after the axis to which they were associated
333+
bound_colorbars = []
331334
content = _ContentManager()
332335
for child in obj.get_children():
333336
# Some patches are Spines, too; skip those entirely.
@@ -338,17 +341,20 @@ def _recurse(data, obj):
338341
if isinstance(child, mpl.axes.Axes):
339342
ax = _axes.Axes(data, child)
340343

341-
if ax.is_colorbar:
344+
if ax.is_colorbar and any(ax.colorbar is cb for cb in bound_colorbars):
342345
continue
343346

347+
if not ax.is_colorbar and ax.colorbar is not None:
348+
bound_colorbars.append(ax.colorbar)
349+
344350
# add extra axis options
345351
if data["extra axis options [base]"]:
346352
ax.axis_options.extend(data["extra axis options [base]"])
347353

348354
data["current mpl axes obj"] = child
349355
data["current axes"] = ax
350356

351-
if ax.is_visible:
357+
if ax.is_visible and not ax.is_colorbar:
352358
# Run through the child objects, gather the content.
353359
data, children_content = _recurse(data, child)
354360
else:

tests/test_colorbars_reference.tex

+64
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,70 @@
22

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

5+
\begin{axis}[
6+
colorbar horizontal,
7+
colormap={mymap}{[1pt]
8+
rgb(0pt)=(0,1,1);
9+
rgb(1pt)=(1,0,1)
10+
},
11+
hide x axis,
12+
hide y axis,
13+
point meta max=10,
14+
point meta min=-5,
15+
tick align=outside,
16+
tick pos=left,
17+
x grid style={darkgray176},
18+
xlabel={Some Units},
19+
xmin=-5, xmax=10,
20+
xtick style={color=black},
21+
ymin=0, ymax=1
22+
]
23+
\end{axis}
24+
25+
\begin{axis}[
26+
colorbar horizontal,
27+
colormap={mymap}{[1pt]
28+
rgb(0pt)=(1,0,0);
29+
rgb(1pt)=(0,0.5,0);
30+
rgb(2pt)=(0,0,1);
31+
rgb(3pt)=(0,0.75,0.75)
32+
},
33+
hide x axis,
34+
hide y axis,
35+
point meta max=8,
36+
point meta min=1,
37+
tick align=outside,
38+
tick pos=left,
39+
x grid style={darkgray176},
40+
xlabel={Discrete intervals, some other units},
41+
xmin=1, xmax=8,
42+
xtick style={color=black},
43+
ymin=0, ymax=1
44+
]
45+
\end{axis}
46+
47+
\begin{axis}[
48+
colorbar horizontal,
49+
colormap={mymap}{[1pt]
50+
rgb(0pt)=(0,0.4,1);
51+
rgb(1pt)=(0,0.8,1);
52+
rgb(2pt)=(1,0.8,0);
53+
rgb(3pt)=(1,0.4,0)
54+
},
55+
hide x axis,
56+
hide y axis,
57+
point meta max=1,
58+
point meta min=-1,
59+
tick align=outside,
60+
tick pos=left,
61+
x grid style={darkgray176},
62+
xlabel={Custom extension lengths, some other units},
63+
xmin=-1, xmax=1,
64+
xtick style={color=black},
65+
ymin=0, ymax=1
66+
]
67+
\end{axis}
68+
569
\begin{groupplot}[group style={group size=1 by 4}]
670
\nextgroupplot[
771
colorbar horizontal,

0 commit comments

Comments
 (0)