Skip to content

Commit c745c2a

Browse files
Fix FacetGrid.set_titles #6839 (#6843)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent db634ea commit c745c2a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Bug fixes
4242
By `Max Jones <https://github.com/maxrjones>`_.
4343
- Fix incompatibility with numpy 1.20 (:issue:`6818`, :pull:`6821`)
4444
By `Michael Niklas <https://github.com/headtr1ck>`_.
45+
- Make FacetGrid.set_titles send kwargs correctly using `handle.udpate(kwargs)`.
46+
(:issue:`6839`, :pull:`6843`)
47+
By `Oliver Lopez <https://github.com/lopezvoliver>`_.
4548

4649
Documentation
4750
~~~~~~~~~~~~~

xarray/plot/facetgrid.py

+2
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ def set_titles(self, template="{coord} = {value}", maxchar=30, size=None, **kwar
553553
)
554554
else:
555555
handle.set_text(title)
556+
handle.update(kwargs)
556557

557558
# The column titles on the top row
558559
for index, (ax, col_name, handle) in enumerate(
@@ -563,6 +564,7 @@ def set_titles(self, template="{coord} = {value}", maxchar=30, size=None, **kwar
563564
self.col_labels[index] = ax.set_title(title, size=size, **kwargs)
564565
else:
565566
handle.set_text(title)
567+
handle.update(kwargs)
566568

567569
return self
568570

xarray/tests/test_plot.py

+25
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def substring_not_in_axes(substring, ax):
112112
return all(check)
113113

114114

115+
def property_in_axes_text(property, property_str, target_txt, ax):
116+
"""
117+
Return True if the specified text in an axes
118+
has the property assigned to property_str
119+
"""
120+
alltxt = ax.findobj(mpl.text.Text)
121+
check = []
122+
for t in alltxt:
123+
if t.get_text() == target_txt:
124+
check.append(plt.getp(t, property) == property_str)
125+
return all(check)
126+
127+
115128
def easy_array(shape, start=0, stop=1):
116129
"""
117130
Make an array with desired shape using np.linspace
@@ -2260,6 +2273,18 @@ def setUp(self):
22602273

22612274
self.darray = darray
22622275

2276+
def test_title_kwargs(self):
2277+
g = xplt.FacetGrid(self.darray, col="col", row="row")
2278+
g.set_titles(template="{value}", weight="bold")
2279+
2280+
# Rightmost column titles should be bold
2281+
for label, ax in zip(self.darray.coords["row"].values, g.axes[:, -1]):
2282+
assert property_in_axes_text("weight", "bold", label, ax)
2283+
2284+
# Top row titles should be bold
2285+
for label, ax in zip(self.darray.coords["col"].values, g.axes[0, :]):
2286+
assert property_in_axes_text("weight", "bold", label, ax)
2287+
22632288
@pytest.mark.slow
22642289
def test_default_labels(self):
22652290
g = xplt.FacetGrid(self.darray, col="col", row="row")

0 commit comments

Comments
 (0)