Python: displaying a string of text outside matplotlib chart

I have a matrix chart created by matplotlib library. The size of my matrix is ​​256x256, and I already have a legend and a color panel with the correct ticks. I cannot attach an image because I was new to stackoverflow. Anyway, I use this code to generate the graph:

# 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 subtitles for too long (the 3rd line of code in the passage indicated here), it interferes with the ticks of the Y-axis, and I do not want this. Instead, I would like to redirect some of the information reported in the subtitles to a line of text that will be placed at the bottom of the image under the color label. How can this be done with matplotlib?

Sorry for not being able to attach the image. Thanks.

+4
1

, annotate.

, x ( ) y- (, ), .

( extent kwarg imshow , ):

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 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

. annotate.

Annotate (xytext) (xy) , .

, :

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')

, . (intro ) .

+7

Source: https://habr.com/ru/post/1608684/


All Articles