Matplotlib extend limits to fit text
Draft.
Context: 230529-2208 Seaborn matplotlib labeling data points
Given: need to make the limits larger to fit text, the last lines here:
data = df_pages.reset_index().sort_values('num_pages')
ax = sns.barplot(data,y="collection",x="num_pages")
# label points
for i in ax.axes.containers:
ax.bar_label(
i,
)
# make the labels fit the limits
xlim = ax.axes.get_xlim()[1]
new_xlim = xlim + 14600
ax.axes.set_xlim(0, new_xlim)
Question: by how much?
Answer:
- Transformations Tutorial — Matplotlib 3.8.0 documentation for converting text-pixels to data-limit
- ??? to get the text dimensions
for i in ax.axes.containers:
an = ax.bar_label(
i,
)
# `an` is a list of all Annotations
an[0].get_window_extent()
>>> Bbox(88.66956472198585, 388.99999999999994], [123.66956472198585, 402.99999999999994)
def get_text_size(anno): # Annotation
""" TODO: get array of annos, find the leftmost one etc."""
bbox = anno.get_window_extent()
ext = bbox.bounds
# > (91.43835300441604, 336.19999999999993, 35.0, 14.0)
x=ext[2]
y=ext[3]
return x,y
"""
ano = an[1]
bbox = ano.get_window_extent()
bbox.bounds
> (91.43835300441604, 336.19999999999993, 35.0, 14.0)
"""
get_text_size(an[6])
Nel mezzo del deserto posso dire tutto quello che voglio.