How to extend plot area to keep text inside of plot area without clipping

51 views Asked by At

I have a plot with a long text string that was inserted (string not determined by me, so I can't shorten it):

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

lines = [[(0, 1), (1, 1)], [(2, 3), (3, 3)], [(1, 2), (1, 3)]]
z = [1, 2, 3]

lc = LineCollection(lines, array=z, cmap="coolwarm", linewidth = 8)

fig, ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()

fig.colorbar(lc)
ax.text(2,3,'some very long text that will go outside of the plot boundaries')
plt.savefig('tmp.png')

which generates the following:

enter image description here

as you can see, the long text goes outside of the plot area.

matplotlib text only in plot area is similar, but I cannot clip the text.

I have also tried subplots.adjust() (Increasing the space for x axis labels in Matplotlib) but that generates something even worse:

enter image description here

I've also thought of using some other way than subplots, but subplots seem to be standard practice: https://matplotlib.org/stable/gallery/shapes_and_collections/line_collection.html

How can I extend the plot area to the maximum extent of the text?

0

There are 0 answers