Skip to content

Commit 702dc4d

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 a3928a3 commit 702dc4d

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
@@ -17,25 +17,26 @@ def __init__(self, data, obj): # noqa: C901
1717
"""Returns the PGFPlots code for an axis environment."""
1818
self.content = []
1919

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-
if self.is_colorbar:
24-
return
25-
2620
# instantiation
2721
self.nsubplots = 1
2822
self.subplot_index = 0
2923
self.is_subplot = False
3024

31-
if isinstance(obj, mpl.axes.Subplot):
25+
self.axis_options = []
26+
27+
# Are we dealing with an axis that hosts a colorbar? Skip then, those are
28+
# treated implicitily by the associated axis.
29+
self.is_colorbar = _is_colorbar_heuristic(obj)
30+
31+
if isinstance(obj, mpl.axes.Subplot) and not self.is_colorbar:
3232
self._subplot(obj, data)
3333

3434
self.axis_options = []
3535
self.is_visible = obj.get_visible()
3636

3737
# check if axes need to be displayed at all
38-
if not (obj.axison and self.is_visible):
38+
# unassociated colorbars should have hidden axes; colorbars associated to axes will be printed by the axis
39+
if not (obj.axison and self.is_visible) or self.is_colorbar:
3940
self.axis_options.append("hide x axis")
4041
self.axis_options.append("hide y axis")
4142

@@ -154,10 +155,9 @@ def __init__(self, data, obj): # noqa: C901
154155
if col != "white":
155156
self.axis_options.append(f"axis background/.style={{fill={col}}}")
156157

157-
# find color bar
158-
colorbar = _find_associated_colorbar(obj)
159-
if colorbar:
160-
self._colorbar(colorbar, data)
158+
self.colorbar = _find_associated_colorbar(obj)
159+
if self.colorbar:
160+
self._colorbar(self.colorbar, data)
161161

162162
if self.is_subplot:
163163
self.content.append("\n\\nextgroupplot")
@@ -801,7 +801,7 @@ def _handle_listed_color_map(cmap, data):
801801
if cmap.N is None or cmap.N == len(cmap.colors):
802802
colors = [
803803
f"rgb({k}{unit})=({rgb[0]:{ff}},{rgb[1]:{ff}},{rgb[2]:{ff}})"
804-
for k, rgb in enumerate(cmap.colors)
804+
for k, rgb in enumerate(map(mpl.colors.to_rgb, cmap.colors))
805805
]
806806
else:
807807
reps = int(float(cmap.N) / len(cmap.colors) - 0.5) + 1
@@ -859,6 +859,12 @@ def _find_associated_colorbar(obj):
859859
next axis environment, and see if it is de facto a color bar; if yes, return the
860860
color bar object.
861861
"""
862+
try:
863+
cbar = obj._colorbar
864+
if cbar is not None:
865+
return cbar
866+
except AttributeError:
867+
pass
862868
for child in obj.get_children():
863869
try:
864870
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)