Skip to content

Commit

Permalink
Fix flake8 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloneves committed Sep 7, 2021
1 parent daed494 commit c678b52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"python.testing.nosetestsEnabled": false,
"python.linting.pylintEnabled": false,
"python.linting.enabled": true,
"editor.formatOnPaste": true
"editor.formatOnPaste": true,
"files.trimTrailingWhitespace": true
}
14 changes: 7 additions & 7 deletions charp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
CharP is Charts for Paulo — Paulos aux functions and preferences for beautiful
and functional charts
"""
import textwrap
from typing import List, Iterable

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.text import Text

plt.style.use("./paulo.mplstyle")

color_highlight = plt.rcParams["lines.color"] # default is orange
# default is orange
color_highlight = plt.rcParams["lines.color"] # type: ignore
color_highlight2 = (0, 153, 51)
color_highlight3 = (30, 66, 139)


def set_title(title, fontsize=16, ax=None):
"Title always aligned to the left axis label"
"""Title always aligned to the left axis label.
Call after changing labels"""
# https://stackoverflow.com/questions/62997001/matplotlib-how-to-exact-align-the-title-to-the-y-label
if ax is None:
ax = plt.gca()
plt.gcf().canvas.draw() # without this, it won't work
x_min = min(lb.get_window_extent().x0 for lb in ax.get_yticklabels())
x_min = min(mpl.text.Text.get_window_extent(lb).x0 for lb in ax.get_yticklabels())
x_min = min(Text.get_window_extent(lb).x0 for lb in ax.get_yticklabels()) # type: ignore
x, _ = ax.transAxes.inverted().transform([x_min, 0])
plt.gcf().canvas.draw() # without this, it won't work
return ax.set_title(title, ha="left", x=x, fontsize=fontsize)
Expand Down Expand Up @@ -79,8 +80,7 @@ def break_labels(tick_labels: Iterable[Text]) -> List[str]:
line1, line2 = _break2lines(label_biggest)
acceptable_size = max(len(line1), len(line2))
labels_breaked = [
"\n".join(_break2lines(label.get_text(), acceptable_size))
for label in tick_labels
"\n".join(_break2lines(label.get_text(), acceptable_size)) for label in tick_labels
]
return labels_breaked

Expand Down
13 changes: 3 additions & 10 deletions charp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@

def test_break2lines():
s = "1234 1234 1234 1234"
assert ["1234 1234", "1234 1234"] == charp._break2lines(
s
), "Must break in 2 lines. Always."
assert ["1234 1234", "1234 1234"] == charp._break2lines(s), "Must break in 2 lines. Always."


def test_break2lines_middle():
s = "123 1234 123"
assert ["123 1234", "123"] == charp._break2lines(
s
), "Must break in 2 lines. Always."
assert ["123 1234", "123"] == charp._break2lines(s), "Must break in 2 lines. Always."


def test_break2lines_second_bigger():
s = "123 123 12 12345"
assert ["123 123", "12 12345"] == charp._break2lines(
s
), "Must break in 2 lines. Always."
assert ["123 123", "12 12345"] == charp._break2lines(s), "Must break in 2 lines. Always."


def test_break2lines_acceptable_size():
Expand All @@ -38,4 +32,3 @@ def test_break_labels():
labels = build_ticklabels(["abc cde efg", "abc cde"])
labels_breaked = charp.break_labels(labels)
assert ["abc cde\nefg", "abc cde"] == labels_breaked

0 comments on commit c678b52

Please sign in to comment.