FaC*_*fee 4 python plot text label matplotlib
我有matplotlib库生成的矩阵图.我的矩阵的大小是256x256,我已经有一个图例和一个带有正确刻度的颜色条.由于我是stackoverflow的新手,我无法附加任何图像.无论如何,我使用此代码生成图:
# 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)')
Run Code Online (Sandbox Code Playgroud)
现在,我的副标题太长了(这里报道的摘录中的第3行代码),它干扰了Y轴刻度,我不想要这个.相反,字幕中报告的一些信息我想要重新路由到一行文本,放置在图像的底部中心,在彩条标签下.如何用matplotlib完成?
很抱歉无法附加图片.谢谢.
通常,您annotate会这样做.
关键是将带有x坐标的文本放在轴坐标中(因此它与轴对齐)和图坐标中的y坐标(因此它位于图的底部)然后添加一个点的偏移量,这样就可以了不在图的确切底部.
作为一个完整的例子(我还展示了一个使用extentkwarg 的例子,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'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()
Run Code Online (Sandbox Code Playgroud)
其中大部分都是复制类似于你的例子.关键是annotate电话.
Annotate最常用于在xytext相对于point(xy)的position()处发送文本,并可选择使用箭头连接文本和点,我们将在此处跳过.
这有点复杂,所以让我们分解一下:
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')
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助.文档中的注释指南(简介和详细内容)在进一步阅读时非常有用.