When placing text in a matplotlib figure, it is possible to specify a location in axes coordinates. However, as the text becomes longer, it will eventually take more space. As suggested here, given a text it is possible to draw a bounding box around it. Is there a way to do it the other way around, that is, given a user-defined bounding box, make sure that the text stays in that bounding box? I am aware that this might require automatic font size adjustment or line breaking. I'm just not sure if something like this already exists in matplotlib.
Use case: What I want to get is a table consisting of rectangular cells as shown below, where each cell should contain a small string in it. The strings should not leve the cells and overlap with each other. Optimally, they should be entered. Here is the code generating the table (without text so far). If you could think of an easier way to achieve my goal rather than manipulating text in matplotlib, please let me know as well.
import matplotlib.pyplot as plt
import numpy as np
import sys
np.random.seed(int(sys.argv[1]))
fig, ax = plt.subplots(1,1)
all_coordinates = np.array([(i,j) for i in range(1, 6) for j in range(1,6)])
for i, coordinates in enumerate(np.random.permutation(all_coordinates)):
x, y = coordinates
if i<9:
color = 'r'
elif i<17:
color = 'b'
elif i<24:
color = 'y'
else:
color = 'k'
ax.fill_between(np.arange(x-0.5, x+0.5, 0.01), y-0.5, y+0.5, color=color)
low, high = 0.5, 5.5
ax.set_xlim(low, high)
ax.set_ylim(low, high)
major_ticks = np.arange(1, 6, 1)
ax.set_xticks(major_ticks)
ax.set_yticks(major_ticks)
minor_ticks = [1.5, 2.5, 3.5, 4.5]
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(minor_ticks, minor=True)
ax.grid(True, which='minor')
plt.show()
