Skip to content

Commit

Permalink
Fix axes title positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloneves committed Sep 5, 2021
1 parent ff66eaf commit 8dd98d6
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 49 deletions.
27 changes: 21 additions & 6 deletions charp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
and functional charts
"""

import matplotlib as mpl
import matplotlib.pyplot as plt


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

color_highlight = plt.rcParams["lines.color"] # default is orange
Expand All @@ -17,11 +19,12 @@ def set_title(title, fontsize=16, ax=None):
# https://stackoverflow.com/questions/62997001/matplotlib-how-to-exact-align-the-title-to-the-y-label
if ax is None:
ax = plt.gca()

bbox = ax.get_yticklabels()[-1].get_window_extent()
print(bbox)
x, _ = ax.transAxes.inverted().transform([0, bbox.y0])
return ax.set_title(title, ha="left", x=0, fontsize=fontsize)
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, _ = 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)


def rotate_xlabels(angle=45):
Expand Down Expand Up @@ -54,9 +57,21 @@ def bar(ax=None):
if ax is None:
ax = plt.gca()
# white grid
ax.grid(axis="y", color="white", linestyle="--")
ax.grid(axis="y", color="white", linestyle="dotted")
ax.set_axisbelow(False)

ax.tick_params(
axis="x", bottom=False,
)


def example_chart():
# in ipython type: %matplotlib
import seaborn as sns

df = sns.load_dataset("iris")
fig = plt.figure()
ax = fig.gca()
plt.plot(df.query('species=="virginica"').petal_length)
ax.set_title("Maria vai com as outras")
return ax
Loading

0 comments on commit 8dd98d6

Please sign in to comment.