FaCoffee
FaCoffee

Reputation: 7929

Displaying a line of text outside of a plot

I have a matrix plot produced by the matplotlib library. The size of my matrix is 256x256, and I already have a legend and a colorbar with proper ticks. I cannot attach any image due to my being new to stackoverflow. Anyhow, I use this code to generate the plot:

# Plotting - Showing interpolation of randomization
plt.imshow(M[-257:,-257:].T, origin='lower',interpolation='nearest',cmap='Blues', norm=mc.Normalize(vmin=0,vmax=M.max()))
title_string=('fBm: Inverse FFT on Spectral Synthesis')
subtitle_string=('Lattice size: 256x256 | H=0.8 | dim(f)=1.2 | Ref: Saupe, 1988 | Event: 50 mm/h, 15 min')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=9)
plt.show()

# Makes a custom list of tick mark intervals for color bar (assumes minimum is always zero)
numberOfTicks = 5
ticksListIncrement = M.max()/(numberOfTicks)
ticksList = []
for i in range((numberOfTicks+1)):
    ticksList.append(ticksListIncrement * i) 

cb=plt.colorbar(orientation='horizontal', format='%0.2f', ticks=ticksList) 
cb.set_label('Water depth [m]') 
plt.show()
plt.xlim(0, 255)
plt.xlabel('Easting (Cells)') 
plt.ylim(255, 0)
plt.ylabel('Northing (Cells)')

Now, being my subtitle too long (3rd line of code in the excerpt reported here), it interferes with the Y axis ticks, and I don't want this. Instead, some of the information reported in the subtitle I would like to be re-routed to a line of text to be placed at the bottom center of the image, under the colorbar label. How can this be done with matplotlib?

Upvotes: 5

Views: 13452

Answers (1)

Joe Kington
Joe Kington

Reputation: 284880

Typically, you'd use annotate to do this.

The key is to place the text with the x-coordinates in axes coordinates (so it's aligned with the axes) and the y-coordinates in figure coordinates (so it's at the bottom of the figure) and then add an offset in points so it's not at the exact bottom of the figure.

As a complete example (I'm also showing an example of using the extent kwarg with imshow just in case you weren't aware of it):

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((10, 10))

fig, ax = plt.subplots()
im = ax.imshow(data, interpolation='nearest', cmap='gist_earth', aspect='auto',
               extent=[220, 2000, 3000, 330])

ax.invert_yaxis()
ax.set(xlabel='Easting (m)', ylabel='Northing (m)', title='This is a title')
fig.colorbar(im, orientation='horizontal').set_label('Water Depth (m)')

# Now let's add your additional information
ax.annotate('...Additional information...',
            xy=(0.5, 0), xytext=(0, 10),
            xycoords=('axes fraction', 'figure fraction'),
            textcoords='offset points',
            size=14, ha='center', va='bottom')


plt.show()

enter image description here

Most of this is reproducing something similar to your example. The key is the annotate call.

Annotate is most commonly used to text at a position (xytext) relative to a point (xy) and optionally connect the text and the point with an arrow, which we'll skip here.

This is a bit complex, so let's break it down:

ax.annotate('...Additional information...',  # Your string

            # The point that we'll place the text in relation to 
            xy=(0.5, 0), 
            # Interpret the x as axes coords, and the y as figure coords
            xycoords=('axes fraction', 'figure fraction'),

            # The distance from the point that the text will be at
            xytext=(0, 10),  
            # Interpret `xytext` as an offset in points...
            textcoords='offset points',

            # Any other text parameters we'd like
            size=14, ha='center', va='bottom')

Hopefully that helps. The Annotation guides (intro and detailed) in the documentation are quite useful as further reading.

Upvotes: 14

Related Questions