I am trying to produce a set of plots using grispec. There should be 5 rows and 2 columns. There should be an images shown in each of the axes (using imshow) in the top four rows. In the bottom left axes I want to show/plot some text. However, the text seems to be too long to be displayed in one line. Is there a way to print it in something that I would call a 'text box'?
I created a minimal example (see below). 'circle.png' can be seen as a placeholder for some png file.
There are two related examples I found on stackoverflow (example 1 and example2). But I am not sure how they could be applicable here.
I can not / do not want to make a string variable with three quotation marks (docstring), because I am reading the text from a bigger ascii file.
Also, I am not sure if gridspec is the best way to do this. Thanks for pointers!
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
def main():
"""
goal is to show justified text in one axes of matplotlib
"""
plt.close('all')
fig = plt.figure(figsize=(5, 10))
plt.subplots_adjust(left=0.1, right=0.9, top=0.95, bottom=0.1)
n_rows = 5
outer_grid = gridspec.GridSpec(n_rows, 2 )# ,wspace=0.0, hspace=0.0
lst_files = [ 'circle.png'
, 'circle.png'
, 'circle.png'
, 'circle.png'
, 'text'
, 'circle.png'
, 'circle.png'
, 'circle.png'
, 'circle.png']
for cur_map_id, cur_map_file in enumerate(lst_files):
cur_row = (cur_map_id % n_rows)
if cur_map_id / n_rows == 0:
cur_column = 0
else:
cur_column = 1
# preparation: no axes
ax = plt.subplot(outer_grid[cur_row, cur_column], frameon=False)
ax.axes.get_yaxis().set_visible(False)
ax.axes.get_xaxis().set_visible(False)
# fix for the fact that the fourth entry is text and not in tmp_lst_imgs
if cur_map_id > 4:
cur_map_id = cur_map_id - 1
# the actual plotting
if cur_map_file == 'text':
lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
ax.text(0.05, 0.9, lorem, size=6)
else:
print cur_map_id
im = plt.imread(cur_map_file)
ax.imshow(im)
ax.set_title(cur_map_file, size=6)
fig.add_subplot(ax)
plt.savefig('blah.png', dpi=300)
print "done!"
if __name__ == '__main__':
main()