Skip to content

Commit

Permalink
Improve tick text wrapping by encouraging line breaks at certain char…
Browse files Browse the repository at this point in the history
…acters like slashes.
  • Loading branch information
maximz committed Jan 31, 2022
1 parent 509e534 commit 07b5644
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions genetools/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,11 @@ def stacked_bar_plot(


def wrap_tick_labels(
ax: matplotlib.axes.Axes, wrap_x_axis=True, wrap_y_axis=True, wrap_amount=20
ax: matplotlib.axes.Axes,
wrap_x_axis=True,
wrap_y_axis=True,
wrap_amount=20,
break_characters=["/"],
) -> matplotlib.axes.Axes:
"""Add text wrapping to tick labels on x and/or y axes on any plot.
Expand All @@ -572,6 +576,8 @@ def wrap_tick_labels(
:type wrap_y_axis: bool, optional
:param wrap_amount: length of each line of text, defaults to 20
:type wrap_amount: int, optional
:param break_characters: characters at which to encourage to breaking text into lines, defaults to ['/']. set to None or [] to disable.
:type break_characters: list, optional
:return: plot with modified tick labels
:rtype: matplotlib.axes.Axes
"""
Expand All @@ -584,7 +590,15 @@ def wrap_tick_labels(

def wrap_labels(labels):
for label in labels:
label.set_text("\n".join(textwrap.wrap(label.get_text(), wrap_amount)))
original_text = label.get_text()
if break_characters is not None:
# encourage breaking at this character. e.g. convert "/" to "/ " to encourage line break there.
for break_character in break_characters:
break_character_stripped = break_character.strip()
original_text = original_text.replace(
break_character_stripped, f"{break_character_stripped} "
)
label.set_text("\n".join(textwrap.wrap(original_text, wrap_amount)))
return labels

if wrap_x_axis:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def test_wrap_axis_labels():
+ [{"cluster": "very long cluster name 1", "expanded": "Expanded"}] * 20
+ [{"cluster": "very long cluster name 2", "expanded": "Not expanded"}] * 50
+ [{"cluster": "very long cluster name 2", "expanded": "Expanded"}] * 5
+ [{"cluster": "very long cluster name 3", "expanded": "Not expanded"}] * 15
+ [{"cluster": "very long cluster name 3", "expanded": "Expanded"}] * 15
+ [{"cluster": "very/long/cluster/name/3", "expanded": "Not expanded"}] * 15
+ [{"cluster": "very/long/cluster/name/3", "expanded": "Expanded"}] * 15
)
fig, ax = plots.stacked_bar_plot(
df,
Expand Down

0 comments on commit 07b5644

Please sign in to comment.